From 9097de9e62f003bd038e4da6b36823a2d0b9853a Mon Sep 17 00:00:00 2001 From: Martijn Pieters Date: Wed, 1 Mar 2017 15:38:42 +0000 Subject: [PATCH 1/2] [backport to 2.7] bpo-28598: Support __rmod__ for RHS subclasses of str in % string formatting operations --- Lib/test/test_str.py | 9 +++++++++ Python/ceval.c | 8 ++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_str.py b/Lib/test/test_str.py index 5bb9f4867bcb0e..71061973bbd14d 100644 --- a/Lib/test/test_str.py +++ b/Lib/test/test_str.py @@ -38,6 +38,15 @@ def test_formatting(self): string_tests.MixinStrUnicodeUserStringTest.test_formatting(self) self.assertRaises(OverflowError, '%c'.__mod__, 0x1234) + 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") + @test_support.cpython_only def test_formatting_huge_precision(self): from _testcapi import INT_MAX diff --git a/Python/ceval.c b/Python/ceval.c index 2af78ff8747411..68f2a398acece4 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -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); From ee2754cb7dffc52e34c42eacb7cae495e166982e Mon Sep 17 00:00:00 2001 From: Martijn Pieters Date: Wed, 1 Mar 2017 16:44:46 +0000 Subject: [PATCH 2/2] Record change in NEWS --- Misc/NEWS | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Misc/NEWS b/Misc/NEWS index 4523df5a936155..868b2a2b995af1 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -10,6 +10,9 @@ What's New in Python 2.7.14? Core and Builtins ----------------- +- Issue #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. pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy