[Python-checkins] cpython (2.7): Issue #21548: Fix pydoc.synopsis() and pydoc.apropos() on modules with empty

berker.peksag python-checkins at python.org
Fri Feb 20 11:09:36 CET 2015


https://hg.python.org/cpython/rev/43641e03692a
changeset:   94697:43641e03692a
branch:      2.7
parent:      94691:a40481bbb62b
user:        Berker Peksag <berker.peksag at gmail.com>
date:        Fri Feb 20 12:10:33 2015 +0200
summary:
  Issue #21548: Fix pydoc.synopsis() and pydoc.apropos() on modules with empty
docstrings.

Initial patch by Yuyang Guo.

files:
  Lib/pydoc.py           |   4 ++--
  Lib/test/test_pydoc.py |  29 +++++++++++++++++++++++++++++
  Misc/NEWS              |   3 +++
  3 files changed, 34 insertions(+), 2 deletions(-)


diff --git a/Lib/pydoc.py b/Lib/pydoc.py
--- a/Lib/pydoc.py
+++ b/Lib/pydoc.py
@@ -255,7 +255,7 @@
         if info and 'b' in info[2]: # binary modules have to be imported
             try: module = imp.load_module('__temp__', file, filename, info[1:])
             except: return None
-            result = (module.__doc__ or '').splitlines()[0]
+            result = module.__doc__.splitlines()[0] if module.__doc__ else None
             del sys.modules['__temp__']
         else: # text modules can be directly examined
             result = source_synopsis(file)
@@ -2020,7 +2020,7 @@
                         path = None
                 else:
                     module = loader.load_module(modname)
-                    desc = (module.__doc__ or '').splitlines()[0]
+                    desc = module.__doc__.splitlines()[0] if module.__doc__ else ''
                     path = getattr(module,'__file__',None)
                 if find(lower(modname + ' - ' + desc), key) >= 0:
                     callback(path, modname, desc)
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
@@ -3,6 +3,7 @@
 import difflib
 import __builtin__
 import re
+import py_compile
 import pydoc
 import contextlib
 import inspect
@@ -382,6 +383,34 @@
         self.assertEqual(stripid("<type 'exceptions.Exception'>"),
                          "<type 'exceptions.Exception'>")
 
+    def test_synopsis(self):
+        with test.test_support.temp_cwd() as test_dir:
+            init_path = os.path.join(test_dir, 'dt.py')
+            with open(init_path, 'w') as fobj:
+                fobj.write('''\
+"""
+my doc
+
+second line
+"""
+foo = 1
+''')
+            py_compile.compile(init_path)
+            synopsis = pydoc.synopsis(init_path, {})
+            self.assertEqual(synopsis, 'my doc')
+
+    def test_synopsis_sourceless_empty_doc(self):
+        with test.test_support.temp_cwd() as test_dir:
+            init_path = os.path.join(test_dir, 'foomod42.py')
+            cached_path = os.path.join(test_dir, 'foomod42.pyc')
+            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)
+
 
 class PydocImportTest(PydocBaseTest):
 
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -18,6 +18,9 @@
 Library
 -------
 
+- Issue #21548: Fix pydoc.synopsis() and pydoc.apropos() on modules with empty
+  docstrings.  Initial patch by Yuyang Guo.
+
 - Issue #22885: Fixed arbitrary code execution vulnerability in the dumbdbm
   module.  Original patch by Claudiu Popa.
 

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


More information about the Python-checkins mailing list