diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2021-07-30 17:46:42 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-30 17:46:42 (GMT) |
commit | f468ede4a2b7ab5c72ae85ab04cb56904300cd23 (patch) | |
tree | aac90287c84307d96c2f8319901b1dfb589748fd | |
parent | 440c9f772a9b66c1ea387c1c3efc9ff438880acf (diff) | |
download | cpython-f468ede4a2b7ab5c72ae85ab04cb56904300cd23.zip cpython-f468ede4a2b7ab5c72ae85ab04cb56904300cd23.tar.gz cpython-f468ede4a2b7ab5c72ae85ab04cb56904300cd23.tar.bz2 |
bpo-44648: Fix error type in inspect.getsource() in interactive session (GH-27171) (GH-27495)
(cherry picked from commit 48a62559dfaf775e4f1cc56b19379c799e8e2587)
Co-authored-by: andrei kulakov <andrei.avk@gmail.com>
-rw-r--r-- | Lib/inspect.py | 2 | ||||
-rw-r--r-- | Lib/test/test_inspect.py | 17 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Library/2021-07-15-16-51-32.bpo-44648.2o49TB.rst | 3 |
3 files changed, 21 insertions, 1 deletions
diff --git a/Lib/inspect.py b/Lib/inspect.py index 750fd45..7aedcf1 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -781,6 +781,8 @@ def getfile(object): module = sys.modules.get(object.__module__) if getattr(module, '__file__', None): return module.__file__ + if object.__module__ == '__main__': + raise OSError('source code not available') raise TypeError('{!r} is a built-in class'.format(object)) if ismethod(object): object = object.__func__ diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py index 38d1618..b664c14 100644 --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -585,6 +585,20 @@ class TestRetrievingSourceCode(GetSourceBase): def test_getsource_on_code_object(self): self.assertSourceEqual(mod.eggs.__code__, 12, 18) +class TestGetsourceInteractive(unittest.TestCase): + def tearDown(self): + mod.ParrotDroppings.__module__ = mod + sys.modules['__main__'] = self.main + + def test_getclasses_interactive(self): + self.main = sys.modules['__main__'] + class MockModule: + __file__ = None + sys.modules['__main__'] = MockModule + mod.ParrotDroppings.__module__ = '__main__' + with self.assertRaisesRegex(OSError, 'source code not available') as e: + inspect.getsource(mod.ParrotDroppings) + class TestGettingSourceOfToplevelFrames(GetSourceBase): fodderModule = mod @@ -4349,7 +4363,8 @@ def test_main(): TestBoundArguments, TestSignaturePrivateHelpers, TestSignatureDefinitions, TestIsDataDescriptor, TestGetClosureVars, TestUnwrap, TestMain, TestReload, - TestGetCoroutineState, TestGettingSourceOfToplevelFrames + TestGetCoroutineState, TestGettingSourceOfToplevelFrames, + TestGetsourceInteractive, ) if __name__ == "__main__": diff --git a/Misc/NEWS.d/next/Library/2021-07-15-16-51-32.bpo-44648.2o49TB.rst b/Misc/NEWS.d/next/Library/2021-07-15-16-51-32.bpo-44648.2o49TB.rst new file mode 100644 index 0000000..f7171c3 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-07-15-16-51-32.bpo-44648.2o49TB.rst @@ -0,0 +1,3 @@ +Fixed wrong error being thrown by :func:`inspect.getsource` when examining a +class in the interactive session. Instead of :exc:`TypeError`, it should be +:exc:`OSError` with appropriate error message. |