contrib/byteify-strings.py
changeset 42683 bbb002b378f3
parent 42679 5e296f618920
child 42687 26a31c88e1a5
equal deleted inserted replaced
42679:5e296f618920 42683:bbb002b378f3
   206         # This looks like a function call.
   206         # This looks like a function call.
   207         if t.type == token.NAME and _isop(i + 1, '('):
   207         if t.type == token.NAME and _isop(i + 1, '('):
   208             fn = t.string
   208             fn = t.string
   209 
   209 
   210             # *attr() builtins don't accept byte strings to 2nd argument.
   210             # *attr() builtins don't accept byte strings to 2nd argument.
   211             if (fn in ('getattr', 'setattr', 'hasattr', 'safehasattr') and
   211             if fn in (
   212                     not _isop(i - 1, '.')):
   212                 'getattr', 'setattr', 'hasattr', 'safehasattr', 'wrapfunction',
       
   213                 'wrapclass', 'addattr'
       
   214             ) and (opts['allow-attr-methods'] or not _isop(i - 1, '.')):
   213                 arg1idx = _findargnofcall(1)
   215                 arg1idx = _findargnofcall(1)
   214                 if arg1idx is not None:
   216                 if arg1idx is not None:
   215                     _ensuresysstr(arg1idx)
   217                     _ensuresysstr(arg1idx)
   216 
   218 
   217             # .encode() and .decode() on str/bytes/unicode don't accept
   219             # .encode() and .decode() on str/bytes/unicode don't accept
   274     ap = argparse.ArgumentParser()
   276     ap = argparse.ArgumentParser()
   275     ap.add_argument('-i', '--inplace', action='store_true', default=False,
   277     ap.add_argument('-i', '--inplace', action='store_true', default=False,
   276                     help='edit files in place')
   278                     help='edit files in place')
   277     ap.add_argument('--dictiter', action='store_true', default=False,
   279     ap.add_argument('--dictiter', action='store_true', default=False,
   278                     help='rewrite iteritems() and itervalues()'),
   280                     help='rewrite iteritems() and itervalues()'),
       
   281     ap.add_argument('--allow-attr-methods', action='store_true',
       
   282                     default=False,
       
   283                     help='also handle attr*() when they are methods'),
   279     ap.add_argument('--treat-as-kwargs', nargs="+", default=[],
   284     ap.add_argument('--treat-as-kwargs', nargs="+", default=[],
   280                     help="ignore kwargs-like objects"),
   285                     help="ignore kwargs-like objects"),
   281     ap.add_argument('files', metavar='FILE', nargs='+', help='source file')
   286     ap.add_argument('files', metavar='FILE', nargs='+', help='source file')
   282     args = ap.parse_args()
   287     args = ap.parse_args()
   283     opts = {
   288     opts = {
   284         'dictiter': args.dictiter,
   289         'dictiter': args.dictiter,
   285         'treat-as-kwargs': set(args.treat_as_kwargs),
   290         'treat-as-kwargs': set(args.treat_as_kwargs),
       
   291         'allow-attr-methods': args.allow_attr_methods,
   286     }
   292     }
   287     for fname in args.files:
   293     for fname in args.files:
   288         if args.inplace:
   294         if args.inplace:
   289             with editinplace(fname) as fout:
   295             with editinplace(fname) as fout:
   290                 with open(fname, 'rb') as fin:
   296                 with open(fname, 'rb') as fin: