# Try the cache again with the absolute file name
try:
file = getabsfile(object, _filename)
- except TypeError:
+ except (TypeError, FileNotFoundError):
return None
if file in modulesbyfile:
return sys.modules.get(modulesbyfile[file])
# 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")