Patchwork [2,of,5] byteify-strings: add cli argument to handle `attr*()` when they are methods

login
register
mail settings
Submitter Raphaël Gomès
Date Aug. 2, 2019, 3:01 p.m.
Message ID <fea16ec00093e73bf0aa.1564758083@alphare-carbon.lan>
Download mbox | patch
Permalink /patch/41129/
State Accepted
Headers show

Comments

Raphaël Gomès - Aug. 2, 2019, 3:01 p.m.
# HG changeset patch
# User Raphaël Gomès <rgomes@octobus.net>
# Date 1564755422 -7200
#      Fri Aug 02 16:17:02 2019 +0200
# Node ID fea16ec00093e73bf0aa4db3f2a709c385fc6336
# Parent  4a3e6c4ac759ed0d7678980328c0dec3824192ea
# EXP-Topic byteify-strings
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`.

Patch

diff -r 4a3e6c4ac759 -r fea16ec00093 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: