Skip to content

Commit 41b4a21

Browse files
authored
bpo-16355: Clarify when inspect.getcomments() returns None (#428) (#691)
Initial patch by Vajrasky Kok. (cherry picked from commit 3f2155f)
1 parent 091d90f commit 41b4a21

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
@@ -440,7 +440,9 @@ Retrieving source code
440440

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

445447

446448
.. function:: getfile(object)

Lib/test/test_inspect.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,11 @@ def test_cleandoc(self):
376376
def test_getcomments(self):
377377
self.assertEqual(inspect.getcomments(mod), '# line 1\n')
378378
self.assertEqual(inspect.getcomments(mod.StupidGit), '# line 20\n')
379+
# If the object source file is not available, return None.
380+
co = compile('x=1', '_non_existing_filename.py', 'exec')
381+
self.assertIsNone(inspect.getcomments(co))
382+
# If the object has been defined in C, return None.
383+
self.assertIsNone(inspect.getcomments(list))
379384

380385
def test_getmodule(self):
381386
# Check actual module

0 commit comments

Comments
 (0)