changeset 42678:f95b59ffc307

byteify-strings: add --treat-as-kwargs argument to handle kwargs-like objects This argument will help extensions move to Python 3 as keyword arguments should not be byte-prefixed. Most of the time, code bases will call this object `kwargs`, but other conventions exist like `opts`, so it should make sense to allow for custom names. This is a best effort solution that does minimal static checking; cases like `options = [o for o in ('a', 'b', 'c') if kwargs.get(o)]` and other just as complicated will not be detected.
author Raphaël Gomès <rgomes@octobus.net>
date Fri, 02 Aug 2019 10:18:22 +0200
parents c9fd8163131f
children 5e296f618920
files contrib/byteify-strings.py
diffstat 1 files changed, 11 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/contrib/byteify-strings.py	Fri Aug 02 10:10:23 2019 +0200
+++ b/contrib/byteify-strings.py	Fri Aug 02 10:18:22 2019 +0200
@@ -228,6 +228,12 @@
                 yield adjusttokenpos(t._replace(string=fn[4:]), coloffset)
                 continue
 
+        if t.type == token.NAME and t.string in opts['treat-as-kwargs']:
+            if _isitemaccess(i):
+                _ensuresysstr(i + 2)
+            if _ismethodcall(i, 'get', 'pop', 'setdefault', 'popitem'):
+                _ensuresysstr(i + 4)
+
         # Looks like "if __name__ == '__main__'".
         if (t.type == token.NAME and t.string == '__name__'
             and _isop(i + 1, '==')):
@@ -270,10 +276,15 @@
                     help='edit files in place')
     ap.add_argument('--dictiter', action='store_true', default=False,
                     help='rewrite iteritems() and itervalues()'),
+    ap.add_argument('--treat-as-kwargs', nargs="+",
+                    help="ignore kwargs-like objects"),
     ap.add_argument('files', metavar='FILE', nargs='+', help='source file')
     args = ap.parse_args()
     opts = {
         'dictiter': args.dictiter,
+        'treat-as-kwargs': set(
+            args.treat_as_kwargs
+        ) if args.treat_as_kwargs else set()
     }
     for fname in args.files:
         if args.inplace: