# HG changeset patch # User Brett Cannon # Date 1427467526 14400 # Node ID 2104fc9aa513320587d4900f0074a43d6a62dee4 # Parent 6564544576b9258287e789d7365def198ee861e6 util: make cmdbuilder work with bytes (issue4520) diff -r 6564544576b9 -r 2104fc9aa513 hglib/util.py --- a/hglib/util.py Wed Mar 25 20:23:41 2015 -0400 +++ b/hglib/util.py Fri Mar 27 10:45:26 2015 -0400 @@ -15,6 +15,7 @@ else: from itertools import izip integertypes = (long, int) + bytes = str # Defined in Python 2.6/2.7, but to the same value. def b(s): """Encode the string as bytes.""" @@ -69,6 +70,12 @@ return b('') +def _cmdval(val): + if isinstance(val, bytes): + return val + else: + return strtobytes(val) + def cmdbuilder(name, *args, **kwargs): """ A helper for building the command arguments @@ -83,27 +90,28 @@ None arguments are skipped - >>> cmdbuilder('cmd', a=True, b=False, c=None) - ['cmd', '-a'] - >>> cmdbuilder('cmd', long=True) - ['cmd', '--long'] - >>> cmdbuilder('cmd', str='s') - ['cmd', '--str', 's'] - >>> cmdbuilder('cmd', d_ash=True) - ['cmd', '--d-ash'] - >>> cmdbuilder('cmd', _=True) - ['cmd', '-'] - >>> cmdbuilder('cmd', list=[1, 2]) - ['cmd', '--list', '1', '--list', '2'] - >>> cmdbuilder('cmd', None) - ['cmd'] + >>> cmdbuilder(b('cmd'), a=True, b=False, c=None) == [b('cmd'), b('-a')] + True + >>> cmdbuilder(b('cmd'), long=True) == [b('cmd'), b('--long')] + True + >>> cmdbuilder(b('cmd'), str=b('s')) == [b('cmd'), b('--str'), b('s')] + True + >>> cmdbuilder(b('cmd'), d_ash=True) == [b('cmd'), b('--d-ash')] + True + >>> cmdbuilder(b('cmd'), _=True) == [b('cmd'), b('-')] + True + >>> expect = [b('cmd'), b('--list'), b('1'), b('--list'), b('2')] + >>> cmdbuilder(b('cmd'), list=[1, 2]) == expect + True + >>> cmdbuilder(b('cmd'), None) == [b('cmd')] + True """ cmd = [name] for arg, val in kwargs.items(): if val is None: continue - arg = arg.replace(b('_'), b('-')) + arg = arg.encode('latin-1').replace(b('_'), b('-')) if arg != b('-'): if len(arg) == 1: arg = b('-') + arg @@ -115,10 +123,10 @@ elif isinstance(val, list): for v in val: cmd.append(arg) - cmd.append(str(v)) + cmd.append(_cmdval(v)) else: cmd.append(arg) - cmd.append(str(val)) + cmd.append(_cmdval(val)) for a in args: if a is not None: