[PATCH 3 of 3 RFC] mercurial: rewrite "iteritems" to "items"

Gregory Szorc gregory.szorc at gmail.com
Mon May 16 00:02:53 EDT 2016


# HG changeset patch
# User Gregory Szorc <gregory.szorc at gmail.com>
# Date 1463371090 25200
#      Sun May 15 20:58:10 2016 -0700
# Node ID 7e5fd80296fec5acd073b166b8c26d3b2ee3d01b
# Parent  effc44c0ec7440fabb134a076e974ca478d89e11
mercurial: rewrite "iteritems" to "items"

This is extremely hacky. We should probably verify the token
before is an OP token with value ".". But it does result
in not encountering an iteritems run-time failure!

This change demonstrates we can pave over some Python 2/3
differences using on-the-fly source transformations without
"contaminating" the original source code to be Python 3
aware.

With this change, we fail on some attribute access in mail.py.

diff --git a/mercurial/__init__.py b/mercurial/__init__.py
--- a/mercurial/__init__.py
+++ b/mercurial/__init__.py
@@ -197,16 +197,23 @@ if sys.version_info[0] >= 3:
                 return t
 
             if s[0] not in ("'", '"'):
                 return t
 
             # String literal. Prefix to make a b'' string.
             return tokenize.TokenInfo(t.type, 'b%s' % s, t.start, t.end, t.line)
 
+        if t.type == token.NAME:
+            if t.string == 'iteritems':
+                return tokenize.TokenInfo(t.type, 'items', t.start, t.end,
+                                          t.line)
+
+            return t
+
         return t
 
     class hgloader(importlib.machinery.SourceFileLoader):
         """Custom module loader that transforms source code.
 
         When the source code is converted to code, we first transform
         string literals to byte literals using the tokenize API.
         """


More information about the Mercurial-devel mailing list