From 9d396399dad743124535dcb28cd073617ad15980 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Sat, 16 Oct 2010 21:54:59 +0000 Subject: Issue #10123: Don't use non-ascii filenames in test_doctest tests. Add a new test specific to unicode (non-ascii name and filename). --- Lib/test/test_doctest.py | 58 ++++++++++++++++++++++++++++++++++++++---------- Misc/NEWS | 6 +++++ 2 files changed, 52 insertions(+), 12 deletions(-) diff --git a/Lib/test/test_doctest.py b/Lib/test/test_doctest.py index a4b7e35..484a9b8 100644 --- a/Lib/test/test_doctest.py +++ b/Lib/test/test_doctest.py @@ -4,6 +4,7 @@ Test script for doctest. from test import support import doctest +import os # NOTE: There are some additional tests relating to interaction with @@ -372,7 +373,7 @@ We'll simulate a __file__ attr that ends in pyc: >>> 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. @@ -1703,7 +1704,7 @@ def test_pdb_set_trace(): ... >>> import pdb; pdb.set_trace() ... ''' >>> parser = doctest.DocTestParser() - >>> test = parser.get_doctest(doc, {}, "foo-bär@baz", "foo-bär@baz.py", 0) + >>> test = parser.get_doctest(doc, {}, "foo-bar@baz", "foo-bar@baz.py", 0) >>> runner = doctest.DocTestRunner(verbose=False) To demonstrate this, we'll create a fake standard input that @@ -1719,7 +1720,7 @@ def test_pdb_set_trace(): >>> try: runner.run(test) ... finally: sys.stdin = real_stdin --Return-- - > (1)()->None + > (1)()->None -> import pdb; pdb.set_trace() (Pdb) print(x) 42 @@ -1736,7 +1737,7 @@ def test_pdb_set_trace(): ... >>> x=1 ... >>> calls_set_trace() ... ''' - >>> test = parser.get_doctest(doc, globals(), "foo-bär@baz", "foo-bär@baz.py", 0) + >>> test = parser.get_doctest(doc, globals(), "foo-bar@baz", "foo-bar@baz.py", 0) >>> real_stdin = sys.stdin >>> sys.stdin = _FakeInput([ ... 'print(y)', # print data defined in the function @@ -1755,7 +1756,7 @@ def test_pdb_set_trace(): (Pdb) print(y) 2 (Pdb) up - > (1)() + > (1)() -> calls_set_trace() (Pdb) print(x) 1 @@ -1773,7 +1774,7 @@ def test_pdb_set_trace(): ... ... import pdb; pdb.set_trace() ... >>> f(3) ... ''' - >>> test = parser.get_doctest(doc, globals(), "foo-bär@baz", "foo-bär@baz.py", 0) + >>> test = parser.get_doctest(doc, globals(), "foo-bar@baz", "foo-bar@baz.py", 0) >>> real_stdin = sys.stdin >>> sys.stdin = _FakeInput([ ... 'list', # list source from example 2 @@ -1787,7 +1788,7 @@ def test_pdb_set_trace(): ... finally: sys.stdin = real_stdin ... # doctest: +NORMALIZE_WHITESPACE --Return-- - > (3)g()->None + > (3)g()->None -> import pdb; pdb.set_trace() (Pdb) list 1 def g(x): @@ -1796,7 +1797,7 @@ def test_pdb_set_trace(): [EOF] (Pdb) next --Return-- - > (2)f()->None + > (2)f()->None -> g(x*2) (Pdb) list 1 def f(x): @@ -1804,14 +1805,14 @@ def test_pdb_set_trace(): [EOF] (Pdb) next --Return-- - > (1)()->None + > (1)()->None -> f(3) (Pdb) list 1 -> f(3) [EOF] (Pdb) continue ********************************************************************** - File "foo-bär@baz.py", line 7, in foo-bär@baz + File "foo-bar@baz.py", line 7, in foo-bar@baz Failed example: f(3) Expected nothing @@ -1845,7 +1846,7 @@ def test_pdb_set_trace_nested(): ... ''' >>> parser = doctest.DocTestParser() >>> runner = doctest.DocTestRunner(verbose=False) - >>> test = parser.get_doctest(doc, globals(), "foo-bär@baz", "foo-bär@baz.py", 0) + >>> test = parser.get_doctest(doc, globals(), "foo-bar@baz", "foo-bar@baz.py", 0) >>> real_stdin = sys.stdin >>> sys.stdin = _FakeInput([ ... 'print(y)', # print data defined in the function @@ -1898,7 +1899,7 @@ def test_pdb_set_trace_nested(): (Pdb) print(y) 1 (Pdb) up - > (1)() + > (1)() -> calls_set_trace() (Pdb) print(foo) *** NameError: name 'foo' is not defined @@ -2433,6 +2434,39 @@ out of the binary module. TestResults(failed=0, attempted=0) """ +try: + os.fsencode("foo-bär@baz.py") +except UnicodeEncodeError: + # Skip the test: the filesystem encoding is unable to encode the filename + pass +else: + def test_unicode(): """ +Check doctest with a non-ascii filename: + + >>> doc = ''' + ... >>> raise Exception('clé') + ... ''' + ... + >>> parser = doctest.DocTestParser() + >>> test = parser.get_doctest(doc, {}, "foo-bär@baz", "foo-bär@baz.py", 0) + >>> test + + >>> runner = doctest.DocTestRunner(verbose=False) + >>> runner.run(test) # doctest: +ELLIPSIS + ********************************************************************** + File "foo-bär@baz.py", line 2, in foo-bär@baz + Failed example: + raise Exception('clé') + Exception raised: + Traceback (most recent call last): + File ... + compileflags, 1), test.globs) + File "", line 1, in + raise Exception('clé') + Exception: clé + TestResults(failed=1, attempted=1) + """ + ###################################################################### ## Main ###################################################################### diff --git a/Misc/NEWS b/Misc/NEWS index b32f485..9dad54d 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -98,6 +98,12 @@ Tools/Demos - Issue #7287: Demo/imputil/knee.py was removed. +Tests +----- + +- Issue #10123: Don't use non-ascii filenames in test_doctest tests. Add a + new test specific to unicode (non-ascii name and filename). + Build ----- -- cgit v0.12