comparison mercurial/__init__.py @ 30166:102e6ef5bb3a

py3: use namedtuple._replace to produce new tokens
author Martijn Pieters <mjpieters@fb.com>
date Thu, 13 Oct 2016 09:27:37 +0100
parents 423377290a3a
children 76a64c1e5439
comparison
equal deleted inserted replaced
30165:423377290a3a 30166:102e6ef5bb3a
231 already been done. 231 already been done.
232 232
233 """ 233 """
234 st = tokens[j] 234 st = tokens[j]
235 if st.type == token.STRING and st.string.startswith(("'", '"')): 235 if st.type == token.STRING and st.string.startswith(("'", '"')):
236 rt = tokenize.TokenInfo(st.type, 'u%s' % st.string, 236 tokens[j] = st._replace(string='u%s' % st.string)
237 st.start, st.end, st.line)
238 tokens[j] = rt
239 237
240 for i, t in enumerate(tokens): 238 for i, t in enumerate(tokens):
241 # Convert most string literals to byte literals. String literals 239 # Convert most string literals to byte literals. String literals
242 # in Python 2 are bytes. String literals in Python 3 are unicode. 240 # in Python 2 are bytes. String literals in Python 3 are unicode.
243 # Most strings in Mercurial are bytes and unicode strings are rare. 241 # Most strings in Mercurial are bytes and unicode strings are rare.
264 if s[0] not in ("'", '"'): 262 if s[0] not in ("'", '"'):
265 yield t 263 yield t
266 continue 264 continue
267 265
268 # String literal. Prefix to make a b'' string. 266 # String literal. Prefix to make a b'' string.
269 yield tokenize.TokenInfo(t.type, 'b%s' % s, t.start, t.end, 267 yield t._replace(string='b%s' % t.string)
270 t.line)
271 continue 268 continue
272 269
273 # Insert compatibility imports at "from __future__ import" line. 270 # Insert compatibility imports at "from __future__ import" line.
274 # No '\n' should be added to preserve line numbers. 271 # No '\n' should be added to preserve line numbers.
275 if (t.type == token.NAME and t.string == 'import' and 272 if (t.type == token.NAME and t.string == 'import' and
285 l = (b'; from mercurial.pycompat import ' 282 l = (b'; from mercurial.pycompat import '
286 b'delattr, getattr, hasattr, setattr, xrange\n') 283 b'delattr, getattr, hasattr, setattr, xrange\n')
287 for u in tokenize.tokenize(io.BytesIO(l).readline): 284 for u in tokenize.tokenize(io.BytesIO(l).readline):
288 if u.type in (tokenize.ENCODING, token.ENDMARKER): 285 if u.type in (tokenize.ENCODING, token.ENDMARKER):
289 continue 286 continue
290 yield tokenize.TokenInfo(u.type, u.string, 287 yield u._replace(
291 (r, c + u.start[1]), 288 start=(r, c + u.start[1]), end=(r, c + u.end[1]))
292 (r, c + u.end[1]),
293 '')
294 continue 289 continue
295 290
296 # This looks like a function call. 291 # This looks like a function call.
297 if t.type == token.NAME and _isop(i + 1, '('): 292 if t.type == token.NAME and _isop(i + 1, '('):
298 fn = t.string 293 fn = t.string
320 _ensureunicode(arg1idx) 315 _ensureunicode(arg1idx)
321 316
322 # It changes iteritems to items as iteritems is not 317 # It changes iteritems to items as iteritems is not
323 # present in Python 3 world. 318 # present in Python 3 world.
324 elif fn == 'iteritems': 319 elif fn == 'iteritems':
325 yield tokenize.TokenInfo(t.type, 'items', 320 yield t._replace(string='items')
326 t.start, t.end, t.line)
327 continue 321 continue
328 322
329 # Emit unmodified token. 323 # Emit unmodified token.
330 yield t 324 yield t
331 325