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


Groups > comp.lang.python > #64158

Re: doctests compatibility for python 2 & python 3

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
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; 'example:': 0.03; 'encoding': 0.05; '"""': 0.07; '-*-': 0.07; 'utf-8': 0.07; '"__main__":': 0.09; '__name__': 0.09; 'arguments': 0.09; 'coding:': 0.09; 'doctest': 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; 'python': 0.11; 'def': 0.12; 'docstring': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'unequal': 0.16; 'utf8': 0.16; 'subject:python': 0.16; 'wrote:': 0.18; 'seems': 0.21; '>>>': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'unicode': 0.24; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'leave': 0.29; 'characters': 0.30; 'comparison': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'file': 0.32; 'skip:c 30': 0.32; 'actual': 0.34; 'skip:_ 10': 0.34; 'convert': 0.35; 'equal': 0.35; 'but': 0.35; 'wrong': 0.37; 'turn': 0.37; 'being': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'skip:u 10': 0.60; 'subject: & ': 0.68; 'received:109': 0.72; 'skip:* 70': 0.78; '2.7.': 0.84; 'failures.': 0.84; 'want:': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Robin Becker <robin@reportlab.com>
Subject Re: doctests compatibility for python 2 & python 3
Date Fri, 17 Jan 2014 12:12:35 +0000
References <mailman.5634.1389957389.18130.python-list@python.org> <52d916e4$0$29999$c3e8da3$5496439d@news.astraweb.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 8bit
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 <52d916e4$0$29999$c3e8da3$5496439d@news.astraweb.com>
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.5637.1389960773.18130.python-list@python.org> (permalink)
Lines 61
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1389960773 news.xs4all.nl 2924 [2001:888:2000:d::a6]:38119
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:64158

Show key headers only | View raw


On 17/01/2014 11:41, Steven D'Aprano wrote:
> def func(a):
>      """
>      >>> print(func(u'aaa'))
>      aaa
>      """
>      return a
I think this approach seems to work if I turn the docstring into unicode

def func(a):
	u"""
	>>> print(func(u'aaa\u020b'))
	aaa\u020b
	"""
	return a
def _doctest():
	import doctest
	doctest.testmod()

if __name__ == "__main__":
	_doctest()

If I leave the u off the docstring it goes wrong in python 2.7. I also tried to 
put an encoding onto the file and use the actual utf8 characters ie

# -*- coding: utf-8 -*-
def func(a):
     """
     >>> print(func(u'aaa\u020b'))
     aaaȋ
     """
     return a
def _doctest():
     import doctest
     doctest.testmod()

and that works in python3, but fails in python 2 with this
> (py27) C:\code\hg-repos>python tdt1.py
> C:\python\Lib\doctest.py:1531: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - in
> terpreting them as being unequal
>   if got == want:
> C:\python\Lib\doctest.py:1551: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - in
> terpreting them as being unequal
>   if got == want:
> **********************************************************************
> File "tdt1.py", line 4, in __main__.func
> Failed example:
>     print(func(u'aaa\u020b'))
> Expected:
>     aaaȋ
> Got:
>     aaaȋ
> **********************************************************************
> 1 items had failures:
>    1 of   1 in __main__.func
> ***Test Failed*** 1 failures.


--
Robin Becker

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


Thread

doctests compatibility for python 2 & python 3 Robin Becker <robin@reportlab.com> - 2014-01-17 11:16 +0000
  Re: doctests compatibility for python 2 & python 3 Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-01-17 11:41 +0000
    Re: doctests compatibility for python 2 & python 3 Robin Becker <robin@reportlab.com> - 2014-01-17 12:12 +0000
      Re: doctests compatibility for python 2 & python 3 Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-01-17 15:27 +0000
        Re: doctests compatibility for python 2 & python 3 Robin Becker <robin@reportlab.com> - 2014-01-17 16:17 +0000

csiph-web