# HG changeset patch # User Raphaël Gomès # Date 1564733902 -7200 # Node ID f95b59ffc307c4549d9640a81d750a99bd75f423 # Parent c9fd8163131fd4f2b23eb9f6bb2dd3ee4d909fd3 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. diff -r c9fd8163131f -r f95b59ffc307 contrib/byteify-strings.py --- 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: