# HG changeset patch # User Raphaël Gomès # Date 2019-08-02 14:17:02 # Node ID bbb002b378f33620d70d71872c869f3d67262ddf # Parent 5e296f618920911dbd77ef0c40b96a7a3397c89b byteify-strings: add cli argument to handle `attr*()` when they are methods Certain code bases have useful utils that wrap the builtin functions, and are called like `util.setattr`. diff --git a/contrib/byteify-strings.py b/contrib/byteify-strings.py --- a/contrib/byteify-strings.py +++ b/contrib/byteify-strings.py @@ -208,8 +208,10 @@ def replacetokens(tokens, opts): fn = t.string # *attr() builtins don't accept byte strings to 2nd argument. - if (fn in ('getattr', 'setattr', 'hasattr', 'safehasattr') and - not _isop(i - 1, '.')): + if fn in ( + 'getattr', 'setattr', 'hasattr', 'safehasattr', 'wrapfunction', + 'wrapclass', 'addattr' + ) and (opts['allow-attr-methods'] or not _isop(i - 1, '.')): arg1idx = _findargnofcall(1) if arg1idx is not None: _ensuresysstr(arg1idx) @@ -276,6 +278,9 @@ def main(): help='edit files in place') ap.add_argument('--dictiter', action='store_true', default=False, help='rewrite iteritems() and itervalues()'), + ap.add_argument('--allow-attr-methods', action='store_true', + default=False, + help='also handle attr*() when they are methods'), ap.add_argument('--treat-as-kwargs', nargs="+", default=[], help="ignore kwargs-like objects"), ap.add_argument('files', metavar='FILE', nargs='+', help='source file') @@ -283,6 +288,7 @@ def main(): opts = { 'dictiter': args.dictiter, 'treat-as-kwargs': set(args.treat_as_kwargs), + 'allow-attr-methods': args.allow_attr_methods, } for fname in args.files: if args.inplace: