Skip to content

Commit d7e6433

Browse files
mjpietersserhiy-storchaka
authored andcommitted
bpo-28598: Support __rmod__ for RHS subclasses of str in % string formatting operations (#51)
When you use `'%s' % SubClassOfStr()`, where `SubClassOfStr.__rmod__` exists, the reverse operation is ignored as normally such string formatting operations use the `PyUnicode_Format()` fast path. This patch tests for subclasses of `str` first and picks the slow path in that case. Patch by Martijn Pieters.
1 parent 2771304 commit d7e6433

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

Lib/test/test_unicode.py

+9
Original file line numberDiff line numberDiff line change
@@ -1448,6 +1448,15 @@ def test_formatting_huge_precision(self):
14481448
with self.assertRaises(ValueError):
14491449
result = format_string % 2.34
14501450

1451+
def test_issue28598_strsubclass_rhs(self):
1452+
# A subclass of str with an __rmod__ method should be able to hook
1453+
# into the % operator
1454+
class SubclassedStr(str):
1455+
def __rmod__(self, other):
1456+
return 'Success, self.__rmod__({!r}) was called'.format(other)
1457+
self.assertEqual('lhs %% %r' % SubclassedStr('rhs'),
1458+
"Success, self.__rmod__('lhs %% %r') was called")
1459+
14511460
@support.cpython_only
14521461
def test_formatting_huge_precision_c_limits(self):
14531462
from _testcapi import INT_MAX

Misc/NEWS

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ What's New in Python 3.7.0 alpha 1?
1010
Core and Builtins
1111
-----------------
1212

13+
- bpo-28598: Support __rmod__ for subclasses of str being called before
14+
str.__mod__. Patch by Martijn Pieters.
15+
1316
- bpo-29607: Fix stack_effect computation for CALL_FUNCTION_EX.
1417
Patch by Matthieu Dartiailh.
1518

@@ -19,6 +22,7 @@ Core and Builtins
1922

2023
- bpo-29347: Fixed possibly dereferencing undefined pointers
2124
when creating weakref objects.
25+
2226
- bpo-29463: Add ``docstring`` field to Module, ClassDef, FunctionDef,
2327
and AsyncFunctionDef ast nodes. docstring is not first stmt in their body
2428
anymore. It affects ``co_firstlineno`` and ``co_lnotab`` of code object

Python/ceval.c

+9-3
Original file line numberDiff line numberDiff line change
@@ -1354,9 +1354,15 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
13541354
TARGET(BINARY_MODULO) {
13551355
PyObject *divisor = POP();
13561356
PyObject *dividend = TOP();
1357-
PyObject *res = PyUnicode_CheckExact(dividend) ?
1358-
PyUnicode_Format(dividend, divisor) :
1359-
PyNumber_Remainder(dividend, divisor);
1357+
PyObject *res;
1358+
if (PyUnicode_CheckExact(dividend) && (
1359+
!PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1360+
// fast path; string formatting, but not if the RHS is a str subclass
1361+
// (see issue28598)
1362+
res = PyUnicode_Format(dividend, divisor);
1363+
} else {
1364+
res = PyNumber_Remainder(dividend, divisor);
1365+
}
13601366
Py_DECREF(divisor);
13611367
Py_DECREF(dividend);
13621368
SET_TOP(res);

0 commit comments

Comments
 (0)