Index: Lib/doctest.py =================================================================== --- Lib/doctest.py (revision 78464) +++ Lib/doctest.py (working copy) @@ -218,11 +218,16 @@ return file_contents.replace(os.linesep, '\n'), filename return open(filename).read(), filename +_encoding = getattr(sys.stdout, 'encoding', None) or 'utf-8' + def _indent(s, indent=4): """ Add the given number of space characters to the beginning every non-blank line in `s`, and return the result. """ + # Coerce all Unicode strings to bytestrings for output + if isinstance(s, unicode): + s = s.encode(_encoding, 'backslashreplace') # This regexp matches the start of non-blank lines: return re.sub('(?m)^(?!$)', indent*' ', s) Index: Lib/test/test_doctest.py =================================================================== --- Lib/test/test_doctest.py (revision 78464) +++ Lib/test/test_doctest.py (working copy) @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- """ Test script for doctest. """ @@ -372,7 +373,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. @@ -2284,6 +2285,40 @@ >>> doctest.testfile('test_doctest4.txt', encoding='utf-8') TestResults(failed=0, attempted=4) >>> doctest.master = None # Reset master. + +Switch the module encoding to 'utf-8' to test the verbose output without +bothering with the current sys.stdout encoding. + + >>> saved_encoding, doctest._encoding = doctest._encoding, 'utf-8' + >>> doctest.testfile('test_doctest4.txt', encoding='utf-8', verbose=True) + Trying: + u'föö' + Expecting: + u'f\xf6\xf6' + ok + Trying: + u'bąr' + Expecting: + u'b\u0105r' + ok + Trying: + 'föö' + Expecting: + 'f\xc3\xb6\xc3\xb6' + ok + Trying: + 'bąr' + Expecting: + 'b\xc4\x85r' + ok + 1 items passed all tests: + 4 tests in test_doctest4.txt + 4 tests in 1 items. + 4 passed and 0 failed. + Test passed. + TestResults(failed=0, attempted=4) + >>> doctest._encoding = saved_encoding + >>> doctest.master = None # Reset master. """ # old_test1, ... used to live in doctest.py, but cluttered it. Note @@ -2419,9 +2454,25 @@ def test_main(): # Check the doctest cases in doctest itself: test_support.run_doctest(doctest, verbosity=True) - # Check the doctest cases defined here: + from test import test_doctest - test_support.run_doctest(test_doctest, verbosity=True) + with warnings.catch_warnings(): + # Silence Py3k warning + warnings.filterwarnings("ignore", "backquote not supported", + SyntaxWarning) + warnings.filterwarnings("ignore", "execfile.. not supported", + DeprecationWarning) + # Save the real sys.argv list. + save_argv = sys.argv + if '-v' in sys.argv: + # Remove '-v' before running tests + sys.argv = [arg for arg in save_argv if arg != '-v'] + try: + # Check the doctest cases defined here: + test_support.run_doctest(test_doctest, verbosity=True) + finally: + # Restore sys.argv + sys.argv = save_argv import trace, sys def test_coverage(coverdir):