mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
bpo-30639: Lazily compute repr for error (#2132)
This commit is contained in:
parent
8e482bea21
commit
e968bc7357
3 changed files with 13 additions and 2 deletions
|
@ -662,8 +662,9 @@ def getfile(object):
|
||||||
object = object.f_code
|
object = object.f_code
|
||||||
if iscode(object):
|
if iscode(object):
|
||||||
return object.co_filename
|
return object.co_filename
|
||||||
raise TypeError('{!r} is not a module, class, method, '
|
raise TypeError('module, class, method, function, traceback, frame, or '
|
||||||
'function, traceback, frame, or code object'.format(object))
|
'code object was expected, got {}'.format(
|
||||||
|
type(object).__name__))
|
||||||
|
|
||||||
def getmodulename(path):
|
def getmodulename(path):
|
||||||
"""Return the module name for a given file, or None."""
|
"""Return the module name for a given file, or None."""
|
||||||
|
|
|
@ -463,6 +463,14 @@ class TestRetrievingSourceCode(GetSourceBase):
|
||||||
with self.assertRaises(TypeError):
|
with self.assertRaises(TypeError):
|
||||||
inspect.getfile(C)
|
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):
|
def test_getmodule_recursion(self):
|
||||||
from types import ModuleType
|
from types import ModuleType
|
||||||
name = '__inspect_dummy'
|
name = '__inspect_dummy'
|
||||||
|
|
|
@ -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.
|
Loading…
Add table
Add a link
Reference in a new issue