Skip to content

Commit 3f2155f

Browse files
marco-buttuberkerpeksag
authored andcommitted
bpo-16355: Clarify when inspect.getcomments() returns None (#428)
Initial patch by Vajrasky Kok.
1 parent 1bb0f37 commit 3f2155f

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

Doc/library/inspect.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,9 @@ Retrieving source code
442442

443443
Return in a single string any lines of comments immediately preceding the
444444
object's source code (for a class, function, or method), or at the top of the
445-
Python source file (if the object is a module).
445+
Python source file (if the object is a module). If the object's source code
446+
is unavailable, return ``None``. This could happen if the object has been
447+
defined in C or the interactive shell.
446448

447449

448450
.. function:: getfile(object)

Lib/test/test_inspect.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,11 @@ def test_cleandoc(self):
384384
def test_getcomments(self):
385385
self.assertEqual(inspect.getcomments(mod), '# line 1\n')
386386
self.assertEqual(inspect.getcomments(mod.StupidGit), '# line 20\n')
387+
# If the object source file is not available, return None.
388+
co = compile('x=1', '_non_existing_filename.py', 'exec')
389+
self.assertIsNone(inspect.getcomments(co))
390+
# If the object has been defined in C, return None.
391+
self.assertIsNone(inspect.getcomments(list))
387392

388393
def test_getmodule(self):
389394
# Check actual module

0 commit comments

Comments
 (0)