]> granicus.if.org Git - python/commitdiff
Backport of r59082 (doctest and using __loader__.get_data()).
authorBrett Cannon <bcannon@gmail.com>
Wed, 21 Nov 2007 00:58:54 +0000 (00:58 +0000)
committerBrett Cannon <bcannon@gmail.com>
Wed, 21 Nov 2007 00:58:54 +0000 (00:58 +0000)
Lib/doctest.py
Lib/test/test_doctest.py
Misc/NEWS

index 32d076aa7f357ecb13e29eeec0068a1e877de1ad..d609c5def54f6697c79845be443e0573df237458 100644 (file)
@@ -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):
index e8379c5dc4cf073517f77a1d735653c14a5906e8..c3d1bd86c1dd07e713cf130dd49f4590a60dfb06 100644 (file)
@@ -1908,6 +1908,24 @@ 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:
+         ...     if added_loader:
+         ...         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:
 
index 28c6c2a96a7ac288c81a87e60339c1f98139dab5..a7cdba01431c2c8309f8a31a5c4cbbdb7888cd95 100644 (file)
--- 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.