##// END OF EJS Templates
py3: import builtin wrappers automagically by code transformer...
Yuya Nishihara -
r29800:178c89e8 default
parent child Browse files
Show More
@@ -170,7 +170,7 b' if sys.version_info[0] >= 3:'
170 spec.loader = hgloader(spec.name, spec.origin)
170 spec.loader = hgloader(spec.name, spec.origin)
171 return spec
171 return spec
172
172
173 def replacetokens(tokens):
173 def replacetokens(tokens, fullname):
174 """Transform a stream of tokens from raw to Python 3.
174 """Transform a stream of tokens from raw to Python 3.
175
175
176 It is called by the custom module loading machinery to rewrite
176 It is called by the custom module loading machinery to rewrite
@@ -184,6 +184,7 b' if sys.version_info[0] >= 3:'
184 REMEMBER TO CHANGE ``BYTECODEHEADER`` WHEN CHANGING THIS FUNCTION
184 REMEMBER TO CHANGE ``BYTECODEHEADER`` WHEN CHANGING THIS FUNCTION
185 OR CACHED FILES WON'T GET INVALIDATED PROPERLY.
185 OR CACHED FILES WON'T GET INVALIDATED PROPERLY.
186 """
186 """
187 futureimpline = False
187 for i, t in enumerate(tokens):
188 for i, t in enumerate(tokens):
188 # Convert most string literals to byte literals. String literals
189 # Convert most string literals to byte literals. String literals
189 # in Python 2 are bytes. String literals in Python 3 are unicode.
190 # in Python 2 are bytes. String literals in Python 3 are unicode.
@@ -217,6 +218,29 b' if sys.version_info[0] >= 3:'
217 t.line)
218 t.line)
218 continue
219 continue
219
220
221 # Insert compatibility imports at "from __future__ import" line.
222 # No '\n' should be added to preserve line numbers.
223 if (t.type == token.NAME and t.string == 'import' and
224 all(u.type == token.NAME for u in tokens[i - 2:i]) and
225 [u.string for u in tokens[i - 2:i]] == ['from', '__future__']):
226 futureimpline = True
227 if t.type == token.NEWLINE and futureimpline:
228 futureimpline = False
229 if fullname == 'mercurial.pycompat':
230 yield t
231 continue
232 r, c = t.start
233 l = (b'; from mercurial.pycompat import '
234 b'delattr, getattr, hasattr, setattr, xrange\n')
235 for u in tokenize.tokenize(io.BytesIO(l).readline):
236 if u.type in (tokenize.ENCODING, token.ENDMARKER):
237 continue
238 yield tokenize.TokenInfo(u.type, u.string,
239 (r, c + u.start[1]),
240 (r, c + u.end[1]),
241 '')
242 continue
243
220 try:
244 try:
221 nexttoken = tokens[i + 1]
245 nexttoken = tokens[i + 1]
222 except IndexError:
246 except IndexError:
@@ -279,7 +303,7 b' if sys.version_info[0] >= 3:'
279 # ``replacetoken`` or any mechanism that changes semantics of module
303 # ``replacetoken`` or any mechanism that changes semantics of module
280 # loading is changed. Otherwise cached bytecode may get loaded without
304 # loading is changed. Otherwise cached bytecode may get loaded without
281 # the new transformation mechanisms applied.
305 # the new transformation mechanisms applied.
282 BYTECODEHEADER = b'HG\x00\x01'
306 BYTECODEHEADER = b'HG\x00\x02'
283
307
284 class hgloader(importlib.machinery.SourceFileLoader):
308 class hgloader(importlib.machinery.SourceFileLoader):
285 """Custom module loader that transforms source code.
309 """Custom module loader that transforms source code.
@@ -338,7 +362,7 b' if sys.version_info[0] >= 3:'
338 """Perform token transformation before compilation."""
362 """Perform token transformation before compilation."""
339 buf = io.BytesIO(data)
363 buf = io.BytesIO(data)
340 tokens = tokenize.tokenize(buf.readline)
364 tokens = tokenize.tokenize(buf.readline)
341 data = tokenize.untokenize(replacetokens(list(tokens)))
365 data = tokenize.untokenize(replacetokens(list(tokens), self.name))
342 # Python's built-in importer strips frames from exceptions raised
366 # Python's built-in importer strips frames from exceptions raised
343 # for this code. Unfortunately, that mechanism isn't extensible
367 # for this code. Unfortunately, that mechanism isn't extensible
344 # and our frame will be blamed for the import failure. There
368 # and our frame will be blamed for the import failure. There
@@ -32,7 +32,6 b' else:'
32 if sys.version_info[0] >= 3:
32 if sys.version_info[0] >= 3:
33 import builtins
33 import builtins
34 import functools
34 import functools
35 builtins.xrange = range
36
35
37 def _wrapattrfunc(f):
36 def _wrapattrfunc(f):
38 @functools.wraps(f)
37 @functools.wraps(f)
@@ -42,10 +41,12 b' if sys.version_info[0] >= 3:'
42 return f(object, name, *args)
41 return f(object, name, *args)
43 return w
42 return w
44
43
44 # these wrappers are automagically imported by hgloader
45 delattr = _wrapattrfunc(builtins.delattr)
45 delattr = _wrapattrfunc(builtins.delattr)
46 getattr = _wrapattrfunc(builtins.getattr)
46 getattr = _wrapattrfunc(builtins.getattr)
47 hasattr = _wrapattrfunc(builtins.hasattr)
47 hasattr = _wrapattrfunc(builtins.hasattr)
48 setattr = _wrapattrfunc(builtins.setattr)
48 setattr = _wrapattrfunc(builtins.setattr)
49 xrange = builtins.range
49
50
50 stringio = io.StringIO
51 stringio = io.StringIO
51 empty = _queue.Empty
52 empty = _queue.Empty
@@ -48,7 +48,7 b''
48 hgext/gpg.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
48 hgext/gpg.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
49 hgext/graphlog.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
49 hgext/graphlog.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
50 hgext/hgk.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
50 hgext/hgk.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
51 hgext/highlight/highlight.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
51 hgext/highlight/highlight.py: error importing module: <ImportError> No module named 'pygments' (line 13)
52 hgext/histedit.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
52 hgext/histedit.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
53 hgext/journal.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
53 hgext/journal.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
54 hgext/keyword.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
54 hgext/keyword.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
@@ -122,52 +122,51 b''
122 mercurial/hook.py: error importing: <TypeError> str expected, not bytes (error at i18n.py:*) (glob)
122 mercurial/hook.py: error importing: <TypeError> str expected, not bytes (error at i18n.py:*) (glob)
123 mercurial/httpconnection.py: error importing: <TypeError> str expected, not bytes (error at i18n.py:*) (glob)
123 mercurial/httpconnection.py: error importing: <TypeError> str expected, not bytes (error at i18n.py:*) (glob)
124 mercurial/httppeer.py: error importing: <TypeError> str expected, not bytes (error at i18n.py:*) (glob)
124 mercurial/httppeer.py: error importing: <TypeError> str expected, not bytes (error at i18n.py:*) (glob)
125 mercurial/keepalive.py: error importing: <TypeError> getattr(): attribute name must be string (error at pycompat.py:*) (glob)
125 mercurial/keepalive.py: error importing: <TypeError> __slots__ items must be strings, not 'bytes' (error at util.py:558)
126 mercurial/localrepo.py: error importing: <TypeError> getattr(): attribute name must be string (error at pycompat.py:*) (glob)
126 mercurial/localrepo.py: error importing: <TypeError> __slots__ items must be strings, not 'bytes' (error at util.py:558)
127 mercurial/lock.py: error importing: <TypeError> getattr(): attribute name must be string (error at pycompat.py:*) (glob)
127 mercurial/lock.py: error importing: <TypeError> __slots__ items must be strings, not 'bytes' (error at util.py:558)
128 mercurial/mail.py: error importing: <TypeError> getattr(): attribute name must be string (error at pycompat.py:*) (glob)
128 mercurial/mail.py: error importing: <TypeError> __slots__ items must be strings, not 'bytes' (error at util.py:558)
129 mercurial/manifest.py: error importing: <TypeError> getattr(): attribute name must be string (error at pycompat.py:*) (glob)
129 mercurial/manifest.py: error importing: <TypeError> __slots__ items must be strings, not 'bytes' (error at util.py:558)
130 mercurial/match.py: error importing: <TypeError> getattr(): attribute name must be string (error at pycompat.py:*) (glob)
130 mercurial/match.py: error importing: <TypeError> __slots__ items must be strings, not 'bytes' (error at util.py:558)
131 mercurial/mdiff.py: error importing: <TypeError> getattr(): attribute name must be string (error at pycompat.py:*) (glob)
131 mercurial/mdiff.py: error importing: <TypeError> __slots__ items must be strings, not 'bytes' (error at util.py:558)
132 mercurial/merge.py: error importing: <TypeError> getattr(): attribute name must be string (error at pycompat.py:*) (glob)
132 mercurial/merge.py: error importing: <TypeError> __slots__ items must be strings, not 'bytes' (error at util.py:558)
133 mercurial/minirst.py: error importing: <TypeError> getattr(): attribute name must be string (error at pycompat.py:*) (glob)
133 mercurial/minirst.py: error importing: <TypeError> __slots__ items must be strings, not 'bytes' (error at util.py:558)
134 mercurial/namespaces.py: error importing: <TypeError> getattr(): attribute name must be string (error at pycompat.py:*) (glob)
134 mercurial/namespaces.py: error importing: <TypeError> __slots__ items must be strings, not 'bytes' (error at util.py:558)
135 mercurial/obsolete.py: error importing: <TypeError> getattr(): attribute name must be string (error at pycompat.py:*) (glob)
135 mercurial/obsolete.py: error importing: <TypeError> __slots__ items must be strings, not 'bytes' (error at util.py:558)
136 mercurial/patch.py: error importing: <TypeError> getattr(): attribute name must be string (error at pycompat.py:*) (glob)
136 mercurial/patch.py: error importing: <TypeError> __slots__ items must be strings, not 'bytes' (error at util.py:558)
137 mercurial/pathutil.py: error importing: <TypeError> getattr(): attribute name must be string (error at pycompat.py:*) (glob)
137 mercurial/pathutil.py: error importing: <TypeError> __slots__ items must be strings, not 'bytes' (error at util.py:558)
138 mercurial/peer.py: error importing: <TypeError> getattr(): attribute name must be string (error at pycompat.py:*) (glob)
138 mercurial/peer.py: error importing: <TypeError> __slots__ items must be strings, not 'bytes' (error at util.py:558)
139 mercurial/pure/mpatch.py: error importing: <TypeError> getattr(): attribute name must be string (error at pycompat.py:*) (glob)
139 mercurial/profiling.py: error importing: <TypeError> __slots__ items must be strings, not 'bytes' (error at util.py:558)
140 mercurial/pure/parsers.py: error importing: <TypeError> getattr(): attribute name must be string (error at pycompat.py:*) (glob)
140 mercurial/pushkey.py: error importing: <TypeError> __slots__ items must be strings, not 'bytes' (error at util.py:558)
141 mercurial/pushkey.py: error importing: <TypeError> getattr(): attribute name must be string (error at pycompat.py:*) (glob)
141 mercurial/pvec.py: error importing: <TypeError> __slots__ items must be strings, not 'bytes' (error at util.py:558)
142 mercurial/pvec.py: error importing: <TypeError> getattr(): attribute name must be string (error at pycompat.py:*) (glob)
142 mercurial/registrar.py: error importing: <TypeError> __slots__ items must be strings, not 'bytes' (error at util.py:558)
143 mercurial/registrar.py: error importing: <TypeError> getattr(): attribute name must be string (error at util.py:*) (glob)
143 mercurial/repair.py: error importing: <TypeError> __slots__ items must be strings, not 'bytes' (error at util.py:558)
144 mercurial/repair.py: error importing: <TypeError> getattr(): attribute name must be string (error at util.py:*) (glob)
144 mercurial/repoview.py: error importing: <TypeError> __slots__ items must be strings, not 'bytes' (error at util.py:558)
145 mercurial/repoview.py: error importing: <TypeError> getattr(): attribute name must be string (error at util.py:*) (glob)
145 mercurial/revlog.py: error importing: <TypeError> __slots__ items must be strings, not 'bytes' (error at util.py:558)
146 mercurial/revlog.py: error importing: <TypeError> getattr(): attribute name must be string (error at util.py:*) (glob)
146 mercurial/revset.py: error importing: <TypeError> __slots__ items must be strings, not 'bytes' (error at util.py:558)
147 mercurial/revset.py: error importing: <TypeError> getattr(): attribute name must be string (error at util.py:*) (glob)
147 mercurial/scmutil.py: error importing: <TypeError> __slots__ items must be strings, not 'bytes' (error at util.py:558)
148 mercurial/scmutil.py: error importing: <TypeError> getattr(): attribute name must be string (error at util.py:*) (glob)
148 mercurial/scmwindows.py: error importing: <TypeError> __slots__ items must be strings, not 'bytes' (error at util.py:558)
149 mercurial/scmwindows.py: error importing: <TypeError> getattr(): attribute name must be string (error at util.py:*) (glob)
149 mercurial/similar.py: error importing: <TypeError> __slots__ items must be strings, not 'bytes' (error at util.py:558)
150 mercurial/similar.py: error importing: <TypeError> getattr(): attribute name must be string (error at util.py:*) (glob)
150 mercurial/simplemerge.py: error importing: <TypeError> __slots__ items must be strings, not 'bytes' (error at util.py:558)
151 mercurial/simplemerge.py: error importing: <TypeError> getattr(): attribute name must be string (error at util.py:*) (glob)
151 mercurial/sshpeer.py: error importing: <TypeError> __slots__ items must be strings, not 'bytes' (error at util.py:558)
152 mercurial/sshpeer.py: error importing: <TypeError> getattr(): attribute name must be string (error at util.py:*) (glob)
152 mercurial/sshserver.py: error importing: <TypeError> __slots__ items must be strings, not 'bytes' (error at util.py:558)
153 mercurial/sshserver.py: error importing: <TypeError> getattr(): attribute name must be string (error at util.py:*) (glob)
153 mercurial/sslutil.py: error importing: <TypeError> __slots__ items must be strings, not 'bytes' (error at util.py:558)
154 mercurial/sslutil.py: error importing: <TypeError> getattr(): attribute name must be string (error at util.py:*) (glob)
154 mercurial/statichttprepo.py: error importing: <TypeError> __slots__ items must be strings, not 'bytes' (error at util.py:558)
155 mercurial/statichttprepo.py: error importing: <TypeError> getattr(): attribute name must be string (error at util.py:*) (glob)
155 mercurial/store.py: error importing: <TypeError> __slots__ items must be strings, not 'bytes' (error at util.py:558)
156 mercurial/store.py: error importing: <TypeError> getattr(): attribute name must be string (error at util.py:*) (glob)
156 mercurial/streamclone.py: error importing: <TypeError> __slots__ items must be strings, not 'bytes' (error at util.py:558)
157 mercurial/streamclone.py: error importing: <TypeError> getattr(): attribute name must be string (error at util.py:*) (glob)
157 mercurial/subrepo.py: error importing: <TypeError> __slots__ items must be strings, not 'bytes' (error at util.py:558)
158 mercurial/subrepo.py: error importing: <TypeError> getattr(): attribute name must be string (error at util.py:*) (glob)
158 mercurial/tagmerge.py: error importing: <TypeError> __slots__ items must be strings, not 'bytes' (error at util.py:558)
159 mercurial/tagmerge.py: error importing: <TypeError> getattr(): attribute name must be string (error at util.py:*) (glob)
159 mercurial/tags.py: error importing: <TypeError> __slots__ items must be strings, not 'bytes' (error at util.py:558)
160 mercurial/tags.py: error importing: <TypeError> getattr(): attribute name must be string (error at util.py:*) (glob)
160 mercurial/templatefilters.py: error importing: <TypeError> __slots__ items must be strings, not 'bytes' (error at util.py:558)
161 mercurial/templatefilters.py: error importing: <TypeError> getattr(): attribute name must be string (error at util.py:*) (glob)
161 mercurial/templatekw.py: error importing: <TypeError> __slots__ items must be strings, not 'bytes' (error at util.py:558)
162 mercurial/templatekw.py: error importing: <TypeError> getattr(): attribute name must be string (error at util.py:*) (glob)
162 mercurial/templater.py: error importing: <TypeError> __slots__ items must be strings, not 'bytes' (error at util.py:558)
163 mercurial/templater.py: error importing: <TypeError> getattr(): attribute name must be string (error at util.py:*) (glob)
163 mercurial/transaction.py: error importing: <TypeError> __slots__ items must be strings, not 'bytes' (error at util.py:558)
164 mercurial/transaction.py: error importing: <TypeError> getattr(): attribute name must be string (error at util.py:*) (glob)
164 mercurial/ui.py: error importing: <TypeError> __slots__ items must be strings, not 'bytes' (error at util.py:558)
165 mercurial/ui.py: error importing: <TypeError> getattr(): attribute name must be string (error at util.py:*) (glob)
165 mercurial/unionrepo.py: error importing: <TypeError> __slots__ items must be strings, not 'bytes' (error at util.py:558)
166 mercurial/unionrepo.py: error importing: <TypeError> getattr(): attribute name must be string (error at util.py:*) (glob)
166 mercurial/url.py: error importing: <TypeError> __slots__ items must be strings, not 'bytes' (error at util.py:558)
167 mercurial/url.py: error importing: <TypeError> getattr(): attribute name must be string (error at util.py:*) (glob)
167 mercurial/verify.py: error importing module: <TypeError> unorderable types: str() >= tuple() (line 884)
168 mercurial/verify.py: error importing: <TypeError> attribute name must be string, not 'bytes' (error at mdiff.py:*) (glob)
169 mercurial/win32.py: error importing module: <ImportError> No module named 'msvcrt' (line *) (glob)
168 mercurial/win32.py: error importing module: <ImportError> No module named 'msvcrt' (line *) (glob)
170 mercurial/windows.py: error importing module: <ImportError> No module named 'msvcrt' (line *) (glob)
169 mercurial/windows.py: error importing module: <ImportError> No module named 'msvcrt' (line *) (glob)
171 mercurial/wireproto.py: error importing module: <TypeError> a bytes-like object is required, not 'str' (line *) (glob)
170 mercurial/wireproto.py: error importing module: <TypeError> unorderable types: str() >= tuple() (line 884)
172
171
173 #endif
172 #endif
General Comments 0
You need to be logged in to leave comments. Login now