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 <>
The `BINARY_MODULO` operator hardcodes a test for `PyUnicode`:

            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?
Date User Action Args
2016-11-03 13:36:04mjpieterssetrecipients: + mjpieters
2016-11-03 13:36:04mjpieterssetmessageid: <>
2016-11-03 13:36:04mjpieterslinkissue28598 messages
2016-11-03 13:36:04mjpieterscreate