summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2019-06-08 15:27:06 (GMT)
committerGitHub <noreply@github.com>2019-06-08 15:27:06 (GMT)
commit51c9cc73cb8768a691688755af0a8b6b12cf712c (patch)
tree727b4f77cb5da601d67cd9a78bac242fad2c4778
parent10b4fd98142edef6ab7b034e10ae5c9551043999 (diff)
downloadcpython-51c9cc73cb8768a691688755af0a8b6b12cf712c.zip
cpython-51c9cc73cb8768a691688755af0a8b6b12cf712c.tar.gz
cpython-51c9cc73cb8768a691688755af0a8b6b12cf712c.tar.bz2
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 (cherry picked from commit d407d2a7265f6102e51a1d62b3fd28b4f7a78d16) Co-authored-by: Philipp A <flying-sheep@web.de>
-rw-r--r--Lib/inspect.py6
-rw-r--r--Lib/test/test_inspect.py18
-rw-r--r--Misc/NEWS.d/next/Library/2019-06-08-11-33-48.bpo-37173.0e_8gS.rst1
3 files changed, 22 insertions, 3 deletions
diff --git a/Lib/inspect.py b/Lib/inspect.py
index da4a424..4d4f33d 100644
--- a/Lib/inspect.py
+++ b/Lib/inspect.py
@@ -647,9 +647,9 @@ def getfile(object):
raise TypeError('{!r} is a built-in module'.format(object))
if isclass(object):
if hasattr(object, '__module__'):
- object = sys.modules.get(object.__module__)
- if getattr(object, '__file__', None):
- return object.__file__
+ module = sys.modules.get(object.__module__)
+ if getattr(module, '__file__', None):
+ return module.__file__
raise TypeError('{!r} is a built-in class'.format(object))
if ismethod(object):
object = object.__func__
diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py
index ee5a2d4..f3eb2f6 100644
--- a/Lib/test/test_inspect.py
+++ b/Lib/test/test_inspect.py
@@ -478,6 +478,24 @@ class TestRetrievingSourceCode(GetSourceBase):
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
diff --git a/Misc/NEWS.d/next/Library/2019-06-08-11-33-48.bpo-37173.0e_8gS.rst b/Misc/NEWS.d/next/Library/2019-06-08-11-33-48.bpo-37173.0e_8gS.rst
new file mode 100644
index 0000000..e996f97
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2019-06-08-11-33-48.bpo-37173.0e_8gS.rst
@@ -0,0 +1 @@
+The exception message for ``inspect.getfile()`` now correctly reports the passed class rather than the builtins module. \ No newline at end of file