Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e968bc7

Browse files
takluyver1st1
authored andcommitted
bpo-30639: Lazily compute repr for error (python#2132)
1 parent 8e482be commit e968bc7

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

Lib/inspect.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -662,8 +662,9 @@ def getfile(object):
662662
object = object.f_code
663663
if iscode(object):
664664
return object.co_filename
665-
raise TypeError('{!r} is not a module, class, method, '
666-
'function, traceback, frame, or code object'.format(object))
665+
raise TypeError('module, class, method, function, traceback, frame, or '
666+
'code object was expected, got {}'.format(
667+
type(object).__name__))
667668

668669
def getmodulename(path):
669670
"""Return the module name for a given file, or None."""

Lib/test/test_inspect.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,14 @@ class C(metaclass=CM):
463463
with self.assertRaises(TypeError):
464464
inspect.getfile(C)
465465

466+
def test_getfile_broken_repr(self):
467+
class ErrorRepr:
468+
def __repr__(self):
469+
raise Exception('xyz')
470+
er = ErrorRepr()
471+
with self.assertRaises(TypeError):
472+
inspect.getfile(er)
473+
466474
def test_getmodule_recursion(self):
467475
from types import ModuleType
468476
name = '__inspect_dummy'
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:func:`inspect.getfile` no longer computes the repr of unknown objects to
2+
display in an error message, to protect against badly behaved custom reprs.

0 commit comments

Comments
 (0)