Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9d39639

Browse files
author
Victor Stinner
committed
Issue #10123: Don't use non-ascii filenames in test_doctest tests. Add a
new test specific to unicode (non-ascii name and filename).
1 parent 2a53139 commit 9d39639

2 files changed

Lines changed: 52 additions & 12 deletions

File tree

Lib/test/test_doctest.py

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from test import support
66
import doctest
7+
import os
78

89

910
# NOTE: There are some additional tests relating to interaction with
@@ -372,7 +373,7 @@ def test_DocTestFinder(): r"""
372373
>>> tests = finder.find(sample_func)
373374
374375
>>> print(tests) # doctest: +ELLIPSIS
375-
[<DocTest sample_func from ...:16 (1 example)>]
376+
[<DocTest sample_func from ...:17 (1 example)>]
376377
377378
The exact name depends on how test_doctest was invoked, so allow for
378379
leading path components.
@@ -1703,7 +1704,7 @@ def test_pdb_set_trace():
17031704
... >>> import pdb; pdb.set_trace()
17041705
... '''
17051706
>>> parser = doctest.DocTestParser()
1706-
>>> test = parser.get_doctest(doc, {}, "foo-bär@baz", "foo-bär@baz.py", 0)
1707+
>>> test = parser.get_doctest(doc, {}, "foo-bar@baz", "foo-bar@baz.py", 0)
17071708
>>> runner = doctest.DocTestRunner(verbose=False)
17081709
17091710
To demonstrate this, we'll create a fake standard input that
@@ -1719,7 +1720,7 @@ def test_pdb_set_trace():
17191720
>>> try: runner.run(test)
17201721
... finally: sys.stdin = real_stdin
17211722
--Return--
1722-
> <doctest foo-bär@baz[2]>(1)<module>()->None
1723+
> <doctest foo-bar@baz[2]>(1)<module>()->None
17231724
-> import pdb; pdb.set_trace()
17241725
(Pdb) print(x)
17251726
42
@@ -1736,7 +1737,7 @@ def test_pdb_set_trace():
17361737
... >>> x=1
17371738
... >>> calls_set_trace()
17381739
... '''
1739-
>>> test = parser.get_doctest(doc, globals(), "foo-bär@baz", "foo-bär@baz.py", 0)
1740+
>>> test = parser.get_doctest(doc, globals(), "foo-bar@baz", "foo-bar@baz.py", 0)
17401741
>>> real_stdin = sys.stdin
17411742
>>> sys.stdin = _FakeInput([
17421743
... 'print(y)', # print data defined in the function
@@ -1755,7 +1756,7 @@ def test_pdb_set_trace():
17551756
(Pdb) print(y)
17561757
2
17571758
(Pdb) up
1758-
> <doctest foo-bär@baz[1]>(1)<module>()
1759+
> <doctest foo-bar@baz[1]>(1)<module>()
17591760
-> calls_set_trace()
17601761
(Pdb) print(x)
17611762
1
@@ -1773,7 +1774,7 @@ def test_pdb_set_trace():
17731774
... ... import pdb; pdb.set_trace()
17741775
... >>> f(3)
17751776
... '''
1776-
>>> test = parser.get_doctest(doc, globals(), "foo-bär@baz", "foo-bär@baz.py", 0)
1777+
>>> test = parser.get_doctest(doc, globals(), "foo-bar@baz", "foo-bar@baz.py", 0)
17771778
>>> real_stdin = sys.stdin
17781779
>>> sys.stdin = _FakeInput([
17791780
... 'list', # list source from example 2
@@ -1787,7 +1788,7 @@ def test_pdb_set_trace():
17871788
... finally: sys.stdin = real_stdin
17881789
... # doctest: +NORMALIZE_WHITESPACE
17891790
--Return--
1790-
> <doctest foo-bär@baz[1]>(3)g()->None
1791+
> <doctest foo-bar@baz[1]>(3)g()->None
17911792
-> import pdb; pdb.set_trace()
17921793
(Pdb) list
17931794
1 def g(x):
@@ -1796,22 +1797,22 @@ def test_pdb_set_trace():
17961797
[EOF]
17971798
(Pdb) next
17981799
--Return--
1799-
> <doctest foo-bär@baz[0]>(2)f()->None
1800+
> <doctest foo-bar@baz[0]>(2)f()->None
18001801
-> g(x*2)
18011802
(Pdb) list
18021803
1 def f(x):
18031804
2 -> g(x*2)
18041805
[EOF]
18051806
(Pdb) next
18061807
--Return--
1807-
> <doctest foo-bär@baz[2]>(1)<module>()->None
1808+
> <doctest foo-bar@baz[2]>(1)<module>()->None
18081809
-> f(3)
18091810
(Pdb) list
18101811
1 -> f(3)
18111812
[EOF]
18121813
(Pdb) continue
18131814
**********************************************************************
1814-
File "foo-bär@baz.py", line 7, in foo-bär@baz
1815+
File "foo-bar@baz.py", line 7, in foo-bar@baz
18151816
Failed example:
18161817
f(3)
18171818
Expected nothing
@@ -1845,7 +1846,7 @@ def test_pdb_set_trace_nested():
18451846
... '''
18461847
>>> parser = doctest.DocTestParser()
18471848
>>> runner = doctest.DocTestRunner(verbose=False)
1848-
>>> test = parser.get_doctest(doc, globals(), "foo-bär@baz", "foo-bär@baz.py", 0)
1849+
>>> test = parser.get_doctest(doc, globals(), "foo-bar@baz", "foo-bar@baz.py", 0)
18491850
>>> real_stdin = sys.stdin
18501851
>>> sys.stdin = _FakeInput([
18511852
... 'print(y)', # print data defined in the function
@@ -1898,7 +1899,7 @@ def test_pdb_set_trace_nested():
18981899
(Pdb) print(y)
18991900
1
19001901
(Pdb) up
1901-
> <doctest foo-bär@baz[1]>(1)<module>()
1902+
> <doctest foo-bar@baz[1]>(1)<module>()
19021903
-> calls_set_trace()
19031904
(Pdb) print(foo)
19041905
*** NameError: name 'foo' is not defined
@@ -2433,6 +2434,39 @@ def test_testmod(): r"""
24332434
TestResults(failed=0, attempted=0)
24342435
"""
24352436

2437+
try:
2438+
os.fsencode("foo-bä[email protected]")
2439+
except UnicodeEncodeError:
2440+
# Skip the test: the filesystem encoding is unable to encode the filename
2441+
pass
2442+
else:
2443+
def test_unicode(): """
2444+
Check doctest with a non-ascii filename:
2445+
2446+
>>> doc = '''
2447+
... >>> raise Exception('clé')
2448+
... '''
2449+
...
2450+
>>> parser = doctest.DocTestParser()
2451+
>>> test = parser.get_doctest(doc, {}, "foo-bär@baz", "foo-bä[email protected]", 0)
2452+
>>> test
2453+
<DocTest foo-bär@baz from foo-bä[email protected]:0 (1 example)>
2454+
>>> runner = doctest.DocTestRunner(verbose=False)
2455+
>>> runner.run(test) # doctest: +ELLIPSIS
2456+
**********************************************************************
2457+
File "foo-bä[email protected]", line 2, in foo-bär@baz
2458+
Failed example:
2459+
raise Exception('clé')
2460+
Exception raised:
2461+
Traceback (most recent call last):
2462+
File ...
2463+
compileflags, 1), test.globs)
2464+
File "<doctest foo-bär@baz[0]>", line 1, in <module>
2465+
raise Exception('clé')
2466+
Exception: clé
2467+
TestResults(failed=1, attempted=1)
2468+
"""
2469+
24362470
######################################################################
24372471
## Main
24382472
######################################################################

Misc/NEWS

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ Tools/Demos
9898

9999
- Issue #7287: Demo/imputil/knee.py was removed.
100100

101+
Tests
102+
-----
103+
104+
- Issue #10123: Don't use non-ascii filenames in test_doctest tests. Add a
105+
new test specific to unicode (non-ascii name and filename).
106+
101107
Build
102108
-----
103109

0 commit comments

Comments
 (0)