[issue44137] importlib.resources.path raises RuntimeError when FileNotFoundError is raise in context manager

2021-05-22 Thread Nils Kattenbeck


Nils Kattenbeck  added the comment:

Thanks for looking into it. Yes I can confirm that `importlib_resources` has 
the expected behaviour - I did not download Python 3.10 as the code seems to be 
the same.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44137] importlib.resources.path raises RuntimeError when FileNotFoundError is raise in context manager

2021-05-21 Thread Jason R. Coombs


Jason R. Coombs  added the comment:

Thanks for tracking this down, Filipe. Agreed it sounds like it's fixed in 
Python 3.10. It's unlikely the fix will be backported to Python 3.9. Instead, 
if this behavior affects your usage, consider using the `importlib_resources` 
backport, which also includes the fix. And please follow-up if there are issues 
not addressed by this approach.

--
resolution:  -> out of date
stage:  -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44137] importlib.resources.path raises RuntimeError when FileNotFoundError is raise in context manager

2021-05-17 Thread Filipe Laíns

Filipe Laíns  added the comment:

The supress(FileNotFoundError) context manager now is only active when fetching 
the file, not during importlib.resources.path. _path_from_resource_path is not 
longer a generator, so supress(FileNotFoundError) is promptly closed after the 
return, preventing its side effects from leaking outside the function.

Your reproducible now works as intended:

Traceback (most recent call last):
  File "/home/anubis/git/cpython/test.py", line 9, in 
main()
  File "/home/anubis/git/cpython/test.py", line 6, in main
raise FileNotFoundError()
FileNotFoundError

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44137] importlib.resources.path raises RuntimeError when FileNotFoundError is raise in context manager

2021-05-17 Thread Nils Kattenbeck


Nils Kattenbeck  added the comment:

Yes I understand that the function handles this specially to not raise an 
exception if the file is not found in the package (even though the intention 
behind this is not clear to me). However if a user causes a 
FileNotFoundException itself inside of the context manager everything breaks 
(e.g. does something erroneous with the path, calls subprocess.run with a non 
existing binary etc).

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44137] importlib.resources.path raises RuntimeError when FileNotFoundError is raise in context manager

2021-05-17 Thread Filipe Laíns

Filipe Laíns  added the comment:

This was fixed in https://github.com/python/cpython/pull/22915. It can be 
closed now.

--
nosy: +FFY00

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44137] importlib.resources.path raises RuntimeError when FileNotFoundError is raise in context manager

2021-05-15 Thread Miguel Brito


Miguel Brito  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 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44137] importlib.resources.path raises RuntimeError when FileNotFoundError is raise in context manager

2021-05-14 Thread Nils Kattenbeck


Change by Nils Kattenbeck :


--
title: importlib.resources.path raises RuntimeError import FileNotFoundError is 
raise in context manager -> importlib.resources.path raises RuntimeError when 
FileNotFoundError is raise in context manager

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com