This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author ShashkovS
Recipients ShashkovS
Date 2015-10-15.14:09:53
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1444918194.04.0.71189202918.issue25412@psf.upfronthosting.co.za>
In-reply-to
Content
__floordiv__ in module fraction fails with TypeError instead of returning NotImplemented when modulo is a class without __rtruediv__ or __mul__.

Code sample:

class Foo(object):
    def __rdivmod__(self, other):
        return 'rdivmod works'

from fractions import Fraction
a = Fraction(1,1)
b = Foo()
print(divmod(1, b))
print(divmod(a, b))



__divmod__ in Fraction is inherited from class Real (numbers.py):
    def __divmod__(self, other):
        return (self // other, self % other)

So __floordiv__ and __mod__ are called. 

    def __floordiv__(a, b):
        """a // b"""
        return math.floor(a / b)

    def __mod__(a, b):
        """a % b"""
        div = a // b
        return a - b * div

__floordiv__ if fractions.py makes a true division, and __mod__ makes multiplication.



The following code will fix the problem:


    def __divmod__(self, other):
        if isinstance(a, numbers.Complex):
            return (self // other, self % other)
        else:
            return NotImplemented
History
Date User Action Args
2015-10-15 14:09:54ShashkovSsetrecipients: + ShashkovS
2015-10-15 14:09:54ShashkovSsetmessageid: <1444918194.04.0.71189202918.issue25412@psf.upfronthosting.co.za>
2015-10-15 14:09:54ShashkovSlinkissue25412 messages
2015-10-15 14:09:53ShashkovScreate