Message279993
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? |
|
Date |
User |
Action |
Args |
2016-11-03 13:36:04 | mjpieters | set | recipients:
+ mjpieters |
2016-11-03 13:36:04 | mjpieters | set | messageid: <1478180164.76.0.63576586286.issue28598@psf.upfronthosting.co.za> |
2016-11-03 13:36:04 | mjpieters | link | issue28598 messages |
2016-11-03 13:36:04 | mjpieters | create | |
|