This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author rhettinger
Recipients jcgoble3, rhettinger, serhiy.storchaka, xiang.zhang
Date 2016-03-06.08:14:13
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1457252053.99.0.769448082282.issue25652@psf.upfronthosting.co.za>
In-reply-to
Content
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?
History
Date User Action Args
2016-03-06 08:14:14rhettingersetrecipients: + rhettinger, serhiy.storchaka, xiang.zhang, jcgoble3
2016-03-06 08:14:13rhettingersetmessageid: <1457252053.99.0.769448082282.issue25652@psf.upfronthosting.co.za>
2016-03-06 08:14:13rhettingerlinkissue25652 messages
2016-03-06 08:14:13rhettingercreate