Skip to content

Commit e0b8101

Browse files
authored
bpo-1812: Fix newline conversion when doctest.testfile loads from a package whose loader has a get_data method (GH-17385)
This pull request fixes the newline conversion bug originally reported in bpo-1812. When that issue was originally submitted, the open builtin did not default to universal newline mode; now it does, which makes the issue fix simpler, since the only code path that needs to be changed is the one in doctest._load_testfile where the file is loaded from a package whose loader has a get_data method.
1 parent 59c644e commit e0b8101

File tree

4 files changed

+100
-5
lines changed

4 files changed

+100
-5
lines changed

Lib/doctest.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,13 @@ def _normalize_module(module, depth=2):
211211
else:
212212
raise TypeError("Expected a module, string, or None")
213213

214+
def _newline_convert(data):
215+
# We have two cases to cover and we need to make sure we do
216+
# them in the right order
217+
for newline in ('\r\n', '\r'):
218+
data = data.replace(newline, '\n')
219+
return data
220+
214221
def _load_testfile(filename, package, module_relative, encoding):
215222
if module_relative:
216223
package = _normalize_module(package, 3)
@@ -221,7 +228,7 @@ def _load_testfile(filename, package, module_relative, encoding):
221228
file_contents = file_contents.decode(encoding)
222229
# get_data() opens files as 'rb', so one must do the equivalent
223230
# conversion as universal newlines would do.
224-
return file_contents.replace(os.linesep, '\n'), filename
231+
return _newline_convert(file_contents), filename
225232
with open(filename, encoding=encoding) as f:
226233
return f.read(), filename
227234

Lib/test/test_doctest.py

Lines changed: 89 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@
88
import os
99
import sys
1010
import importlib
11+
import importlib.abc
12+
import importlib.util
1113
import unittest
1214
import tempfile
15+
import shutil
16+
import contextlib
1317

1418
# NOTE: There are some additional tests relating to interaction with
1519
# zipimport in the test_zipimport_support test module.
@@ -437,7 +441,7 @@ def basics(): r"""
437441
>>> tests = finder.find(sample_func)
438442
439443
>>> print(tests) # doctest: +ELLIPSIS
440-
[<DocTest sample_func from ...:21 (1 example)>]
444+
[<DocTest sample_func from ...:25 (1 example)>]
441445
442446
The exact name depends on how test_doctest was invoked, so allow for
443447
leading path components.
@@ -2663,12 +2667,52 @@ def test_testfile(): r"""
26632667
>>> sys.argv = save_argv
26642668
"""
26652669

2670+
class TestImporter(importlib.abc.MetaPathFinder, importlib.abc.ResourceLoader):
2671+
2672+
def find_spec(self, fullname, path, target=None):
2673+
return importlib.util.spec_from_file_location(fullname, path, loader=self)
2674+
2675+
def get_data(self, path):
2676+
with open(path, mode='rb') as f:
2677+
return f.read()
2678+
2679+
class TestHook:
2680+
2681+
def __init__(self, pathdir):
2682+
self.sys_path = sys.path[:]
2683+
self.meta_path = sys.meta_path[:]
2684+
self.path_hooks = sys.path_hooks[:]
2685+
sys.path.append(pathdir)
2686+
sys.path_importer_cache.clear()
2687+
self.modules_before = sys.modules.copy()
2688+
self.importer = TestImporter()
2689+
sys.meta_path.append(self.importer)
2690+
2691+
def remove(self):
2692+
sys.path[:] = self.sys_path
2693+
sys.meta_path[:] = self.meta_path
2694+
sys.path_hooks[:] = self.path_hooks
2695+
sys.path_importer_cache.clear()
2696+
sys.modules.clear()
2697+
sys.modules.update(self.modules_before)
2698+
2699+
2700+
@contextlib.contextmanager
2701+
def test_hook(pathdir):
2702+
hook = TestHook(pathdir)
2703+
try:
2704+
yield hook
2705+
finally:
2706+
hook.remove()
2707+
2708+
26662709
def test_lineendings(): r"""
2667-
*nix systems use \n line endings, while Windows systems use \r\n. Python
2710+
*nix systems use \n line endings, while Windows systems use \r\n, and
2711+
old Mac systems used \r, which Python still recognizes as a line ending. Python
26682712
handles this using universal newline mode for reading files. Let's make
26692713
sure doctest does so (issue 8473) by creating temporary test files using each
2670-
of the two line disciplines. One of the two will be the "wrong" one for the
2671-
platform the test is run on.
2714+
of the three line disciplines. At least one will not match either the universal
2715+
newline \n or os.linesep for the platform the test is run on.
26722716
26732717
Windows line endings first:
26742718
@@ -2691,6 +2735,47 @@ def test_lineendings(): r"""
26912735
TestResults(failed=0, attempted=1)
26922736
>>> os.remove(fn)
26932737
2738+
And finally old Mac line endings:
2739+
2740+
>>> fn = tempfile.mktemp()
2741+
>>> with open(fn, 'wb') as f:
2742+
... f.write(b'Test:\r\r >>> x = 1 + 1\r\rDone.\r')
2743+
30
2744+
>>> doctest.testfile(fn, module_relative=False, verbose=False)
2745+
TestResults(failed=0, attempted=1)
2746+
>>> os.remove(fn)
2747+
2748+
Now we test with a package loader that has a get_data method, since that
2749+
bypasses the standard universal newline handling so doctest has to do the
2750+
newline conversion itself; let's make sure it does so correctly (issue 1812).
2751+
We'll write a file inside the package that has all three kinds of line endings
2752+
in it, and use a package hook to install a custom loader; on any platform,
2753+
at least one of the line endings will raise a ValueError for inconsistent
2754+
whitespace if doctest does not correctly do the newline conversion.
2755+
2756+
>>> dn = tempfile.mkdtemp()
2757+
>>> pkg = os.path.join(dn, "doctest_testpkg")
2758+
>>> os.mkdir(pkg)
2759+
>>> support.create_empty_file(os.path.join(pkg, "__init__.py"))
2760+
>>> fn = os.path.join(pkg, "doctest_testfile.txt")
2761+
>>> with open(fn, 'wb') as f:
2762+
... f.write(
2763+
... b'Test:\r\n\r\n'
2764+
... b' >>> x = 1 + 1\r\n\r\n'
2765+
... b'Done.\r\n'
2766+
... b'Test:\n\n'
2767+
... b' >>> x = 1 + 1\n\n'
2768+
... b'Done.\n'
2769+
... b'Test:\r\r'
2770+
... b' >>> x = 1 + 1\r\r'
2771+
... b'Done.\r'
2772+
... )
2773+
95
2774+
>>> with test_hook(dn):
2775+
... doctest.testfile("doctest_testfile.txt", package="doctest_testpkg", verbose=False)
2776+
TestResults(failed=0, attempted=3)
2777+
>>> shutil.rmtree(dn)
2778+
26942779
"""
26952780

26962781
def test_testmod(): r"""

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@ Walter Dörwald
421421
Jaromir Dolecek
422422
Zsolt Dollenstein
423423
Brendan Donegan
424+
Peter Donis
424425
Ismail Donmez
425426
Ray Donnelly
426427
Robert Donohue
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix newline handling in doctest.testfile when loading from a package whose
2+
loader has a get_data method. Patch by Peter Donis.

0 commit comments

Comments
 (0)