]> granicus.if.org Git - python/commitdiff
bpo-16355: Clarify when inspect.getcomments() returns None (#428) (#691)
authorBerker Peksag <berker.peksag@gmail.com>
Fri, 17 Mar 2017 11:37:51 +0000 (14:37 +0300)
committerGitHub <noreply@github.com>
Fri, 17 Mar 2017 11:37:51 +0000 (14:37 +0300)
Initial patch by Vajrasky Kok.

(cherry picked from commit 3f2155ffe683080f2a1b28408fa48d43ba92f943)

Doc/library/inspect.rst
Lib/test/test_inspect.py

index 89263e7bec1795469a933f66b22376e1528053d4..9526a0d59dabb515378705992f250a7ef920c678 100644 (file)
@@ -440,7 +440,9 @@ Retrieving source code
 
    Return in a single string any lines of comments immediately preceding the
    object's source code (for a class, function, or method), or at the top of the
-   Python source file (if the object is a module).
+   Python source file (if the object is a module).  If the object's source code
+   is unavailable, return ``None``.  This could happen if the object has been
+   defined in C or the interactive shell.
 
 
 .. function:: getfile(object)
index d33de9e5bc3f78f1adc805db1f5712f35ff3c8a8..2283000c8602468df4c39fd8ddc1e0ddeb651868 100644 (file)
@@ -376,6 +376,11 @@ class TestRetrievingSourceCode(GetSourceBase):
     def test_getcomments(self):
         self.assertEqual(inspect.getcomments(mod), '# line 1\n')
         self.assertEqual(inspect.getcomments(mod.StupidGit), '# line 20\n')
+        # If the object source file is not available, return None.
+        co = compile('x=1', '_non_existing_filename.py', 'exec')
+        self.assertIsNone(inspect.getcomments(co))
+        # If the object has been defined in C, return None.
+        self.assertIsNone(inspect.getcomments(list))
 
     def test_getmodule(self):
         # Check actual module