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

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder3.xlned.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; '"""': 0.07; 'prefix': 0.09; 'subject:skip:c 10': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'jan': 0.12; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'non-ascii': 0.16; 'repr()': 0.16; 'str()': 0.16; 'subject:python': 0.16; 'wrote:': 0.18; 'not,': 0.20; '>>>': 0.22; 'tests': 0.22; 'cc:addr:python.org': 0.22; 'byte': 0.24; 'string,': 0.24; 'cc:2**0': 0.24; 'compare': 0.26; 'possibly': 0.26; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'on,': 0.29; 'message-id:@mail.gmail.com': 0.30; 'fri,': 0.33; 'convert': 0.35; 'test': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'possible': 0.36; 'should': 0.36; 'changing': 0.37; 'pm,': 0.38; 'aside': 0.39; 'skip:u 10': 0.60; 'more': 0.64; 'subject: & ': 0.68; 'to:none': 0.92
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=M8cr/n/GDbUCXu+AfkaHx/T0nAqk0ZFdfQj2Ogjj4kc=; b=DblOoGOmt68i55A1Os/ES6iZnTjOY8in4qug7AksTrbcUsWliSkeNV7EhsPU+WA9he QPwXIz78z/xDCX0M1NlomM+5o/0IWg0dgCM2DvF/5OcwgRFdg21P1OuuROdMV0RQc4AS aLJECtbn0lXFIXNce90lGQlenqSNj0H2v0RMQ8Aw+DE8dD4q/Knebuw2HWRBF75wsl31 kl9F19+wSrKVeZH/ixmqXqa3GN8YBJDbflhTNFIcNcoO5II9cxrTkifLIt4CKumQu0Jw oNNLAfznRoxckBIYbA8Xen/h/LxC8K9PuB07KkkEq5pOmgJPT481y3DX68tL29Zbw5Gs Dk6A==
MIME-Version 1.0
X-Received by 10.68.201.10 with SMTP id jw10mr1462695pbc.25.1389957858112; Fri, 17 Jan 2014 03:24:18 -0800 (PST)
In-Reply-To <52D91101.7000202@chamonix.reportlab.co.uk>
References <52D91101.7000202@chamonix.reportlab.co.uk>
Date Fri, 17 Jan 2014 22:24:18 +1100
Subject Re: doctests compatibility for python 2 & python 3
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.5635.1389957861.18130.python-list@python.org> (permalink)
Lines 26
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1389957861 news.xs4all.nl 2914 [2001:888:2000:d::a6]:49785
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:64155

Show key headers only | 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