]> granicus.if.org Git - python/commitdiff
bpo-29637: clean docstring only if not None (GH-267)
authorMatthias Bussonnier <bussonniermatthias@gmail.com>
Fri, 24 Feb 2017 06:44:19 +0000 (22:44 -0800)
committerINADA Naoki <methane@users.noreply.github.com>
Fri, 24 Feb 2017 06:44:19 +0000 (15:44 +0900)
Lib/ast.py
Lib/test/test_ast.py

index 0729ee71afc87f854c266526f58ae3ab9856575d..070c2bee7f9dee0b5704e4a1c5a17e915a22af02 100644 (file)
@@ -194,11 +194,14 @@ def get_docstring(node, clean=True):
     Return the docstring for the given node or None if no docstring can
     be found.  If the node provided does not have docstrings a TypeError
     will be raised.
+
+    If *clean* is `True`, all tabs are expanded to spaces and any whitespace
+    that can be uniformly removed from the second line onwards is removed.
     """
     if not isinstance(node, (AsyncFunctionDef, FunctionDef, ClassDef, Module)):
         raise TypeError("%r can't have docstrings" % node.__class__.__name__)
     text = node.docstring
-    if clean:
+    if clean and text:
         import inspect
         text = inspect.cleandoc(text)
     return text
index 366269fe405aa94438b2c535be886f07f931d037..2f59527cb8a450ded0213b8bcf9ba625e5aaec58 100644 (file)
@@ -532,6 +532,7 @@ class ASTHelpers_Test(unittest.TestCase):
 
         node = ast.parse('async def foo():\n  """spam\n  ham"""')
         self.assertEqual(ast.get_docstring(node.body[0]), 'spam\nham')
+        self.assertIsNone(ast.get_docstring(ast.parse('')))
 
     def test_literal_eval(self):
         self.assertEqual(ast.literal_eval('[1, 2, 3]'), [1, 2, 3])