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


Groups > comp.lang.python > #64159

Re: doctests compatibility for python 2 & python 3

From Robin Becker <robin@reportlab.com>
Subject Re: doctests compatibility for python 2 & python 3
Date 2014-01-17 12:14 +0000
References <52D91101.7000202@chamonix.reportlab.co.uk> <CAPTjJmqhr9V42ncZHKKGnE=N8KuUmV2t6M3eNYfZzkPNTYQF9A@mail.gmail.com> <CAPTjJmo6XqfZL5xYB-2pyhv+fsmB-faPrLK3y8-dcqRQFEyC9A@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.5638.1389960907.18130.python-list@python.org> (permalink)

Show all headers | View raw


On 17/01/2014 11:30, Chris Angelico wrote:
> On Fri, Jan 17, 2014 at 10:24 PM, Chris Angelico <rosuav@gmail.com> wrote:
>> 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
>
> Actually, probably better than that:
>
> def func(a):
>      """
>      >>> text(func(u'aaa'))
>      'aaa'
>      """
>      return a
>
> try:
>      class text(unicode): # Will throw NameError in Py3
>          def __repr__(self):
>              return unicode.__repr__(self)[1:]
> except NameError:
>      # Python 3 doesn't need this wrapper.
>      text = str
>
> Little helper class that does what I don't think monkey-patching will
> do. I've tested this and it appears to work in both 2.7.4 and 3.4.0b2,
> but that doesn't necessarily mean that the repr of any given Unicode
> string will be exactly the same on both versions. It's likely to be
> better than depending on the strings being ASCII, though.
>
> ChrisA
>

I tried this approach with a few more complicated outcomes and they fail in 
python2 or 3 depending on how I try to render the result in the doctest.
-- 
Robin Becker

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


Thread

Re: doctests compatibility for python 2 & python 3 Robin Becker <robin@reportlab.com> - 2014-01-17 12:14 +0000

csiph-web