changeset 38394:f701bc936e7f

byteify-strings: do not rewrite iteritems() and itervalues() by default We can't do that automatically due to performance concerns.
author Yuya Nishihara <yuya@tcha.org>
date Thu, 31 May 2018 22:34:23 +0900
parents b704da9a9dda
children 1d68fd5f614a
files contrib/byteify-strings.py
diffstat 1 files changed, 11 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/contrib/byteify-strings.py	Thu May 31 22:31:37 2018 +0900
+++ b/contrib/byteify-strings.py	Thu May 31 22:34:23 2018 +0900
@@ -19,7 +19,7 @@
 import tokenize
 
 if True:
-    def replacetokens(tokens):
+    def replacetokens(tokens, opts):
         """Transform a stream of tokens from raw to Python 3.
 
         Returns a generator of possibly rewritten tokens.
@@ -129,16 +129,16 @@
 
                 # It changes iteritems/values to items/values as they are not
                 # present in Python 3 world.
-                elif fn in ('iteritems', 'itervalues'):
+                elif opts['dictiter'] and fn in ('iteritems', 'itervalues'):
                     yield t._replace(string=fn[4:])
                     continue
 
             # Emit unmodified token.
             yield t
 
-def process(fin, fout):
+def process(fin, fout, opts):
     tokens = tokenize.tokenize(fin.readline)
-    tokens = replacetokens(list(tokens))
+    tokens = replacetokens(list(tokens), opts)
     fout.write(tokenize.untokenize(tokens))
 
 def tryunlink(fname):
@@ -168,17 +168,22 @@
     ap = argparse.ArgumentParser()
     ap.add_argument('-i', '--inplace', action='store_true', default=False,
                     help='edit files in place')
+    ap.add_argument('--dictiter', action='store_true', default=False,
+                    help='rewrite iteritems() and itervalues()'),
     ap.add_argument('files', metavar='FILE', nargs='+', help='source file')
     args = ap.parse_args()
+    opts = {
+        'dictiter': args.dictiter,
+    }
     for fname in args.files:
         if args.inplace:
             with editinplace(fname) as fout:
                 with open(fname, 'rb') as fin:
-                    process(fin, fout)
+                    process(fin, fout, opts)
         else:
             with open(fname, 'rb') as fin:
                 fout = sys.stdout.buffer
-                process(fin, fout)
+                process(fin, fout, opts)
 
 if __name__ == '__main__':
     main()