diff options
author | Brett Cannon <bcannon@gmail.com> | 2007-11-21 00:47:36 (GMT) |
---|---|---|
committer | Brett Cannon <bcannon@gmail.com> | 2007-11-21 00:47:36 (GMT) |
commit | 43e53f85b6f5e77a2f09cbda5833ffba5fbbdb2a (patch) | |
tree | 369e908698ea14e519aa00e646e912a7c22d391e | |
parent | 901071bde5eda2189cd396095bbb8934d4fa66b3 (diff) | |
download | cpython-43e53f85b6f5e77a2f09cbda5833ffba5fbbdb2a.zip cpython-43e53f85b6f5e77a2f09cbda5833ffba5fbbdb2a.tar.gz cpython-43e53f85b6f5e77a2f09cbda5833ffba5fbbdb2a.tar.bz2 |
doctest assumed that a package's __loader__.get_data() method used universal
newlines; it doesn't. To rectify this the string returned replaces all
instances of os.linesep with '\n' to fake universal newline support.
Backport candidate.
-rw-r--r-- | Lib/doctest.py | 5 | ||||
-rw-r--r-- | Lib/test/test_doctest.py | 17 | ||||
-rw-r--r-- | Misc/NEWS | 3 |
3 files changed, 24 insertions, 1 deletions
diff --git a/Lib/doctest.py b/Lib/doctest.py index 0db59ba..e874a26 100644 --- a/Lib/doctest.py +++ b/Lib/doctest.py @@ -209,7 +209,10 @@ def _load_testfile(filename, package, module_relative): filename = _module_relative_path(package, filename) if hasattr(package, '__loader__'): if hasattr(package.__loader__, 'get_data'): - return package.__loader__.get_data(filename), filename + file_contents = package.__loader__.get_data(filename) + # 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 open(filename).read(), filename def _indent(s, indent=4): diff --git a/Lib/test/test_doctest.py b/Lib/test/test_doctest.py index e2a24c4..e96785c 100644 --- a/Lib/test/test_doctest.py +++ b/Lib/test/test_doctest.py @@ -1908,6 +1908,23 @@ def test_DocFileSuite(): >>> suite.run(unittest.TestResult()) <unittest.TestResult run=3 errors=0 failures=3> + Support for using a package's __loader__.get_data() is also + provided. + + >>> import unittest, pkgutil, test + >>> if not hasattr(test, '__loader__'): + ... test.__loader__ = pkgutil.get_loader(test) + ... added_loader = True + >>> try: + ... suite = doctest.DocFileSuite('test_doctest.txt', + ... 'test_doctest2.txt', + ... 'test_doctest4.txt', + ... package='test') + ... suite.run(unittest.TestResult()) + ... finally: + ... del test.__loader__ + <unittest.TestResult run=3 errors=0 failures=3> + '/' should be used as a path separator. It will be converted to a native separator at run time: @@ -284,6 +284,9 @@ Core and builtins Library ------- +- doctest made a bad assumption that a package's __loader__.get_data() + method used universal newlines. + - Issue #1705170: contextlib.contextmanager was still swallowing StopIteration in some cases. This should no longer happen. |