Skip to content

Possible bug introduced with PR250, joinpath with "." causes StopIteration #263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
VolkerH opened this issue Aug 17, 2022 · 6 comments
Closed
Labels
invalid This doesn't seem right

Comments

@VolkerH
Copy link

VolkerH commented Aug 17, 2022

Consider the following code snippet:

stopiteration_bug.py:

import spacem_maldi.Ontologies
from importlib_resources import files


print(files(spacem_maldi.Ontologies) / ".")

With importlib-resources<=5.7.1 this produces the following output:

/home/hilsenst/GitlabEMBL/spacem-ht/src/spacem-maldi/spacem_maldi/Ontologies

Starting from version 5.8.0, i.e. after PR #250 this throws a StopIteration exception:

~/Untracked$ python stopiteration_bug.py 
Traceback (most recent call last):
  File "stopiteration_bug.py", line 5, in <module>
    print(files(spacem_maldi.Ontologies) / ".")
  File "/home/hilsenst/miniconda3/envs/spacem-maldi-control/lib/python3.8/site-packages/importlib_resources/abc.py", line 130, in __truediv__
    return self.joinpath(child)
  File "/home/hilsenst/miniconda3/envs/spacem-maldi-control/lib/python3.8/site-packages/importlib_resources/readers.py", line 87, in joinpath
    return super().joinpath(*descendants)
  File "/home/hilsenst/miniconda3/envs/spacem-maldi-control/lib/python3.8/site-packages/importlib_resources/abc.py", line 114, in joinpath
    target = next(names)
StopIteration

The spacem_maldi package is a private package, but I don't think that is relevant here. If needed I can try and create a minimal public package.

@blackadar
Copy link

blackadar commented Aug 31, 2022

+1, there should be a way to get the Pathlib Path without joinpath anyways.

Also, a workaround for now:

print((files(spacem_maldi.Ontologies) / "fakefilename.txt").parent)

@VolkerH
Copy link
Author

VolkerH commented Sep 1, 2022

there should be a way to get the Pathlib Path without joinpath anyways

Yes, I felt that / "." was a crutch to start with but couln't find a better (proper) way.

@jaraco
Copy link
Member

jaraco commented Oct 5, 2022

Oh, you shouldn't rely on joinpath to get a materialized path. If what you need is a materialized path on the file system, use as_file(traversable). files() returns a Traversable and Traversable.joinpath also returns a Traversable, which may or may not be on the filesystem. Although depending on your usage, you may not need to materialize the traversable at all and you may be able to get the value directly from the Traversable (such as with .open or .read_text()).

Still, you shouldn't get a StopIteration by attempting to join a path.

@jaraco
Copy link
Member

jaraco commented Oct 5, 2022

I'm unable to replicate the issue:

>>> import importlib_resources as resources
>>> print(resources.files('importlib_resources') / ".")
/Users/jaraco/code/python/importlib_resources/importlib_resources

What's special about spacem_maldi.Ontologies that triggers the issue?

@jaraco
Copy link
Member

jaraco commented Oct 5, 2022

I thought maybe the issue was that Ontologies was a directory without an __init__, but I'm still unable to trigger the failure:

>>> import pathlib
>>> pathlib.Path('importlib_resources/foo').mkdir()
>>> print(resources.files('importlib_resources.foo') / ".")
/Users/jaraco/code/python/importlib_resources/importlib_resources/foo

@jaraco
Copy link
Member

jaraco commented Dec 4, 2022

Since the issue isn't reproducible, I'm closing it, but feel free to follow-up with the requested info and we can investigate further.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid This doesn't seem right
Projects
None yet
Development

No branches or pull requests

3 participants