FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

bpo-44648: Fix error type in inspect.getsource() in interactive session by akulakov · Pull Request #27171 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
2 changes: 2 additions & 0 deletions Lib/inspect.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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__
Expand Down
17 changes: 16 additions & 1 deletion Lib/test/test_inspect.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,20 @@ def monkey(filename, module_globals=None):
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

Expand Down Expand Up @@ -4301,7 +4315,8 @@ def test_main():
TestBoundArguments, TestSignaturePrivateHelpers,
TestSignatureDefinitions, TestIsDataDescriptor,
TestGetClosureVars, TestUnwrap, TestMain, TestReload,
TestGetCoroutineState, TestGettingSourceOfToplevelFrames
TestGetCoroutineState, TestGettingSourceOfToplevelFrames,
TestGetsourceInteractive,
)

if __name__ == "__main__":
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -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.

Back | FazBrowse Home | New Git URL