From c0eac86a4bc71c40bcc33442348b27362f55ee10 Mon Sep 17 00:00:00 2001 From: Marco Buttu Date: Fri, 3 Mar 2017 14:58:40 +0100 Subject: [PATCH 1/5] bpo-16355: fix lack in inspect.getcomments doc. Initial patch by Vajrasky Kok. --- Doc/library/inspect.rst | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Doc/library/inspect.rst b/Doc/library/inspect.rst index 3fa44a0c99891c..c482a692121fc6 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 interactive shell. .. function:: getfile(object) From ac228789dbca95c0d84d761085b39e603ed319e5 Mon Sep 17 00:00:00 2001 From: Marco Buttu Date: Sat, 4 Mar 2017 08:51:51 +0100 Subject: [PATCH 2/5] bpo-16355: Add test. Initial patch by Vajrasky Kok. --- Lib/test/test_inspect.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py index 9d9fedc21e5795..59d6e73dffa3a4 100644 --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -24,8 +24,8 @@ except ImportError: ThreadPoolExecutor = None -from test.support import run_unittest, TESTFN, DirsOnSysPath, cpython_only -from test.support import MISSING_C_DOCSTRINGS, cpython_only +from test.support import (run_unittest, TESTFN, DirsOnSysPath, cpython_only, + MISSING_C_DOCSTRINGS) from test.support.script_helper import assert_python_ok, assert_python_failure from test import inspect_fodder as mod from test import inspect_fodder2 as mod2 @@ -384,6 +384,12 @@ def test_cleandoc(self): 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 + fn = "_non_existing_filename_used_for_sourcefile_test.py" + co = compile("x=1", fn, "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 From 85516a8ca9f9329c27b38c009dcc5bfb0d3ce76d Mon Sep 17 00:00:00 2001 From: Marco Buttu Date: Wed, 8 Mar 2017 08:54:57 +0100 Subject: [PATCH 3/5] bpo-16355: Do not fix import. Initial patch by Vajrasky Kok. --- Lib/test/test_inspect.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py index 59d6e73dffa3a4..b3e20487d890aa 100644 --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -24,8 +24,8 @@ except ImportError: ThreadPoolExecutor = None -from test.support import (run_unittest, TESTFN, DirsOnSysPath, cpython_only, - MISSING_C_DOCSTRINGS) +from test.support import run_unittest, TESTFN, DirsOnSysPath, cpython_only +from test.support import MISSING_C_DOCSTRINGS, cpython_only from test.support.script_helper import assert_python_ok, assert_python_failure from test import inspect_fodder as mod from test import inspect_fodder2 as mod2 @@ -384,11 +384,10 @@ def test_cleandoc(self): 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 - fn = "_non_existing_filename_used_for_sourcefile_test.py" - co = compile("x=1", fn, "exec") + # 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 + # If the object has been defined in C, return None. self.assertIsNone(inspect.getcomments(list)) def test_getmodule(self): From b750a16409a5c8250e6b4dca4db8066896148b9a Mon Sep 17 00:00:00 2001 From: Marco Buttu Date: Thu, 16 Mar 2017 21:14:32 +0100 Subject: [PATCH 4/5] bpo-16355: Be consistent using single quotes. Initial patch by Vajrasky Kok. --- Lib/test/test_inspect.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py index b3e20487d890aa..2f12e989d907a9 100644 --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -385,7 +385,7 @@ 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") + 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)) From 07f92e81c1d23700b62537bbb978ac9bdc6d87ed Mon Sep 17 00:00:00 2001 From: Marco Buttu Date: Fri, 17 Mar 2017 09:29:59 +0100 Subject: [PATCH 5/5] bpo-16355: fix a grammatical issue. --- Doc/library/inspect.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/inspect.rst b/Doc/library/inspect.rst index c482a692121fc6..4ff2187b45f8eb 100644 --- a/Doc/library/inspect.rst +++ b/Doc/library/inspect.rst @@ -444,7 +444,7 @@ Retrieving source code 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). If the object's source code is unavailable, return ``None``. This could happen if the object has been - defined in C or interactive shell. + defined in C or the interactive shell. .. function:: getfile(object)