bpo-37173: Show passed class in inspect.getfile error (GH-13861)
Currently, inspect.getfile(str) will report nonsense:
```pytb
>>> inspect.getfile(str)
TypeError: <module 'builtins' (built-in)> is a built-in class
```
This fixes that
https://bugs.python.org/issue37173
diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py
index 83a5f7e..1cd4ea2 100644
--- a/Lib/test/test_inspect.py
+++ b/Lib/test/test_inspect.py
@@ -513,6 +513,24 @@
def test_getfile(self):
self.assertEqual(inspect.getfile(mod.StupidGit), mod.__file__)
+ def test_getfile_builtin_module(self):
+ with self.assertRaises(TypeError) as e:
+ inspect.getfile(sys)
+ self.assertTrue(str(e.exception).startswith('<module'))
+
+ def test_getfile_builtin_class(self):
+ with self.assertRaises(TypeError) as e:
+ inspect.getfile(int)
+ self.assertTrue(str(e.exception).startswith('<class'))
+
+ def test_getfile_builtin_function_or_method(self):
+ with self.assertRaises(TypeError) as e_abs:
+ inspect.getfile(abs)
+ self.assertIn('expected, got', str(e_abs.exception))
+ with self.assertRaises(TypeError) as e_append:
+ inspect.getfile(list.append)
+ self.assertIn('expected, got', str(e_append.exception))
+
def test_getfile_class_without_module(self):
class CM(type):
@property