Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '"""': 0.07; 'versions.': 0.07; 'string': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:skip:c 10': 0.09; 'try:': 0.09; 'python': 0.11; 'def': 0.12; 'jan': 0.12; 'ascii,': 0.16; 'nameerror:': 0.16; 'non-ascii': 0.16; 'outcomes': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'repr': 0.16; 'throw': 0.16; 'subject:python': 0.16; 'wrote:': 0.18; 'do.': 0.18; 'not,': 0.20; '>>>': 0.22; 'appears': 0.22; 'cc:addr:gmail.com': 0.22; 'header:User-Agent:1': 0.23; 'helper': 0.24; 'unicode': 0.24; 'cc:2**0': 0.24; "i've": 0.25; 'this:': 0.26; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'chris': 0.29; "doesn't": 0.30; 'though.': 0.31; 'class': 0.32; 'probably': 0.32; 'text': 0.33; 'fri,': 0.33; 'skip:_ 10': 0.34; 'except': 0.35; 'skip:u 20': 0.35; 'test': 0.35; 'but': 0.35; 'being': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'little': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'more': 0.64; 'subject: & ': 0.68; 'received:109': 0.72; 'actually,': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Robin Becker Subject: Re: doctests compatibility for python 2 & python 3 Date: Fri, 17 Jan 2014 12:14:51 +0000 References: <52D91101.7000202@chamonix.reportlab.co.uk> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: 109.174.168.73 User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:24.0) Gecko/20100101 Thunderbird/24.2.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 43 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1389960907 news.xs4all.nl 2889 [2001:888:2000:d::a6]:40680 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:64159 On 17/01/2014 11:30, Chris Angelico wrote: > On Fri, Jan 17, 2014 at 10:24 PM, Chris Angelico 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