Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #64155

Re: doctests compatibility for python 2 & python 3

References <52D91101.7000202@chamonix.reportlab.co.uk>
Date 2014-01-17 22:24 +1100
Subject Re: doctests compatibility for python 2 & python 3
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.5635.1389957861.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Fri, Jan 17, 2014 at 10:16 PM, Robin Becker <robin@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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: doctests compatibility for python 2 & python 3 Chris Angelico <rosuav@gmail.com> - 2014-01-17 22:24 +1100

csiph-web