[Patches] [ python-Patches-592646 ] Prefer nb_multipy over sq_repeat

noreply@sourceforge.net noreply@sourceforge.net
Thu, 08 Aug 2002 12:54:56 -0700


Patches item #592646, was opened at 2002-08-08 15:35
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=592646&group_id=5470

Category: Core (C code)
Group: None
Status: Open
Resolution: Accepted
Priority: 5
Submitted By: Neil Schemenauer (nascheme)
Assigned to: Guido van Rossum (gvanrossum)
Summary: Prefer nb_multipy over sq_repeat

Initial Comment:
>From David Beazlely:
==============================================
class Foo(object):
    def __mul__(self,other):
        print "__mul__"
    def __rmul__(self,other):
        print "__rmul__"

Python-2.2.1, if you try this, you get the following
behavior:

>>> f = Foo()
>>> f*1.0
__mul__
>>> 1.0*f
__rmul__
>>> f*1
__mul__
>>> 1*f
__mul__
==============================================

This is because the int object prefers calling
sq_repeat over
nb_multiply if a type defines both.  The attached patch
changes
int_mul to prefer nb_multiply over sq_repeat.

This is a change is behavior and requires some careful
thought.  The
tests run okay with this change but that doesn't mean
other code
will not break.

----------------------------------------------------------------------

>Comment By: Neil Schemenauer (nascheme)
Date: 2002-08-08 19:54

Message:
Logged In: YES 
user_id=35752

Argh!  Stupid typos.

# without patch
1*a # prints __mul__
1.0*a # raises TypeError

Extension classes do not define __r* methods.

----------------------------------------------------------------------

Comment By: Neil Schemenauer (nascheme)
Date: 2002-08-08 19:52

Message:
Logged In: YES 
user_id=35752

This affects extension classes:

import ExtensionClass
class A(ExtensionClass.Base):
  def __mul__(self, other):
    print '__mul__'
    return 1
a = A()

# without patch
a*1 # prints __mul__
a*1.0 # prints __mul__
1.0*a # prints __mul__
1.0*a # raises TypeError

# with patch
a*1 # prints __mul__
a*1.0 # prints __mul__
1*a # raises TypeError
1.0*a # raises TypeError

The new behavior is more consistent.  Do you still want
it in 2.2?

----------------------------------------------------------------------

Comment By: Guido van Rossum (gvanrossum)
Date: 2002-08-08 18:21

Message:
Logged In: YES 
user_id=6380

Looks good to me, but s/of/if/ in the comment. :-)

Despite the semantic change I think this should be
backported to 2.2; it's more a bug than a feature...

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=592646&group_id=5470