diff options
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/inspect.py | 5 | ||||
-rw-r--r-- | Lib/test/test_inspect.py | 8 |
2 files changed, 11 insertions, 2 deletions
diff --git a/Lib/inspect.py b/Lib/inspect.py index 9a843d6..6d6fde9 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -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.""" diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py index 819fcc5..e64215d 100644 --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -463,6 +463,14 @@ class TestRetrievingSourceCode(GetSourceBase): 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' |