Ticket #2283 (closed defect: fixed)

Opened 5 years ago

Last modified 5 years ago

[with patch, positive review] the coercion code (in __mul__) should call __rmul__ when left or right is not coercible to a Sage element

Reported by: was Owned by: robertwb
Priority: major Milestone: sage-2.10.3
Component: coercion Keywords:
Cc: Work issues:
Report Upstream: Reviewers:
Authors: Merged in:
Dependencies: Stopgaps:

Description

In this example the last print statement goes boom, but should work fine.

class Foo:
   def __rmul__(self, left):
      return 'hello'

H = Foo()
print int(3)*H
print 3*H

Attachments

coercion-rmul.patch Download (728 bytes) - added by jason 5 years ago.
coercion-rmul2.patch Download (1.5 KB) - added by jason 5 years ago.

Change History

Changed 5 years ago by jason

comment:1 Changed 5 years ago by jason

  • Summary changed from the coercion code (in __mul__) should call __rmul__ when left or right is not coercible to a Sage element to [with patch, needs review] the coercion code (in __mul__) should call __rmul__ when left or right is not coercible to a Sage element

comment:2 Changed 5 years ago by jason

Currently, (before this patch), if a class did

_r_action = __rmul__

things would work since the coercion model looks for an _r_action function as a last resort. This patch just makes this line unnecessary by having the coercion system also look for an rmul function (which is standard python; see  http://docs.python.org/ref/numeric-types.html)

Apparently this patch is controversial to at least one person, so it probably ought to be discussed.

comment:3 Changed 5 years ago by jason

disclaimer: I don't know much at all about the coercion system; the above statements are from observations made in running examples.

Changed 5 years ago by jason

comment:4 Changed 5 years ago by jason

credit goes to gfurnish for noticing and helping track down the segfault that the original patch introduced!

Apply coercion-rmul2.patch instead of coercion-rmul.patch

comment:5 Changed 5 years ago by jason

(and gfurnish also knew how to fix the error causing the segfault!)

comment:6 Changed 5 years ago by jason

The patches above apply to 2.10.2.

comment:7 Changed 5 years ago by was

  • Summary changed from [with patch, needs review] the coercion code (in __mul__) should call __rmul__ when left or right is not coercible to a Sage element to [with patch, positive review] the coercion code (in __mul__) should call __rmul__ when left or right is not coercible to a Sage element

Looks good to me. Thanks guys!

comment:8 Changed 5 years ago by mabshoff

  • Status changed from new to closed
  • Resolution set to fixed

Merged coercion-rmul2.patch in Sage 2.10.3.rc0

Note: See TracTickets for help on using tickets.