Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Doctests, Unicode and Python3

Received: by 10.101.152.22 with SMTP id e22mr636091ano.20.1311915080231;
        Thu, 28 Jul 2011 21:51:20 -0700 (PDT)
X-BeenThere: nose-users@googlegroups.com
Received: by 10.91.36.10 with SMTP id o10ls3009644agj.4.gmail; Thu, 28 Jul
 2011 21:51:18 -0700 (PDT)
MIME-Version: 1.0
Received: by 10.91.160.33 with SMTP id m33mr136670ago.3.1311915078484; Thu, 28
 Jul 2011 21:51:18 -0700 (PDT)
Received: by z14g2000yqh.googlegroups.com with HTTP; Thu, 28 Jul 2011 21:51:18
 -0700 (PDT)
Date: Thu, 28 Jul 2011 21:51:18 -0700 (PDT)
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.30
 (KHTML, like Gecko) Chrome/12.0.742.122 Safari/534.30,gzip(gfe)
Message-ID: <bb34066d-9402-4e8b-a92e-d5c92a40e6d4@z14g2000yqh.googlegroups.com>
Subject: Doctests, Unicode and Python3
From: Waylan Limberg <way...@gmail.com>
To: nose-users <nose-users@googlegroups.com>
Content-Type: text/plain; charset=ISO-8859-1

A doctest with unicode output in Python 2 looks like this:

    >>> unicode("foo")
    u"foo"

But in Python 3, all strings are unicode so that little `u` in front
of the expected output causes the test to fail. Additionally,
apparently the 2to3 tool can't (and doesn't) modify the output line.
After some searching I've found that the python devs discussed this
and came to the conclusion found here:

http://www.mail-archive.com/python-...@python.org/msg46171.html

I'm trying to implement something of the sort into nose, but am
running into all sorts of weird problems. I'v tried monkeypatching
`nose.plugins.doctests.DocTestCase._displayhook` but I keep running
into weird errors with my subclass.

So far I have:

    class Py3DocTestCase(nose.plugins.doctests.DocTestCase):
        """ Ensure unicode output in doctests pass in Python 3. """

        def __init__(self, test, optionflags=0, setUp=None,
tearDown=None,
                     checker=None, obj=None, result_var='_'):
            """ Reimplement __init__ to avoid max recursion with
nested super() calls. """
            self._result_var = result_var
            self._nose_obj = obj
            doctest.DocTestCase.__init__(self,
                test, optionflags=optionflags, setUp=setUp,
tearDown=tearDown,
                checker=checker)

        def _displayhook(self, value):
            if value is None:
                return
            setattr(builtin_mod, self._result_var, value)
            s = repr(value)
            if not isinstance(value, unicode):
                # Old behavior
                print s
            else:
                # new behavior
                if s.startswith(("u'", 'u"')):
                    s = s[1:]
                print s

    if sys.version_info >= (3, 0):
        # Monkeypatch Nose
        nose.plugins.doctests.DocTestCase = Py3DocTestCase

That gives me this weird error:

      File "/opt/python3.2/lib/python3.2/site-packages/nose/plugins/
doctests.py", line 374, in shortDescription
        return 'Doctest: %s' % self.id()
      File "/opt/python3.2/lib/python3.2/site-packages/nose/plugins/
doctests.py", line 359, in id
        name = self._dt_test.name
    AttributeError: 'str' object has no attribute 'name'