[Python-checkins] r67790 - in python/trunk/Lib: doctest.py test/test_zipimport_support.py

nick.coghlan python-checkins at python.org
Mon Dec 15 12:41:05 CET 2008


Author: nick.coghlan
Date: Mon Dec 15 12:41:05 2008
New Revision: 67790

Log:
Issue #4197: Fix the remaining part of the doctest-in-zipfile problem by giving linecache access to the module globals when available

Modified:
   python/trunk/Lib/doctest.py
   python/trunk/Lib/test/test_zipimport_support.py

Modified: python/trunk/Lib/doctest.py
==============================================================================
--- python/trunk/Lib/doctest.py	(original)
+++ python/trunk/Lib/doctest.py	Mon Dec 15 12:41:05 2008
@@ -820,7 +820,15 @@
         # given object's docstring.
         try:
             file = inspect.getsourcefile(obj) or inspect.getfile(obj)
-            source_lines = linecache.getlines(file)
+            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
         except TypeError:
@@ -1433,8 +1441,10 @@
         d = self._name2ft
         for name, (f, t) in other._name2ft.items():
             if name in d:
-                print "*** DocTestRunner.merge: '" + name + "' in both" \
-                    " testers; summing outcomes."
+                # Don't print here by default, since doing
+                #     so breaks some of the buildbots
+                #print "*** DocTestRunner.merge: '" + name + "' in both" \
+                #    " testers; summing outcomes."
                 f2, t2 = d[name]
                 f = f + f2
                 t = t + t2

Modified: python/trunk/Lib/test/test_zipimport_support.py
==============================================================================
--- python/trunk/Lib/test/test_zipimport_support.py	(original)
+++ python/trunk/Lib/test/test_zipimport_support.py	Mon Dec 15 12:41:05 2008
@@ -173,6 +173,35 @@
             for obj in known_good_tests:
                 _run_object_doctest(obj, test_zipped_doctest)
 
+    def test_doctest_main_issue4197(self):
+        test_src = textwrap.dedent("""\
+                    class Test:
+                        ">>> 'line 2'"
+                        pass
+
+                    import doctest
+                    doctest.testmod()
+                    """)
+        pattern = 'File "%s", line 2, in %s'
+        with temp_dir() as d:
+            script_name = _make_test_script(d, 'script', test_src)
+            exit_code, data = _run_python(script_name)
+            expected = pattern % (script_name, "__main__.Test")
+            if verbose:
+                print "Expected line", expected
+                print "Got stdout:"
+                print data
+            self.assert_(expected in data)
+            zip_name, run_name = _make_test_zip(d, "test_zip",
+                                                script_name, '__main__.py')
+            exit_code, data = _run_python(zip_name)
+            expected = pattern % (run_name, "__main__.Test")
+            if verbose:
+                print "Expected line", expected
+                print "Got stdout:"
+                print data
+            self.assert_(expected in data)
+
     def test_pdb_issue4201(self):
         test_src = textwrap.dedent("""\
                     def f():


More information about the Python-checkins mailing list