Message261239
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? | |
| Date | User | Action | Args | | 2016-03-06 08:14:14 | rhettinger | set | recipients: + rhettinger, serhiy.storchaka, xiang.zhang, jcgoble3 | | 2016-03-06 08:14:13 | rhettinger | set | messageid: <1457252053.99.0.769448082282.issue25652@psf.upfronthosting.co.za> | | 2016-03-06 08:14:13 | rhettinger | link | issue25652 messages | | 2016-03-06 08:14:13 | rhettinger | create | | |