Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Trac #14186 coerce_binop errors with keyword arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
lftabera committed Dec 20, 2013
1 parent 3472a85 commit 0cb642b
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion src/sage/structure/element.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -3205,11 +3205,22 @@ cdef class NamedBinopMethod:
sage: from sage.structure.element import NamedBinopMethod
sage: test_func = NamedBinopMethod(lambda x, y, **kwds: (x, y, kwds), '_add_')
sage: class test_class(Rational):
....: def __init__(self,value):
....: self.v = value
....: @NamedBinopMethod
....: def test_add(self, other, keyword='z'):
....: return (self.v, other, keyword)
Calls func directly if the two arguments have the same parent::
sage: test_func(1, 2)
(1, 2, {})
sage: x = test_class(1)
sage: x.test_add(1/2)
(1, 1/2, 'z')
sage: x.test_add(1/2, keyword=3)
(1, 1/2, 3)
Passes through coercion and does a method lookup if the
left operand is not the same::
Expand All @@ -3220,6 +3231,30 @@ cdef class NamedBinopMethod:
(1, 2, {'algorithm': 'fast'})
sage: test_func(1, 1/2)
3/2
sage: x.test_add(2)
(1, 2, 'z')
sage: x.test_add(2, keyword=3)
(1, 2, 3)
A real example::
sage: R1=QQ['x,y']
sage: R2=QQ['x,y,z']
sage: f=R1(1)
sage: g=R1(2)
sage: h=R2(1)
sage: f.gcd(g)
1
sage: f.gcd(g,algorithm='modular')
1
sage: f.gcd(h)
1
sage: f.gcd(h,algorithm='modular')
1
sage: h.gcd(f)
1
sage: h.gcd(f,algorithm='modular')
1
"""
if y is None:
if self._self is None:
Expand All @@ -3230,7 +3265,7 @@ cdef class NamedBinopMethod:
old_x = x
x,y = coercion_model.canonical_coercion(x, y)
if old_x is x:
return self._func(x,y, *kwds)
return self._func(x,y, **kwds)
else:
return getattr(x, self._name)(y, **kwds)
else:
Expand Down

0 comments on commit 0cb642b

Please sign in to comment.