From: Berker Peksag Date: Fri, 17 Mar 2017 11:59:16 +0000 (+0300) Subject: bpo-16355: Clarify when inspect.getcomments() returns None (#428) (#690) X-Git-Tag: v3.6.2rc1~308 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=948171bf999cf8b3e12048851041d2e04ae3a78c;p=python bpo-16355: Clarify when inspect.getcomments() returns None (#428) (#690) Initial patch by Vajrasky Kok. (cherry picked from commit 3f2155ffe683080f2a1b28408fa48d43ba92f943) --- diff --git a/Doc/library/inspect.rst b/Doc/library/inspect.rst index 3fa44a0c99..4ff2187b45 100644 --- a/Doc/library/inspect.rst +++ b/Doc/library/inspect.rst @@ -442,7 +442,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) diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py index 88eaabe676..cfea281c70 100644 --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -387,6 +387,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