Mercurial > hg
comparison hgext/convert/common.py @ 49288:ef5f5f1cbd90
py3: constant-fold some `pycompat.ispy3`
author | Manuel Jacob <me@manueljacob.de> |
---|---|
date | Tue, 31 May 2022 00:50:29 +0200 |
parents | 642e31cb55f0 |
children | 2e726c934fcd |
comparison
equal
deleted
inserted
replaced
49287:7fe82a5101c9 | 49288:ef5f5f1cbd90 |
---|---|
53 return self._l.lineno | 53 return self._l.lineno |
54 | 54 |
55 | 55 |
56 def shlexer(data=None, filepath=None, wordchars=None, whitespace=None): | 56 def shlexer(data=None, filepath=None, wordchars=None, whitespace=None): |
57 if data is None: | 57 if data is None: |
58 if pycompat.ispy3: | 58 data = open(filepath, b'r', encoding='latin1') |
59 data = open(filepath, b'r', encoding='latin1') | |
60 else: | |
61 data = open(filepath, b'r') | |
62 else: | 59 else: |
63 if filepath is not None: | 60 if filepath is not None: |
64 raise error.ProgrammingError( | 61 raise error.ProgrammingError( |
65 b'shlexer only accepts data or filepath, not both' | 62 b'shlexer only accepts data or filepath, not both' |
66 ) | 63 ) |
67 if pycompat.ispy3: | 64 data = data.decode('latin1') |
68 data = data.decode('latin1') | |
69 l = shlex.shlex(data, infile=filepath, posix=True) | 65 l = shlex.shlex(data, infile=filepath, posix=True) |
70 if whitespace is not None: | 66 if whitespace is not None: |
71 l.whitespace_split = True | 67 l.whitespace_split = True |
72 if pycompat.ispy3: | 68 l.whitespace += whitespace.decode('latin1') |
73 l.whitespace += whitespace.decode('latin1') | |
74 else: | |
75 l.whitespace += whitespace | |
76 if wordchars is not None: | 69 if wordchars is not None: |
77 if pycompat.ispy3: | 70 l.wordchars += wordchars.decode('latin1') |
78 l.wordchars += wordchars.decode('latin1') | 71 return _shlexpy3proxy(l) |
79 else: | |
80 l.wordchars += wordchars | |
81 if pycompat.ispy3: | |
82 return _shlexpy3proxy(l) | |
83 return l | |
84 | |
85 | |
86 if pycompat.ispy3: | |
87 base64_encodebytes = base64.encodebytes | |
88 base64_decodebytes = base64.decodebytes | |
89 else: | |
90 base64_encodebytes = base64.encodestring | |
91 base64_decodebytes = base64.decodestring | |
92 | 72 |
93 | 73 |
94 def encodeargs(args): | 74 def encodeargs(args): |
95 def encodearg(s): | 75 def encodearg(s): |
96 lines = base64_encodebytes(s) | 76 lines = base64.encodebytes(s) |
97 lines = [l.splitlines()[0] for l in pycompat.iterbytestr(lines)] | 77 lines = [l.splitlines()[0] for l in pycompat.iterbytestr(lines)] |
98 return b''.join(lines) | 78 return b''.join(lines) |
99 | 79 |
100 s = pickle.dumps(args) | 80 s = pickle.dumps(args) |
101 return encodearg(s) | 81 return encodearg(s) |
102 | 82 |
103 | 83 |
104 def decodeargs(s): | 84 def decodeargs(s): |
105 s = base64_decodebytes(s) | 85 s = base64.decodebytes(s) |
106 return pickle.loads(s) | 86 return pickle.loads(s) |
107 | 87 |
108 | 88 |
109 class MissingTool(Exception): | 89 class MissingTool(Exception): |
110 pass | 90 pass |