Skip to content

Commit 7abf8c6

Browse files
isidenticalmiss-islington
authored andcommitted
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
1 parent 565b4f1 commit 7abf8c6

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

Lib/collections/__init__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,9 +1214,8 @@ def __mul__(self, n):
12141214
__rmul__ = __mul__
12151215
def __mod__(self, args):
12161216
return self.__class__(self.data % args)
1217-
def __rmod__(self, format):
1218-
return self.__class__(format % args)
1219-
1217+
def __rmod__(self, template):
1218+
return self.__class__(str(template) % self)
12201219
# the following methods are defined in alphabetical order:
12211220
def capitalize(self): return self.__class__(self.data.capitalize())
12221221
def casefold(self):

Lib/test/test_userstring.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@ def checkcall(self, object, methodname, *args):
3939
# we don't fix the arguments, because UserString can't cope with it
4040
getattr(object, methodname)(*args)
4141

42+
def test_rmod(self):
43+
class ustr2(UserString):
44+
pass
45+
46+
class ustr3(ustr2):
47+
def __rmod__(self, other):
48+
return super().__rmod__(other)
49+
50+
fmt2 = ustr2('value is %s')
51+
str3 = ustr3('TEST')
52+
self.assertEqual(fmt2 % str3, 'value is TEST')
53+
4254

4355
if __name__ == "__main__":
4456
unittest.main()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix bug in ``__rmod__`` of ``UserString`` - by Batuhan Taskaya.

0 commit comments

Comments
 (0)