Patchwork [3,of,8] py3: handle sysstr conversion around get/set attr in contrib/perf

login
register
mail settings
Submitter Matt Harbison
Date Sept. 22, 2018, 3:28 p.m.
Message ID <c0c842de276702455060.1537630119@Envy>
Download mbox | patch
Permalink /patch/34918/
State Accepted
Headers show

Comments

Matt Harbison - Sept. 22, 2018, 3:28 p.m.
# HG changeset patch
# User Matt Harbison <matt_harbison@yahoo.com>
# Date 1537575036 14400
#      Fri Sep 21 20:10:36 2018 -0400
# Node ID c0c842de276702455060c3d5fd3a6aa80185cdc6
# Parent  0d1b3d5a92e159506c823be481cf9f57e45ec03d
py3: handle sysstr conversion around get/set attr in contrib/perf
Yuya Nishihara - Sept. 23, 2018, 6:42 a.m.
On Sat, 22 Sep 2018 11:28:39 -0400, Matt Harbison wrote:
> # HG changeset patch
> # User Matt Harbison <matt_harbison@yahoo.com>
> # Date 1537575036 14400
> #      Fri Sep 21 20:10:36 2018 -0400
> # Node ID c0c842de276702455060c3d5fd3a6aa80185cdc6
> # Parent  0d1b3d5a92e159506c823be481cf9f57e45ec03d
> py3: handle sysstr conversion around get/set attr in contrib/perf
> 
> diff --git a/contrib/perf.py b/contrib/perf.py
> --- a/contrib/perf.py
> +++ b/contrib/perf.py
> @@ -95,7 +95,7 @@ except (AttributeError, ImportError):
>  # available since 1.9.3 (or 94b200a11cf7)
>  _undefined = object()
>  def safehasattr(thing, attr):
> -    return getattr(thing, attr, _undefined) is not _undefined
> +    return getattr(thing, pycompat.sysstr(attr), _undefined) is not _undefined
>  setattr(util, 'safehasattr', safehasattr)
>  
>  # for "historical portability":
> @@ -340,12 +340,12 @@ def safeattrsetter(obj, name, ignoremiss
>          raise error.Abort((b"missing attribute %s of %s might break assumption"
>                             b" of performance measurement") % (name, obj))
>  
> -    origvalue = getattr(obj, name)
> +    origvalue = getattr(obj, pycompat.sysstr(name))
>      class attrutil(object):
>          def set(self, newvalue):
> -            setattr(obj, name, newvalue)
> +            setattr(obj, pycompat.sysstr(name), newvalue)
>          def restore(self):
> -            setattr(obj, name, origvalue)
> +            setattr(obj, pycompat.sysstr(name), origvalue)

Perhaps another hasattr() would be needed since perf.py has to support
old Mercurial codebase where pycompat doesn't exist. The same thing applies
to the other patches.
Matt Harbison - Sept. 23, 2018, 1:48 p.m.
> On Sep 23, 2018, at 2:42 AM, Yuya Nishihara <yuya@tcha.org> wrote:
> 
>> On Sat, 22 Sep 2018 11:28:39 -0400, Matt Harbison wrote:
>> # HG changeset patch
>> # User Matt Harbison <matt_harbison@yahoo.com>
>> # Date 1537575036 14400
>> #      Fri Sep 21 20:10:36 2018 -0400
>> # Node ID c0c842de276702455060c3d5fd3a6aa80185cdc6
>> # Parent  0d1b3d5a92e159506c823be481cf9f57e45ec03d
>> py3: handle sysstr conversion around get/set attr in contrib/perf
>> 
>> diff --git a/contrib/perf.py b/contrib/perf.py
>> --- a/contrib/perf.py
>> +++ b/contrib/perf.py
>> @@ -95,7 +95,7 @@ except (AttributeError, ImportError):
>> # available since 1.9.3 (or 94b200a11cf7)
>> _undefined = object()
>> def safehasattr(thing, attr):
>> -    return getattr(thing, attr, _undefined) is not _undefined
>> +    return getattr(thing, pycompat.sysstr(attr), _undefined) is not _undefined
>> setattr(util, 'safehasattr', safehasattr)
>> 
>> # for "historical portability":
>> @@ -340,12 +340,12 @@ def safeattrsetter(obj, name, ignoremiss
>>         raise error.Abort((b"missing attribute %s of %s might break assumption"
>>                            b" of performance measurement") % (name, obj))
>> 
>> -    origvalue = getattr(obj, name)
>> +    origvalue = getattr(obj, pycompat.sysstr(name))
>>     class attrutil(object):
>>         def set(self, newvalue):
>> -            setattr(obj, name, newvalue)
>> +            setattr(obj, pycompat.sysstr(name), newvalue)
>>         def restore(self):
>> -            setattr(obj, name, origvalue)
>> +            setattr(obj, pycompat.sysstr(name), origvalue)
> 
> Perhaps another hasattr() would be needed since perf.py has to support
> old Mercurial codebase where pycompat doesn't exist. The same thing applies
> to the other patches.

Oops.  I see what you mean about missing pycompat, but don’t understand the hasattr() part.  Wouldn’t you just, say at the top of the module, import pycompat and define local aliases for these things, and then in the ImportError handler, define legacy aliases?
Yuya Nishihara - Sept. 23, 2018, 2:13 p.m.
On Sun, 23 Sep 2018 09:48:38 -0400, Matt Harbison wrote:
> 
> > On Sep 23, 2018, at 2:42 AM, Yuya Nishihara <yuya@tcha.org> wrote:
> > 
> >> On Sat, 22 Sep 2018 11:28:39 -0400, Matt Harbison wrote:
> >> # HG changeset patch
> >> # User Matt Harbison <matt_harbison@yahoo.com>
> >> # Date 1537575036 14400
> >> #      Fri Sep 21 20:10:36 2018 -0400
> >> # Node ID c0c842de276702455060c3d5fd3a6aa80185cdc6
> >> # Parent  0d1b3d5a92e159506c823be481cf9f57e45ec03d
> >> py3: handle sysstr conversion around get/set attr in contrib/perf
> >> 
> >> diff --git a/contrib/perf.py b/contrib/perf.py
> >> --- a/contrib/perf.py
> >> +++ b/contrib/perf.py
> >> @@ -95,7 +95,7 @@ except (AttributeError, ImportError):
> >> # available since 1.9.3 (or 94b200a11cf7)
> >> _undefined = object()
> >> def safehasattr(thing, attr):
> >> -    return getattr(thing, attr, _undefined) is not _undefined
> >> +    return getattr(thing, pycompat.sysstr(attr), _undefined) is not _undefined
> >> setattr(util, 'safehasattr', safehasattr)
> >> 
> >> # for "historical portability":
> >> @@ -340,12 +340,12 @@ def safeattrsetter(obj, name, ignoremiss
> >>         raise error.Abort((b"missing attribute %s of %s might break assumption"
> >>                            b" of performance measurement") % (name, obj))
> >> 
> >> -    origvalue = getattr(obj, name)
> >> +    origvalue = getattr(obj, pycompat.sysstr(name))
> >>     class attrutil(object):
> >>         def set(self, newvalue):
> >> -            setattr(obj, name, newvalue)
> >> +            setattr(obj, pycompat.sysstr(name), newvalue)
> >>         def restore(self):
> >> -            setattr(obj, name, origvalue)
> >> +            setattr(obj, pycompat.sysstr(name), origvalue)
> > 
> > Perhaps another hasattr() would be needed since perf.py has to support
> > old Mercurial codebase where pycompat doesn't exist. The same thing applies
> > to the other patches.
> 
> Oops.  I see what you mean about missing pycompat, but don’t understand the hasattr() part.  Wouldn’t you just, say at the top of the module, import pycompat and define local aliases for these things,

Yes, that's what I expected.

> and then in the ImportError handler, define legacy aliases?

plus AttributeError because pycompat.py evolves.

Patch

diff --git a/contrib/perf.py b/contrib/perf.py
--- a/contrib/perf.py
+++ b/contrib/perf.py
@@ -95,7 +95,7 @@  except (AttributeError, ImportError):
 # available since 1.9.3 (or 94b200a11cf7)
 _undefined = object()
 def safehasattr(thing, attr):
-    return getattr(thing, attr, _undefined) is not _undefined
+    return getattr(thing, pycompat.sysstr(attr), _undefined) is not _undefined
 setattr(util, 'safehasattr', safehasattr)
 
 # for "historical portability":
@@ -340,12 +340,12 @@  def safeattrsetter(obj, name, ignoremiss
         raise error.Abort((b"missing attribute %s of %s might break assumption"
                            b" of performance measurement") % (name, obj))
 
-    origvalue = getattr(obj, name)
+    origvalue = getattr(obj, pycompat.sysstr(name))
     class attrutil(object):
         def set(self, newvalue):
-            setattr(obj, name, newvalue)
+            setattr(obj, pycompat.sysstr(name), newvalue)
         def restore(self):
-            setattr(obj, name, origvalue)
+            setattr(obj, pycompat.sysstr(name), origvalue)
 
     return attrutil()