Skip to content
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

zipfile .suffix/.suffixes raises TypeError when zipfile has no filename #96

Closed
graingert opened this issue Jun 8, 2023 · 8 comments
Closed
Assignees

Comments

@graingert
Copy link

import io
import sys
import zipfile
import logging

logger = logging.getLogger(__name__)


def main():
    with zipfile.ZipFile(io.BytesIO(), "w") as zf:
        zfp = zipfile.Path(zf)
        try:
            print((zfp / "example").suffix)
        except TypeError:
            logger.exception("suffix failed")

        try:
            print((zfp / "example").suffixes)
        except TypeError:
            logger.exception("suffixes failed")

        try:
            print((zfp / ".gitignore")).stem
        except TypeError:
            logger.exception("stem failed")



if __name__ == "__main__":
    sys.exit(main())

output:

suffix failed
Traceback (most recent call last):
  File "/home/graingert/projects/demo_zipp_suffix.py", line 14, in main
    print((zfp / "example").suffix)
          ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/graingert/.virtualenvs/testing311/lib/python3.11/site-packages/zipp/__init__.py", line 307, in suffix
    return pathlib.Path(self.at).suffix or self.filename.suffix
                                           ^^^^^^^^^^^^^
  File "/home/graingert/.virtualenvs/testing311/lib/python3.11/site-packages/zipp/__init__.py", line 319, in filename
    return pathlib.Path(self.root.filename).joinpath(self.at)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/pathlib.py", line 871, in __new__
    self = cls._from_parts(args)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/pathlib.py", line 509, in _from_parts
    drv, root, parts = self._parse_args(args)
                       ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/pathlib.py", line 493, in _parse_args
    a = os.fspath(a)
        ^^^^^^^^^^^^
TypeError: expected str, bytes or os.PathLike object, not NoneType
suffixes failed
Traceback (most recent call last):
  File "/home/graingert/projects/demo_zipp_suffix.py", line 19, in main
    print((zfp / "example").suffixes)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/graingert/.virtualenvs/testing311/lib/python3.11/site-packages/zipp/__init__.py", line 311, in suffixes
    return pathlib.Path(self.at).suffixes or self.filename.suffixes
                                             ^^^^^^^^^^^^^
  File "/home/graingert/.virtualenvs/testing311/lib/python3.11/site-packages/zipp/__init__.py", line 319, in filename
    return pathlib.Path(self.root.filename).joinpath(self.at)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/pathlib.py", line 871, in __new__
    self = cls._from_parts(args)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/pathlib.py", line 509, in _from_parts
    drv, root, parts = self._parse_args(args)
                       ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/pathlib.py", line 493, in _parse_args
    a = os.fspath(a)
        ^^^^^^^^^^^^
TypeError: expected str, bytes or os.PathLike object, not NoneType
stem failed
Traceback (most recent call last):
  File "/home/graingert/projects/demo_zipp_suffix.py", line 24, in main
    print((zfp / ".gitignore")).stem
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/graingert/.virtualenvs/testing311/lib/python3.11/site-packages/zipp/__init__.py", line 384, in __str__
    return posixpath.join(self.root.filename, self.at)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen posixpath>", line 76, in join
TypeError: expected str, bytes or os.PathLike object, not NoneType
@jaraco
Copy link
Owner

jaraco commented Jul 12, 2023

Thanks for the report. Will investigate.

@jaraco
Copy link
Owner

jaraco commented Jul 14, 2023

I'm not able to replicate the issue in tests. The tests are passing and they already exercised the first two concerns (filenames without an extension). And in 906d379, I added a test confirming that stem also works as expected with a filename starting with a dot.

@jaraco
Copy link
Owner

jaraco commented Jul 14, 2023

I found one error in the test above. The parentheses are misplaced for the .stem check and that's what's triggering the error. Rewriting the test allows it to pass:

        try:
            print((zfp / ".gitignore").stem)
        except TypeError:
            logger.exception("stem failed")

@jaraco
Copy link
Owner

jaraco commented Jul 14, 2023

Oh! The reason the other tests are failing is not because at has no suffix, but because root.filename has no suffix because root.filename is None.

@jaraco jaraco changed the title in a BytesIO zipfile .suffix/.suffixes raises TypeError when .at has no suffix, and .stem raises TypeError when .at has no stem zipfile .suffix/.suffixes raises TypeError when zipfile has no filename Jul 14, 2023
@jaraco jaraco closed this as completed in c230378 Jul 14, 2023
@graingert
Copy link
Author

@jaraco thanks for the fix!

@jaraco
Copy link
Owner

jaraco commented Jul 14, 2023

You bet. I'll be getting this and others rolled into CPython soon.

@graingert
Copy link
Author

I'd also expect print((zfp / ".gitignore")) to work

@jaraco
Copy link
Owner

jaraco commented Jul 14, 2023

Yes. Good point. I think that came up in another thread. I'll address the __str__ concern in another bug (#107).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants