]> granicus.if.org Git - python/commitdiff
bpo-30639: Lazily compute repr for error (#2132)
authorThomas Kluyver <takowl@gmail.com>
Tue, 24 Oct 2017 12:42:36 +0000 (13:42 +0100)
committerYury Selivanov <yury@magic.io>
Tue, 24 Oct 2017 12:42:36 +0000 (08:42 -0400)
Lib/inspect.py
Lib/test/test_inspect.py
Misc/NEWS.d/next/Library/2017-10-24-12-24-56.bpo-30639.ptNM9a.rst [new file with mode: 0644]

index 9a843d6420e2e09133c270296df8fc38bc4cc1e9..6d6fde9ee4040137b3207f3b8cb1cbfa3936babc 100644 (file)
@@ -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."""
index 819fcc585376aad85b6ea6528c6e096c8800ec83..e64215d4677720ac7a79a6e46fc785067e6f52ea 100644 (file)
@@ -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'
diff --git a/Misc/NEWS.d/next/Library/2017-10-24-12-24-56.bpo-30639.ptNM9a.rst b/Misc/NEWS.d/next/Library/2017-10-24-12-24-56.bpo-30639.ptNM9a.rst
new file mode 100644 (file)
index 0000000..c6aeb23
--- /dev/null
@@ -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.