comparison mercurial/__init__.py @ 43085:eef9a2d67051

py3: manually import pycompat.open into files that need it We want to eliminate the source transformer. Currently it inserts a `from mercurial.pycompat import ...` at the top of files to alias some builtins. This commit replaces the implicit import of `open` with an explicit import on files that need it and changes the source transformer to no longer import `open`. As part of this, we needed to store an explicit local for `open` in the Python 2 code path in `pycompat` so the import works. (Builtins that are automatically in scope cannot be imported.) Differential Revision: https://phab.mercurial-scm.org/D7005
author Gregory Szorc <gregory.szorc@gmail.com>
date Sun, 06 Oct 2019 13:28:56 -0400
parents 88eba7103660
children bbcbb82e3589
comparison
equal deleted inserted replaced
43084:c2e284cee333 43085:eef9a2d67051
170 continue 170 continue
171 r, c = t.start 171 r, c = t.start
172 l = ( 172 l = (
173 b'; from mercurial.pycompat import ' 173 b'; from mercurial.pycompat import '
174 b'delattr, getattr, hasattr, setattr, ' 174 b'delattr, getattr, hasattr, setattr, '
175 b'open, unicode\n' 175 b'unicode\n'
176 ) 176 )
177 for u in tokenize.tokenize(io.BytesIO(l).readline): 177 for u in tokenize.tokenize(io.BytesIO(l).readline):
178 if u.type in (tokenize.ENCODING, token.ENDMARKER): 178 if u.type in (tokenize.ENCODING, token.ENDMARKER):
179 continue 179 continue
180 yield u._replace( 180 yield u._replace(
219 219
220 # Header to add to bytecode files. This MUST be changed when 220 # Header to add to bytecode files. This MUST be changed when
221 # ``replacetoken`` or any mechanism that changes semantics of module 221 # ``replacetoken`` or any mechanism that changes semantics of module
222 # loading is changed. Otherwise cached bytecode may get loaded without 222 # loading is changed. Otherwise cached bytecode may get loaded without
223 # the new transformation mechanisms applied. 223 # the new transformation mechanisms applied.
224 BYTECODEHEADER = b'HG\x00\x0c' 224 BYTECODEHEADER = b'HG\x00\x0d'
225 225
226 class hgloader(importlib.machinery.SourceFileLoader): 226 class hgloader(importlib.machinery.SourceFileLoader):
227 """Custom module loader that transforms source code. 227 """Custom module loader that transforms source code.
228 228
229 When the source code is converted to a code object, we transform 229 When the source code is converted to a code object, we transform