From d3a81df178474f76407089de318a68554fe36841 Mon Sep 17 00:00:00 2001 From: Brett Cannon Date: Wed, 21 Nov 2007 00:58:54 +0000 Subject: [PATCH] Backport of r59082 (doctest and using __loader__.get_data()). --- Lib/doctest.py | 5 ++++- Lib/test/test_doctest.py | 18 ++++++++++++++++++ Misc/NEWS | 2 ++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/Lib/doctest.py b/Lib/doctest.py index 32d076aa7f..d609c5def5 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 e8379c5dc4..c3d1bd86c1 100644 --- a/Lib/test/test_doctest.py +++ b/Lib/test/test_doctest.py @@ -1908,6 +1908,24 @@ def test_DocFileSuite(): >>> suite.run(unittest.TestResult()) + 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: + ... if added_loader: + ... del test.__loader__ + + '/' should be used as a path separator. It will be converted to a native separator at run time: diff --git a/Misc/NEWS b/Misc/NEWS index 28c6c2a96a..a7cdba0143 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -38,6 +38,8 @@ Core and builtins Library ------- +- doctest mis-used __loader__.get_data(), assuming universal newlines was used. + - Issue #1705170: contextlib.contextmanager was still swallowing StopIteration in some cases. This should no longer happen. -- 2.40.0