[Python-checkins] cpython (merge 3.4 -> default): merge 3.4 (#21548)

benjamin.peterson python-checkins at python.org
Tue Feb 17 01:55:58 CET 2015


https://hg.python.org/cpython/rev/534b26837a13
changeset:   94659:534b26837a13
parent:      94657:f47d683b6c9e
parent:      94658:9436f43b6df2
user:        Benjamin Peterson <benjamin at python.org>
date:        Mon Feb 16 19:45:42 2015 -0500
summary:
  merge 3.4 (#21548)

files:
  Lib/pydoc.py           |   4 +-
  Lib/test/test_pydoc.py |  32 ++++++++++++++++++++++++++++++
  Misc/ACKS              |   1 +
  Misc/NEWS              |   3 ++
  4 files changed, 38 insertions(+), 2 deletions(-)


diff --git a/Lib/pydoc.py b/Lib/pydoc.py
--- a/Lib/pydoc.py
+++ b/Lib/pydoc.py
@@ -269,7 +269,7 @@
             except:
                 return None
             del sys.modules['__temp__']
-            result = (module.__doc__ or '').splitlines()[0]
+            result = module.__doc__.splitlines()[0] if module.__doc__ else None
         # Cache the result.
         cache[filename] = (mtime, result)
     return result
@@ -2073,7 +2073,7 @@
                         if onerror:
                             onerror(modname)
                         continue
-                    desc = (module.__doc__ or '').splitlines()[0]
+                    desc = module.__doc__.splitlines()[0] if module.__doc__ else ''
                     path = getattr(module,'__file__',None)
                 name = modname + ' - ' + desc
                 if name.lower().find(key) >= 0:
diff --git a/Lib/test/test_pydoc.py b/Lib/test/test_pydoc.py
--- a/Lib/test/test_pydoc.py
+++ b/Lib/test/test_pydoc.py
@@ -2,12 +2,15 @@
 import sys
 import builtins
 import contextlib
+import importlib.util
 import inspect
 import pydoc
+import py_compile
 import keyword
 import _pickle
 import pkgutil
 import re
+import stat
 import string
 import test.support
 import time
@@ -543,6 +546,18 @@
 
         self.assertEqual(synopsis, expected)
 
+    def test_synopsis_sourceless_empty_doc(self):
+        with test.support.temp_cwd() as test_dir:
+            init_path = os.path.join(test_dir, 'foomod42.py')
+            cached_path = importlib.util.cache_from_source(init_path)
+            with open(init_path, 'w') as fobj:
+                fobj.write("foo = 1")
+            py_compile.compile(init_path)
+            synopsis = pydoc.synopsis(init_path, {})
+            self.assertIsNone(synopsis)
+            synopsis_cached = pydoc.synopsis(cached_path, {})
+            self.assertIsNone(synopsis_cached)
+
     def test_splitdoc_with_description(self):
         example_string = "I Am A Doc\n\n\nHere is my description"
         self.assertEqual(pydoc.splitdoc(example_string),
@@ -598,6 +613,7 @@
     def setUp(self):
         self.test_dir = os.mkdir(TESTFN)
         self.addCleanup(rmtree, TESTFN)
+        importlib.invalidate_caches()
 
     def test_badimport(self):
         # This tests the fix for issue 5230, where if pydoc found the module
@@ -656,6 +672,22 @@
         self.assertEqual(out.getvalue(), '')
         self.assertEqual(err.getvalue(), '')
 
+    def test_apropos_empty_doc(self):
+        pkgdir = os.path.join(TESTFN, 'walkpkg')
+        os.mkdir(pkgdir)
+        self.addCleanup(rmtree, pkgdir)
+        init_path = os.path.join(pkgdir, '__init__.py')
+        with open(init_path, 'w') as fobj:
+            fobj.write("foo = 1")
+        current_mode = stat.S_IMODE(os.stat(pkgdir).st_mode)
+        try:
+            os.chmod(pkgdir, current_mode & ~stat.S_IEXEC)
+            with self.restrict_walk_packages(path=[TESTFN]), captured_stdout() as stdout:
+                pydoc.apropos('')
+            self.assertIn('walkpkg', stdout.getvalue())
+        finally:
+            os.chmod(pkgdir, current_mode)
+
     @unittest.skip('causes undesireable side-effects (#20128)')
     def test_modules(self):
         # See Helper.listmodules().
diff --git a/Misc/ACKS b/Misc/ACKS
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -513,6 +513,7 @@
 Dag Gruneau
 Filip Gruszczyński
 Thomas Guettler
+Yuyang Guo
 Anuj Gupta
 Michael Guravage
 Lars Gustäbel
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -13,6 +13,9 @@
 Library
 -------
 
+- Issue #21548: Fix pydoc.synopsis() and pydoc.apropos() on modules with empty
+  docstrings.
+
 - Issue #22885: Fixed arbitrary code execution vulnerability in the dbm.dumb
   module.  Original patch by Claudiu Popa.
 

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list