Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 48a6255

Browse filesBrowse files
authored
bpo-44648: Fix error type in inspect.getsource() in interactive session (GH-27171)
1 parent ceea579 commit 48a6255
Copy full SHA for 48a6255

File tree

Expand file treeCollapse file tree

3 files changed

+21
-1
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+21
-1
lines changed

‎Lib/inspect.py

Copy file name to clipboardExpand all lines: Lib/inspect.py
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,8 @@ def getfile(object):
781781
module = sys.modules.get(object.__module__)
782782
if getattr(module, '__file__', None):
783783
return module.__file__
784+
if object.__module__ == '__main__':
785+
raise OSError('source code not available')
784786
raise TypeError('{!r} is a built-in class'.format(object))
785787
if ismethod(object):
786788
object = object.__func__

‎Lib/test/test_inspect.py

Copy file name to clipboardExpand all lines: Lib/test/test_inspect.py
+16-1Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,20 @@ def monkey(filename, module_globals=None):
585585
def test_getsource_on_code_object(self):
586586
self.assertSourceEqual(mod.eggs.__code__, 12, 18)
587587

588+
class TestGetsourceInteractive(unittest.TestCase):
589+
def tearDown(self):
590+
mod.ParrotDroppings.__module__ = mod
591+
sys.modules['__main__'] = self.main
592+
593+
def test_getclasses_interactive(self):
594+
self.main = sys.modules['__main__']
595+
class MockModule:
596+
__file__ = None
597+
sys.modules['__main__'] = MockModule
598+
mod.ParrotDroppings.__module__ = '__main__'
599+
with self.assertRaisesRegex(OSError, 'source code not available') as e:
600+
inspect.getsource(mod.ParrotDroppings)
601+
588602
class TestGettingSourceOfToplevelFrames(GetSourceBase):
589603
fodderModule = mod
590604

@@ -4342,7 +4356,8 @@ def test_main():
43424356
TestBoundArguments, TestSignaturePrivateHelpers,
43434357
TestSignatureDefinitions, TestIsDataDescriptor,
43444358
TestGetClosureVars, TestUnwrap, TestMain, TestReload,
4345-
TestGetCoroutineState, TestGettingSourceOfToplevelFrames
4359+
TestGetCoroutineState, TestGettingSourceOfToplevelFrames,
4360+
TestGetsourceInteractive,
43464361
)
43474362

43484363
if __name__ == "__main__":
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fixed wrong error being thrown by :func:`inspect.getsource` when examining a
2+
class in the interactive session. Instead of :exc:`TypeError`, it should be
3+
:exc:`OSError` with appropriate error message.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.