Patchwork [4,of,6] byteify-strings: broaden handling of attr* functions

login
register
mail settings
Submitter Raphaël Gomès
Date Aug. 2, 2019, 8:22 a.m.
Message ID <edff9054a1a327d3d444.1564734135@alphare-carbon.lan>
Download mbox | patch
Permalink /patch/41123/
State Accepted
Headers show

Comments

Raphaël Gomès - Aug. 2, 2019, 8:22 a.m.
# HG changeset patch
# User Raphaël Gomès <rgomes@octobus.net>
# Date 1564733030 -7200
#      Fri Aug 02 10:03:50 2019 +0200
# Node ID edff9054a1a327d3d444d4acfde0879a92742549
# Parent  2ba0327a754304b371c06d84a80ab1871e02fa66
# EXP-Topic byteify-strings
byteify-strings: broaden handling of attr* functions

The current safeguard that makes sure that `getattr`, etc. is not a method is
too restrictive as it is easy to ignore wrappers use as `util.getattr`.

This change also adds other "good candidates": `wrapfunction`, `wrapclass`
and `addattr`.
Yuya Nishihara - Aug. 2, 2019, 1:15 p.m.
On Fri, 02 Aug 2019 10:22:15 +0200, Raphaël Gomès wrote:
> # HG changeset patch
> # User Raphaël Gomès <rgomes@octobus.net>
> # Date 1564733030 -7200
> #      Fri Aug 02 10:03:50 2019 +0200
> # Node ID edff9054a1a327d3d444d4acfde0879a92742549
> # Parent  2ba0327a754304b371c06d84a80ab1871e02fa66
> # EXP-Topic byteify-strings
> byteify-strings: broaden handling of attr* functions
> 
> The current safeguard that makes sure that `getattr`, etc. is not a method is
> too restrictive as it is easy to ignore wrappers use as `util.getattr`.
> 
> This change also adds other "good candidates": `wrapfunction`, `wrapclass`
> and `addattr`.
> 
> diff -r 2ba0327a7543 -r edff9054a1a3 contrib/byteify-strings.py
> --- a/contrib/byteify-strings.py	Fri Aug 02 09:55:32 2019 +0200
> +++ b/contrib/byteify-strings.py	Fri Aug 02 10:03:50 2019 +0200
> @@ -178,8 +178,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'
> +            ):

IIRC, Mercurial APIs such as util.safehasattr() are supposed to take bytes.

Patch

diff -r 2ba0327a7543 -r edff9054a1a3 contrib/byteify-strings.py
--- a/contrib/byteify-strings.py	Fri Aug 02 09:55:32 2019 +0200
+++ b/contrib/byteify-strings.py	Fri Aug 02 10:03:50 2019 +0200
@@ -178,8 +178,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'
+            ):
                 arg1idx = _findargnofcall(1)
                 if arg1idx is not None:
                     _ensuresysstr(arg1idx)