comparison mercurial/__init__.py @ 43076:2372284d9457

formatting: blacken the codebase This is using my patch to black (https://github.com/psf/black/pull/826) so we don't un-wrap collection literals. Done with: hg files 'set:**.py - mercurial/thirdparty/** - "contrib/python-zstandard/**"' | xargs black -S # skip-blame mass-reformatting only # no-check-commit reformats foo_bar functions Differential Revision: https://phab.mercurial-scm.org/D6971
author Augie Fackler <augie@google.com>
date Sun, 06 Oct 2019 09:45:02 -0400
parents 3018749a71bb
children 88eba7103660
comparison
equal deleted inserted replaced
43075:57875cf423c9 43076:2372284d9457
9 9
10 import sys 10 import sys
11 11
12 # Allow 'from mercurial import demandimport' to keep working. 12 # Allow 'from mercurial import demandimport' to keep working.
13 import hgdemandimport 13 import hgdemandimport
14
14 demandimport = hgdemandimport 15 demandimport = hgdemandimport
15 16
16 __all__ = [] 17 __all__ = []
17 18
18 # Python 3 uses a custom module loader that transforms source code between 19 # Python 3 uses a custom module loader that transforms source code between
25 import token 26 import token
26 import tokenize 27 import tokenize
27 28
28 class hgpathentryfinder(importlib.abc.MetaPathFinder): 29 class hgpathentryfinder(importlib.abc.MetaPathFinder):
29 """A sys.meta_path finder that uses a custom module loader.""" 30 """A sys.meta_path finder that uses a custom module loader."""
31
30 def find_spec(self, fullname, path, target=None): 32 def find_spec(self, fullname, path, target=None):
31 # Only handle Mercurial-related modules. 33 # Only handle Mercurial-related modules.
32 if not fullname.startswith(('mercurial.', 'hgext.')): 34 if not fullname.startswith(('mercurial.', 'hgext.')):
33 return None 35 return None
34 # don't try to parse binary 36 # don't try to parse binary
76 # TODO need to support loaders from alternate specs, like zip 78 # TODO need to support loaders from alternate specs, like zip
77 # loaders. 79 # loaders.
78 loader = hgloader(spec.name, spec.origin) 80 loader = hgloader(spec.name, spec.origin)
79 # Can't use util.safehasattr here because that would require 81 # Can't use util.safehasattr here because that would require
80 # importing util, and we're in import code. 82 # importing util, and we're in import code.
81 if hasattr(spec.loader, 'loader'): # hasattr-py3-only 83 if hasattr(spec.loader, 'loader'): # hasattr-py3-only
82 # This is a nested loader (maybe a lazy loader?) 84 # This is a nested loader (maybe a lazy loader?)
83 spec.loader.loader = loader 85 spec.loader.loader = loader
84 else: 86 else:
85 spec.loader = loader 87 spec.loader = loader
86 return spec 88 return spec
182 yield t._replace(string='b%s' % t.string) 184 yield t._replace(string='b%s' % t.string)
183 continue 185 continue
184 186
185 # Insert compatibility imports at "from __future__ import" line. 187 # Insert compatibility imports at "from __future__ import" line.
186 # No '\n' should be added to preserve line numbers. 188 # No '\n' should be added to preserve line numbers.
187 if (t.type == token.NAME and t.string == 'import' and 189 if (
188 all(u.type == token.NAME for u in tokens[i - 2:i]) and 190 t.type == token.NAME
189 [u.string for u in tokens[i - 2:i]] == ['from', '__future__']): 191 and t.string == 'import'
192 and all(u.type == token.NAME for u in tokens[i - 2 : i])
193 and [u.string for u in tokens[i - 2 : i]]
194 == ['from', '__future__']
195 ):
190 futureimpline = True 196 futureimpline = True
191 if t.type == token.NEWLINE and futureimpline: 197 if t.type == token.NEWLINE and futureimpline:
192 futureimpline = False 198 futureimpline = False
193 if fullname == 'mercurial.pycompat': 199 if fullname == 'mercurial.pycompat':
194 yield t 200 yield t
195 continue 201 continue
196 r, c = t.start 202 r, c = t.start
197 l = (b'; from mercurial.pycompat import ' 203 l = (
198 b'delattr, getattr, hasattr, setattr, ' 204 b'; from mercurial.pycompat import '
199 b'open, unicode\n') 205 b'delattr, getattr, hasattr, setattr, '
206 b'open, unicode\n'
207 )
200 for u in tokenize.tokenize(io.BytesIO(l).readline): 208 for u in tokenize.tokenize(io.BytesIO(l).readline):
201 if u.type in (tokenize.ENCODING, token.ENDMARKER): 209 if u.type in (tokenize.ENCODING, token.ENDMARKER):
202 continue 210 continue
203 yield u._replace( 211 yield u._replace(
204 start=(r, c + u.start[1]), end=(r, c + u.end[1])) 212 start=(r, c + u.start[1]), end=(r, c + u.end[1])
213 )
205 continue 214 continue
206 215
207 # This looks like a function call. 216 # This looks like a function call.
208 if t.type == token.NAME and _isop(i + 1, '('): 217 if t.type == token.NAME and _isop(i + 1, '('):
209 fn = t.string 218 fn = t.string
210 219
211 # *attr() builtins don't accept byte strings to 2nd argument. 220 # *attr() builtins don't accept byte strings to 2nd argument.
212 if (fn in ('getattr', 'setattr', 'hasattr', 'safehasattr') and 221 if fn in (
213 not _isop(i - 1, '.')): 222 'getattr',
223 'setattr',
224 'hasattr',
225 'safehasattr',
226 ) and not _isop(i - 1, '.'):
214 arg1idx = _findargnofcall(1) 227 arg1idx = _findargnofcall(1)
215 if arg1idx is not None: 228 if arg1idx is not None:
216 _ensureunicode(arg1idx) 229 _ensureunicode(arg1idx)
217 230
218 # .encode() and .decode() on str/bytes/unicode don't accept 231 # .encode() and .decode() on str/bytes/unicode don't accept
223 if argidx is not None: 236 if argidx is not None:
224 _ensureunicode(argidx) 237 _ensureunicode(argidx)
225 238
226 # It changes iteritems/values to items/values as they are not 239 # It changes iteritems/values to items/values as they are not
227 # present in Python 3 world. 240 # present in Python 3 world.
228 elif (fn in ('iteritems', 'itervalues') and 241 elif fn in ('iteritems', 'itervalues') and not (
229 not (tokens[i - 1].type == token.NAME and 242 tokens[i - 1].type == token.NAME
230 tokens[i - 1].string == 'def')): 243 and tokens[i - 1].string == 'def'
244 ):
231 yield t._replace(string=fn[4:]) 245 yield t._replace(string=fn[4:])
232 continue 246 continue
233 247
234 # Emit unmodified token. 248 # Emit unmodified token.
235 yield t 249 yield t
267 cached bytecode should be invalidated when transformations change. 281 cached bytecode should be invalidated when transformations change.
268 282
269 The added header has the form ``HG<VERSION>``. That is a literal 283 The added header has the form ``HG<VERSION>``. That is a literal
270 ``HG`` with 2 binary bytes indicating the transformation version. 284 ``HG`` with 2 binary bytes indicating the transformation version.
271 """ 285 """
286
272 def get_data(self, path): 287 def get_data(self, path):
273 data = super(hgloader, self).get_data(path) 288 data = super(hgloader, self).get_data(path)
274 289
275 if not path.endswith(tuple(importlib.machinery.BYTECODE_SUFFIXES)): 290 if not path.endswith(tuple(importlib.machinery.BYTECODE_SUFFIXES)):
276 return data 291 return data