doctests compatibility for python 2 & python 3
Chris Angelico
rosuav at gmail.com
Fri Jan 17 06:24:18 EST 2014
On Fri, Jan 17, 2014 at 10:16 PM, Robin Becker <robin at reportlab.com> wrote:
> Aside from changing the tests so they look like
> """
> >>> func(u'aaa')==u'aaa'
> True
> """
Do your test strings contain any non-ASCII characters? If not, you
might be able to do this:
def func(a):
"""
>>> str(func(u'aaa'))
'aaa'
"""
return a
which should work in both. In Py3, the str() call will do nothing, and
it'll compare correctly; in Py2, it'll convert it into a byte string,
which will repr() without the 'u'.
I don't think it's possible to monkey-patch unicode.__repr__ to not
put the u'' prefix on, but if there is a way, that'd possibly be more
convenient.
ChrisA
More information about the Python-list
mailing list