byteify-strings: add cli argument to handle `attr*()` when they are methods
authorRaphaël Gomès <rgomes@octobus.net>
Fri, 02 Aug 2019 16:17:02 +0200
changeset 42683 bbb002b378f3
parent 42679 5e296f618920
child 42685 863e9e7f8850
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`.
contrib/byteify-strings.py
--- a/contrib/byteify-strings.py	Fri Aug 02 16:14:00 2019 +0200
+++ b/contrib/byteify-strings.py	Fri Aug 02 16:17:02 2019 +0200
@@ -208,8 +208,10 @@
             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 @@
                     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 @@
     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: