Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions 9 Lib/test/test_str.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,15 @@ def test_startswith_endswith_errors(self):
self.assertIn('str', exc)
self.assertIn('tuple', exc)

def test_issue28598_strsubclass_rhs(self):
# A subclass of str with an __rmod__ method should be able to hook
# into the % operator
class SubclassedStr(str):
def __rmod__(self, other):
return 'Success, self.__rmod__({!r}) was called'.format(other)
self.assertEqual('lhs %% %r' % SubclassedStr('rhs'),
"Success, self.__rmod__('lhs %% %r') was called")

def test_main():
test_support.run_unittest(StrTest)

Expand Down
3 changes: 3 additions & 0 deletions 3 Misc/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ What's New in Python 2.7.14?
Core and Builtins
-----------------

- bpo-28598: Support __rmod__ for subclasses of str being called before
str.__mod__. Patch by Martijn Pieters.

- bpo-29602: Fix incorrect handling of signed zeros in complex constructor for
complex subclasses and for inputs having a __complex__ method. Patch
by Serhiy Storchaka.
Expand Down
8 changes: 6 additions & 2 deletions 8 Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -1446,10 +1446,14 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
{
w = POP();
v = TOP();
if (PyString_CheckExact(v))
if (PyString_CheckExact(v)
&& (!PyString_Check(w) || PyString_CheckExact(w))) {
/* fast path; string formatting, but not if the RHS is a str subclass
(see issue28598) */
x = PyString_Format(v, w);
else
} else {
x = PyNumber_Remainder(v, w);
}
Py_DECREF(v);
Py_DECREF(w);
SET_TOP(x);
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.