New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RHS not consulted in str % subclass_of_str
case.
#72784
Comments
The 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 >>> 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 |
Here's a proposed patch for tip; what versions would it be worth backporting this to? (Note, there's no NEWS update in this patch). |
I'm not sure if issues are linked automatically yet. I put the patch up as a pull request on GitHub: #51 |
Is 2.7 free from this bug? |
No, 2.7 is affected too: >>> class SubclassedStr(str):
... def __rmod__(self, other):
... return 'Success, self.__rmod__({!r}) was called'.format(other)
...
>>> 'lhs %% %r' % SubclassedStr('rhs')
"lhs % 'rhs'" Expected output is "Success, self.__rmod__('lhs %% %r') was called" On the plus side, unicode is not affected: >>> class SubclassedUnicode(unicode):
... def __rmod__(self, other):
... return u'Success, self.__rmod__({!r}) was called'.format(other)
...
>>> u'lhs %% %r' % SubclassedUnicode(u'rhs')
u"Success, self.__rmod__(u'lhs %% %r') was called" |
Misc/NEWS
so that it is managed by towncrier #552Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: