bpo-45406: make inspect.getmodule() return None when getabsfile() raises FileNotFoundError (GH-28824)
(cherry picked from commit a459a81530de700b3d3faeb827b22ed1c9985812)
Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py
index 4164634..93ff2f8 100644
--- a/Lib/test/test_inspect.py
+++ b/Lib/test/test_inspect.py
@@ -493,6 +493,15 @@ def test_getmodule(self):
# Check filename override
self.assertEqual(inspect.getmodule(None, modfile), mod)
+ def test_getmodule_file_not_found(self):
+ # See bpo-45406
+ def _getabsfile(obj, _filename):
+ raise FileNotFoundError('bad file')
+ with unittest.mock.patch('inspect.getabsfile', _getabsfile):
+ f = inspect.currentframe()
+ self.assertIsNone(inspect.getmodule(f))
+ inspect.getouterframes(f) # smoke test
+
def test_getframeinfo_get_first_line(self):
frame_info = inspect.getframeinfo(self.fodderModule.fr, 50)
self.assertEqual(frame_info.code_context[0], "# line 1\n")