summaryrefslogtreecommitdiffstats
path: root/Lib/doctest.py
diff options
context:
space:
mode:
authorPeter Donis <peterdonis@alum.mit.edu>2020-03-26 15:53:16 (GMT)
committerGitHub <noreply@github.com>2020-03-26 15:53:16 (GMT)
commite0b8101492f6c61dee831425b4d3dae39a953599 (patch)
treef734255a1f628628a1788b7d84d5e3bf3f00e8f4 /Lib/doctest.py
parent59c644eaa72b0cc48302f59d66852c4ea8332eba (diff)
downloadcpython-e0b8101492f6c61dee831425b4d3dae39a953599.zip
cpython-e0b8101492f6c61dee831425b4d3dae39a953599.tar.gz
cpython-e0b8101492f6c61dee831425b4d3dae39a953599.tar.bz2
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.
Diffstat (limited to 'Lib/doctest.py')
-rw-r--r--Lib/doctest.py9
1 files changed, 8 insertions, 1 deletions
diff --git a/Lib/doctest.py b/Lib/doctest.py
index 0229951..baa503c 100644
--- a/Lib/doctest.py
+++ b/Lib/doctest.py
@@ -211,6 +211,13 @@ def _normalize_module(module, depth=2):
else:
raise TypeError("Expected a module, string, or None")
+def _newline_convert(data):
+ # We have two cases to cover and we need to make sure we do
+ # them in the right order
+ for newline in ('\r\n', '\r'):
+ data = data.replace(newline, '\n')
+ return data
+
def _load_testfile(filename, package, module_relative, encoding):
if module_relative:
package = _normalize_module(package, 3)
@@ -221,7 +228,7 @@ def _load_testfile(filename, package, module_relative, encoding):
file_contents = file_contents.decode(encoding)
# get_data() opens files as 'rb', so one must do the equivalent
# conversion as universal newlines would do.
- return file_contents.replace(os.linesep, '\n'), filename
+ return _newline_convert(file_contents), filename
with open(filename, encoding=encoding) as f:
return f.read(), filename