Skip to content

Commit 948171b

Browse files
authored
bpo-16355: Clarify when inspect.getcomments() returns None (#428) (#690)
Initial patch by Vajrasky Kok. (cherry picked from commit 3f2155f)
1 parent 7c20811 commit 948171b

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
@@ -387,6 +387,11 @@ def test_cleandoc(self):
387387
def test_getcomments(self):
388388
self.assertEqual(inspect.getcomments(mod), '# line 1\n')
389389
self.assertEqual(inspect.getcomments(mod.StupidGit), '# line 20\n')
390+
# If the object source file is not available, return None.
391+
co = compile('x=1', '_non_existing_filename.py', 'exec')
392+
self.assertIsNone(inspect.getcomments(co))
393+
# If the object has been defined in C, return None.
394+
self.assertIsNone(inspect.getcomments(list))
390395

391396
def test_getmodule(self):
392397
# Check actual module

0 commit comments

Comments
 (0)