[Python-checkins] bpo-25652: Fix __rmod__ of UserString (GH-13326)

Miss Islington (bot) webhook-mailer at python.org
Tue May 21 16:27:43 EDT 2019


https://github.com/python/cpython/commit/7abf8c60819d5749e6225b371df51a9c5f1ea8e9
commit: 7abf8c60819d5749e6225b371df51a9c5f1ea8e9
branch: master
author: Batuhan Taşkaya <47358913+isidentical at users.noreply.github.com>
committer: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
date: 2019-05-21T13:27:36-07:00
summary:

bpo-25652: Fix __rmod__ of UserString (GH-13326)



The ``__rmod__`` method of ``collections.UserString`` class had a bug that made it unusable. 


https://bugs.python.org/issue25652

files:
A Misc/NEWS.d/next/Library/2019-05-14-21-39-52.bpo-25652.xLw42k.rst
M Lib/collections/__init__.py
M Lib/test/test_userstring.py

diff --git a/Lib/collections/__init__.py b/Lib/collections/__init__.py
index 960d82a5dcfb..cb7f1bb1fcfe 100644
--- a/Lib/collections/__init__.py
+++ b/Lib/collections/__init__.py
@@ -1214,9 +1214,8 @@ def __mul__(self, n):
     __rmul__ = __mul__
     def __mod__(self, args):
         return self.__class__(self.data % args)
-    def __rmod__(self, format):
-        return self.__class__(format % args)
-
+    def __rmod__(self, template):
+        return self.__class__(str(template) % self)
     # the following methods are defined in alphabetical order:
     def capitalize(self): return self.__class__(self.data.capitalize())
     def casefold(self):
diff --git a/Lib/test/test_userstring.py b/Lib/test/test_userstring.py
index 71528223d35b..19b0acfc760f 100644
--- a/Lib/test/test_userstring.py
+++ b/Lib/test/test_userstring.py
@@ -39,6 +39,18 @@ def checkcall(self, object, methodname, *args):
         # we don't fix the arguments, because UserString can't cope with it
         getattr(object, methodname)(*args)
 
+    def test_rmod(self):
+        class ustr2(UserString):
+            pass
+
+        class ustr3(ustr2):
+            def __rmod__(self, other):
+                return super().__rmod__(other)
+
+        fmt2 = ustr2('value is %s')
+        str3 = ustr3('TEST')
+        self.assertEqual(fmt2 % str3, 'value is TEST')
+
 
 if __name__ == "__main__":
     unittest.main()
diff --git a/Misc/NEWS.d/next/Library/2019-05-14-21-39-52.bpo-25652.xLw42k.rst b/Misc/NEWS.d/next/Library/2019-05-14-21-39-52.bpo-25652.xLw42k.rst
new file mode 100644
index 000000000000..421fccfe8c73
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2019-05-14-21-39-52.bpo-25652.xLw42k.rst
@@ -0,0 +1 @@
+Fix bug in ``__rmod__`` of ``UserString`` - by Batuhan Taskaya.



More information about the Python-checkins mailing list