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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,8 +662,9 @@ def getfile(object):
object = object.f_code
if iscode(object):
return object.co_filename
raise TypeError('{!r} is not a module, class, method, '
'function, traceback, frame, or code object'.format(object))
raise TypeError('module, class, method, function, traceback, frame, or '
'code object was expected, got {}'.format(
type(object).__name__))

def getmodulename(path):
"""Return the module name for a given file, or None."""
Expand Down
8 changes: 8 additions & 0 deletions Lib/test/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,14 @@ class C(metaclass=CM):
with self.assertRaises(TypeError):
inspect.getfile(C)

def test_getfile_broken_repr(self):
class ErrorRepr:
def __repr__(self):
raise Exception('xyz')
er = ErrorRepr()
with self.assertRaises(TypeError):
inspect.getfile(er)

def test_getmodule_recursion(self):
from types import ModuleType
name = '__inspect_dummy'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:func:`inspect.getfile` no longer computes the repr of unknown objects to
display in an error message, to protect against badly behaved custom reprs.