classification
Title: ResourceReader for FileLoader inconsistently handles path separators
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.8, Python 3.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: barry, brett.cannon, indygreg
Priority: normal Keywords:

Created on 2019-02-26 21:05 by indygreg, last changed 2019-02-27 22:58 by brett.cannon.

Messages (2)
msg336712 - (view) Author: Gregory Szorc (indygreg) * Date: 2019-02-26 21:05
The implementation of the ResourceReader API for the FileLoader class in importlib/_bootstrap_external.py is inconsistent with regards to handling of path separators.

Specifically, "is_resource()" returns False if "resource" has a path separator. But "open_resource()" will happily open resources containing a path separator.

I would think the two would agree about whether a path with separators is a resource or not. The documentation at https://docs.python.org/3.7/library/importlib.html#importlib.abc.ResourceReader implies that resources in subdirectories should not be allowed.

One can easily demonstrate this behavior oddity with Mercurial:

(Pdb) p sys.modules['mercurial'].__spec__.loader.get_resource_reader('mercurial').open_resource('help/config.txt')
<_io.FileIO name='/home/gps/src/hg/mercurial/help/config.txt' mode='rb' closefd=True>
(Pdb) p sys.modules['mercurial'].__spec__.loader.get_resource_reader('mercurial').is_resource('help/config.txt')
False

The behavior has been present since the functionality was added (https://github.com/python/cpython/pull/5168).
msg336723 - (view) Author: Barry A. Warsaw (barry) * (Python committer) Date: 2019-02-26 23:45
On Feb 26, 2019, at 13:05, Gregory Szorc <report@bugs.python.org> wrote:
> 
> I would think the two would agree about whether a path with separators is a resource or not. The documentation at https://docs.python.org/3.7/library/importlib.html#importlib.abc.ResourceReader implies that resources in subdirectories should not be allowed.

Historical context is in the standalone tracker:

https://gitlab.com/python-devs/importlib_resources/issues/58

Clearly, with the current definition of the API, open_resource() should not allow slashes.  Please follow up on the above link if you have opinions about changing the behavior.  We would have an opportunity to relax that constraint in Python 3.8, if it’s something we want to do.
History
Date User Action Args
2019-02-27 22:58:33brett.cannonsetnosy: + brett.cannon
2019-02-26 23:45:24barrysetmessages: + msg336723
2019-02-26 21:05:00indygregcreate