[issue44137] importlib.resources.path raises RuntimeError when FileNotFoundError is raise in context manager
Miguel Brito
report at bugs.python.org
Sat May 15 05:01:30 EDT 2021
Miguel Brito <miguel.mdebrito at gmail.com> added the comment:
I can reproduce this. From what I can see the issue is that `importlib.resources.path` intentionally suppresses `FileNotFoundError` errors.
https://github.com/python/cpython/blob/main/Lib/importlib/resources.py#L138
Based on `importlib.resources.path` docstring I think this is to avoid raising an exception if the file is deleted before the context manager exits.
On contextlib.py, since type is not None, it will try to throw the exception but since it's supressed nothing will happen and the code will reach the end by raising the RuntimeError.
https://github.com/python/cpython/blob/main/Lib/contextlib.py#L151
If I'm not mistaken, this is the test that verifies that behaviour: https://github.com/python/cpython/blob/main/Lib/test/test_importlib/test_path.py#L51
I'm not a core dev but it looks like it's an intentional behaviour.
Maybe the docs should be more clear about this. It's not obvious to me when I read the docs.
----------
nosy: +miguendes
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue44137>
_______________________________________
More information about the Python-bugs-list
mailing list