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 mjpieters
Recipients mjpieters
Date 2016-11-03.13:36:04
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1478180164.76.0.63576586286.issue28598@psf.upfronthosting.co.za>
In-reply-to
Content
The `BINARY_MODULO` operator hardcodes a test for `PyUnicode`:

        TARGET(BINARY_MODULO) {
            PyObject *divisor = POP();
            PyObject *dividend = TOP();
            PyObject *res = PyUnicode_CheckExact(dividend) ?
                PyUnicode_Format(dividend, divisor) :
                PyNumber_Remainder(dividend, divisor);

This means that a RHS subclass of str can't override the operator:

>>> class Foo(str):
...     def __rmod__(self, other):
...         return self % other
...
>>> "Bar: %s" % Foo("Foo: %s")
'Bar: Foo %s'

The expected output there is "Foo: Bar %s".

This works correctly for `bytes`:

>>> class FooBytes(bytes):
...     def __rmod__(self, other):
...         return self % other
...
>>> b"Bar: %s" % FooBytes(b"Foo: %s")
b'Foo: Bar: %s'

and for all other types where the RHS is a subclass.

Perhaps there should be a test to see if `divisor` is a subclass, and in that case take the slow path?
History
Date User Action Args
2016-11-03 13:36:04mjpieterssetrecipients: + mjpieters
2016-11-03 13:36:04mjpieterssetmessageid: <1478180164.76.0.63576586286.issue28598@psf.upfronthosting.co.za>
2016-11-03 13:36:04mjpieterslinkissue28598 messages
2016-11-03 13:36:04mjpieterscreate