From cc7384e6135390ce78676e2a60f1929427c85cf5 Mon Sep 17 00:00:00 2001 From: Philipp A Date: Thu, 6 Jun 2019 13:04:05 +0200 Subject: [PATCH 1/3] Show passed class in inspect.getfile error Currently, inspect.getfile(str) will report nonsense: >>> inspect.getfile(str) TypeError: is a built-in class This fixes that --- Lib/inspect.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Lib/inspect.py b/Lib/inspect.py index 91d209dc64bc9fd..99a580bd2f23500 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -659,9 +659,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__ From cffc9352722362c8b7a096fc17d6ec99940812bf Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" Date: Sat, 8 Jun 2019 11:33:50 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NEWS.d/next/Library/2019-06-08-11-33-48.bpo-37173.0e_8gS.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2019-06-08-11-33-48.bpo-37173.0e_8gS.rst 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 000000000000000..e996f97ecddd0ac --- /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 From c3bf2b43365e840c966a3a2c14c971b041500b05 Mon Sep 17 00:00:00 2001 From: Philipp A Date: Sat, 8 Jun 2019 13:39:20 +0200 Subject: [PATCH 3/3] added tests --- Lib/test/test_inspect.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py index 83a5f7ec1f5385d..1cd4ea28939d8ac 100644 --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -513,6 +513,24 @@ def test_getsourcefile(self): 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('