diff -r cf70f030a744 Lib/doctest.py --- a/Lib/doctest.py Wed Jun 18 23:07:46 2014 -0400 +++ b/Lib/doctest.py Mon Jun 23 22:57:37 2014 +0300 @@ -101,6 +101,14 @@ from StringIO import StringIO from collections import namedtuple +try: + _unicode = unicode +except NameError: + # If Python is built without Unicode support, the unicode type + # will not exist. Fake one. + class _unicode(object): + pass + TestResults = namedtuple('TestResults', 'failed attempted') # There are 4 basic classes: @@ -199,7 +207,7 @@ """ if inspect.ismodule(module): return module - elif isinstance(module, (str, unicode)): + elif isinstance(module, basestring): return __import__(module, globals(), locals(), ["*"]) elif module is None: return sys.modules[sys._getframe(depth).f_globals['__name__']] @@ -229,7 +237,7 @@ If the string `s` is Unicode, it is encoded using the stdout encoding and the `backslashreplace` error handler. """ - if isinstance(s, unicode): + if isinstance(s, _unicode): s = s.encode(_encoding, 'backslashreplace') # This regexp matches the start of non-blank lines: return re.sub('(?m)^(?!$)', indent*' ', s) @@ -1395,7 +1403,7 @@ if m and m.group('name') == self.test.name: example = self.test.examples[int(m.group('examplenum'))] source = example.source - if isinstance(source, unicode): + if isinstance(source, _unicode): source = source.encode('ascii', 'backslashreplace') return source.splitlines(True) else: diff -r cf70f030a744 Lib/test/test_doctest.py --- a/Lib/test/test_doctest.py Wed Jun 18 23:07:46 2014 -0400 +++ b/Lib/test/test_doctest.py Mon Jun 23 22:57:37 2014 +0300 @@ -7,6 +7,8 @@ from test import test_support import doctest +unicode_doctest = {'unicode': '' if test_support.have_unicode else '+SKIP'} + # NOTE: There are some additional tests relating to interaction with # zipimport in the test_zipimport_support test module. @@ -432,7 +434,7 @@ >>> tests = finder.find(sample_func) >>> print tests # doctest: +ELLIPSIS - [] + [] The exact name depends on how test_doctest was invoked, so allow for leading path components. @@ -1699,11 +1701,12 @@ """ + @test_support.requires_unicode def test_unicode_output(self): r""" Check that unicode output works: - >>> u'\xe9' + >>> u'\xe9' # doctest: %(unicode)s u'\xe9' If we return unicode, SpoofOut's buf variable becomes automagically @@ -1713,15 +1716,16 @@ tests would fail if unicode has been output previously in the testrun. This test tests that this is no longer so: - >>> print u'abc' + >>> print u'abc' # doctest: %(unicode)s abc And then return a string with non-ascii characters: - >>> print u'\xe9'.encode('utf-8') + >>> print u'\xe9'.encode('utf-8') # doctest: %(unicode)s é """ + test_unicode_output.__doc__ %= unicode_doctest def test_testsource(): r""" @@ -2289,11 +2293,12 @@ >>> suite = doctest.DocFileSuite('test_doctest.txt', ... 'test_doctest2.txt', ... 'test_doctest4.txt', - ... encoding='utf-8') - >>> suite.run(unittest.TestResult()) + ... encoding='utf-8') # doctest: %(unicode)s + >>> suite.run(unittest.TestResult()) # doctest: %(unicode)s """ +test_DocFileSuite.__doc__ %= unicode_doctest def test_trailing_space_in_test(): """ @@ -2511,7 +2516,7 @@ it's unknown which encoding is used. The encoding can be specified using the optional keyword argument `encoding`: - >>> doctest.testfile('test_doctest4.txt') # doctest: +ELLIPSIS + >>> doctest.testfile('test_doctest4.txt') # doctest: +ELLIPSIS %(unicode)s ********************************************************************** File "...", line 7, in test_doctest4.txt Failed example: @@ -2529,7 +2534,7 @@ TestResults(failed=2, attempted=4) >>> doctest.master = None # Reset master. - >>> doctest.testfile('test_doctest4.txt', encoding='utf-8') + >>> doctest.testfile('test_doctest4.txt', encoding='utf-8') # doctest: %(unicode)s TestResults(failed=0, attempted=4) >>> doctest.master = None # Reset master. @@ -2537,7 +2542,7 @@ bothering with the current sys.stdout encoding. >>> doctest._encoding, saved_encoding = 'utf-8', doctest._encoding - >>> doctest.testfile('test_doctest4.txt', encoding='utf-8', verbose=True) + >>> doctest.testfile('test_doctest4.txt', encoding='utf-8', verbose=True) # doctest: %(unicode)s Trying: u'föö' Expecting: @@ -2568,6 +2573,7 @@ >>> doctest.master = None # Reset master. >>> sys.argv = save_argv """ +test_testfile.__doc__ %= unicode_doctest # old_test1, ... used to live in doctest.py, but cluttered it. Note # that these use the deprecated doctest.Tester, so should go away (or