python / python/cpython

str(zipfile.Path()) not working when root attribute is io.BytesIO

Open
#100,609 12 comments 0 reactions 1 assignee View on GitHub

@jaraco is already working on this.

Since Jul 14, 2023.

stdlib type-bug
Dominant language
Python
Stars
77.2k
Forks
36k
PR merge metrics
PR metrics pending

Description

Using zipfile.Path with a ZIP-file "in memory" as io.BytesIO instance. In that case attributes like .filename() or .__str__() do not work because they try to instanciate a pathlib.Path() with a io.BytesIO object.

I can confirm this with Python 3.11 (run in Docker), Python 3.10.2 (Windows) and Python 3.9.2 (Debian stable). See output below for detailed version infos.

Code to reproduce

#!/usr/bin/env python3
import sys
import io
import pathlib
import zipfile

print('{} {}\n'.format(sys.platform, sys.version))

zip_bytes = io.BytesIO()

# create a zipfile in memory
with zipfile.ZipFile(zip_bytes, 'w') as zf:
    zf.writestr('entry.file', 'content')

# ZIP-Path object from in-memory-ZIP
my_zip_path = zipfile.Path(zip_bytes, 'entry.file')
print('Exists {}'.format(my_zip_path.exists()))

# here comes the Exception
print(my_zip_path)

Output: Python 3.11 on Linux/Docker

Digest: sha256:250990a809a15bb6a3e307fec72dead200c882c940523fb1694baa78eb0e47c6
Status: Downloaded newer image for python:3.11
Python 3.11.1 (main, Dec 21 2022, 18:32:57) [GCC 10.2.1 20210110] on linux
......
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.11/zipfile.py", line 2421, in filename
    return pathlib.Path(self.root.filename).joinpath(self.at)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/pathlib.py", line 871, in __new__
    self = cls._from_parts(args)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/pathlib.py", line 509, in _from_parts
    drv, root, parts = self._parse_args(args)
                       ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/pathlib.py", line 493, in _parse_args
    a = os.fspath(a)
        ^^^^^^^^^^^^
TypeError: expected str, bytes or os.PathLike object, not NoneType

Output: Python 3.10 on Windows

win32 3.10.2 (tags/v3.10.2:a58ebcc, Jan 17 2022, 14:12:15) [MSC v.1929 64 bit (AMD64)]

Exists True
Traceback (most recent call last):
  File "C:\Users\buhtzch\ownCloud\my.work\buhtzology\zippathbug.py", line 28, in <module>
    print(my_zip_path)
  File "C:\Program Files\Python310\lib\zipfile.py", line 2380, in __str__
    return posixpath.join(self.root.filename, self.at)
  File "C:\Program Files\Python310\lib\posixpath.py", line 76, in join
    a = os.fspath(a)
TypeError: expected str, bytes or os.PathLike object, not NoneType

Output: Python 3.9.2 on Debian stable (11)

linux 3.9.2 (default, Feb 28 2021, 17:03:44)
[GCC 10.2.1 20210110]

Exists True
Traceback (most recent call last):
  File "/home/user/ownCloud/my.work/buhtzology/zippathbug.py", line 28, in <module>
    print(my_zip_path)
  File "/usr/lib/python3.9/zipfile.py", line 2347, in __str__
    return posixpath.join(self.root.filename, self.at)
  File "/usr/lib/python3.9/posixpath.py", line 76, in join
    a = os.fspath(a)
TypeError: expected str, bytes or os.PathLike object, not NoneType

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.