python / importlib_metadata

Library to access metadata for Python packages
https://importlib-metadata.readthedocs.io
Apache License 2.0
128 stars 82 forks source link

`Distribution._read_egg_info_reqs()` crashes if distribution does not contain egg-info files #56

Closed jaraco closed 4 years ago

jaraco commented 4 years ago

In GitLab by @gahjelle on May 7, 2019, 18:37

Consider the following example:

>>> import importlib_metadata as metadata
>>> dist = metadata.distribution("zipp")
>>> list(dist.files)
[PackagePath('__pycache__/zipp.cpython-37.pyc'), PackagePath('zipp-0.4.0.dist-info/INSTALLER'), PackagePath('zipp-0.4.0.dist-info/LICENSE'), PackagePath('zipp-0.4.0.dist-info/METADATA'), PackagePath('zipp-0.4.0.dist-info/RECORD'), PackagePath('zipp-0.4.0.dist-info/WHEEL'), PackagePath('zipp-0.4.0.dist-info/top_level.txt'), PackagePath('zipp.py')]

>>> dist._read_egg_info_reqs()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/gahjelle/Dropbox/programmering/gitlab/importlib_metadata/importlib_metadata/api.py", line 249, in _read_egg_info_reqs
    return self._deps_from_requires_text(source)
  File "/home/gahjelle/Dropbox/programmering/gitlab/importlib_metadata/importlib_metadata/api.py", line 253, in _deps_from_requires_text
    section_pairs = cls._read_sections(source.splitlines())
AttributeError: 'NoneType' object has no attribute 'splitlines'

It would probably be more consistent to return None in this case?

jaraco commented 4 years ago

In GitLab by @gahjelle on May 7, 2019, 18:39

I think this could be fixed with the following change?

Original code in api.py line 247:

    def _read_egg_info_reqs(self):
        source = self.read_text('requires.txt')
        return self._deps_from_requires_text(source)

Add a simple test in the return statement:

    def _read_egg_info_reqs(self):
        source = self.read_text('requires.txt')
        return source and self._deps_from_requires_text(source)

This would be consistent with how the "sibling"-methods are implemented

jaraco commented 4 years ago

In GitLab by @gahjelle on May 7, 2019, 19:32

mentioned in merge request !58

jaraco commented 4 years ago

In GitLab by @jaraco on May 7, 2019, 20:09

This looks good, though I think it will be addressed in !57, inspired by !38. Glad to see you had the same instincts as us.

jaraco commented 4 years ago

In GitLab by @jaraco on May 7, 2019, 20:16

mentioned in merge request !38

jaraco commented 4 years ago

In GitLab by @jaraco on May 7, 2019, 20:25

closed