]> granicus.if.org Git - python/commitdiff
Issue #6195: fix doctest to no longer try to read 'source' data from
authorR. David Murray <rdmurray@bitdance.com>
Fri, 12 Jun 2009 15:33:19 +0000 (15:33 +0000)
committerR. David Murray <rdmurray@bitdance.com>
Fri, 12 Jun 2009 15:33:19 +0000 (15:33 +0000)
binary files.

Lib/doctest.py
Lib/test/test_doctest.py
Misc/NEWS

index 5fa588de1d522a8918d1b00b59a6f5ca6d67aea9..3d053d9bfdf7c1eac9bf7f82bc5b84b9a716e5e9 100644 (file)
@@ -812,20 +812,28 @@ class DocTestFinder:
         # DocTestFinder._find_lineno to find the line number for a
         # given object's docstring.
         try:
-            file = inspect.getsourcefile(obj) or inspect.getfile(obj)
-            if module is not None:
-                # Supply the module globals in case the module was
-                # originally loaded via a PEP 302 loader and
-                # file is not a valid filesystem path
-                source_lines = linecache.getlines(file, module.__dict__)
-            else:
-                # No access to a loader, so assume it's a normal
-                # filesystem path
-                source_lines = linecache.getlines(file)
-            if not source_lines:
-                source_lines = None
+            file = inspect.getsourcefile(obj)
         except TypeError:
             source_lines = None
+        else:
+            if not file:
+                # Check to see if it's one of our special internal "files"
+                # (see __patched_linecache_getlines).
+                file = inspect.getfile(obj)
+                if not file[0]+file[-2:] == '<]>': file = None
+            if file is None: source_lines = None
+            else:
+                if module is not None:
+                    # Supply the module globals in case the module was
+                    # originally loaded via a PEP 302 loader and
+                    # file is not a valid filesystem path
+                    source_lines = linecache.getlines(file, module.__dict__)
+                else:
+                    # No access to a loader, so assume it's a normal
+                    # filesystem path
+                    source_lines = linecache.getlines(file)
+                if not source_lines:
+                    source_lines = None
 
         # Initialize globals, and merge in extraglobs.
         if globs is None:
index 9a128fb2257289006fdd5369fc1827a24d854e14..8dcc8a4f45854e2104e8cef59f8e130d54752aff 100644 (file)
@@ -2288,6 +2288,17 @@ using the optional keyword argument `encoding`:
     >>> doctest.master = None  # Reset master.
 """
 
+def test_testmod(): r"""
+Tests for the testmod function.  More might be useful, but for now we're just
+testing the case raised by Issue 6195, where trying to doctest a C module would
+fail with a UnicodeDecodeError because doctest tried to read the "source" lines
+out of the binary module.
+
+    >>> import unicodedata
+    >>> doctest.testmod(unicodedata)
+    TestResults(failed=0, attempted=0)
+"""
+
 ######################################################################
 ## Main
 ######################################################################
index eeb6328bd44cf0cd7ab0d1788222a01bf35f722b..7cac68bff5044ad4a1539b960dd4e0662bb2aec1 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -24,6 +24,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #6195: fixed doctest to no longer try to read 'source' data from
+  binary files.
+
 - Issue #5262: Fixed bug in next rollover time computation in
   TimedRotatingFileHandler.