[issue25652] collections.UserString.__rmod__() raises NameError
Raymond Hettinger
report at bugs.python.org
Sun Mar 6 03:14:14 EST 2016
Raymond Hettinger added the comment:
The UserString1 patch is incorrect as it leads to infinite recursion because the __rmod__ operation only gets called when the other argument doesn't have __mod__.
One possible fix is to coerce the template to a regular str:
def __rmod__(self, template):
return self.__class__(str(template) % self)
That would get the following test to pass:
class S:
'strlike class without a __mod__'
def __init__(self, value):
self.value = value
def __str__(self):
return str(self.value)
assert S('say %s') % UserString('hello') == 'say hello'
That said, the goal of UserString is to parallel what a regular string would do. In this case, a TypeError is raised:
>>> print(S('answer is %s') % 'hello')
Traceback (most recent call last):
...
TypeError: unsupported operand type(s) for %: 'S' and 'str'
Serhiy, what do you think should be done, coerce to a str or remove the __rmod__ method entirely?
----------
assignee: rhettinger -> serhiy.storchaka
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue25652>
_______________________________________
More information about the Python-bugs-list
mailing list