Skip to content

Commit

Permalink
workaournd writef() on pypy3
Browse files Browse the repository at this point in the history
pypy3's BufferedReader don't have __sizeof__()
so alternatively calculate it with seek() and tell()

Signed-off-by: Hiroshi Miura <miurahr@linux.com>
  • Loading branch information
miurahr committed Feb 11, 2021
1 parent 74bb0d8 commit 0518c80
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions py7zr/py7zr.py
Expand Up @@ -917,13 +917,21 @@ def writef(self, bio: BinaryIO, arcname: str):
elif isinstance(bio, io.TextIOBase):
# First check whether is it Text?
raise ValueError("Unsupported file object type: please open file with Binary mode.")
elif isinstance(bio, io.BufferedIOBase):
size = bio.__sizeof__()
elif hasattr(bio, "read") and hasattr(bio, "__sizeof__"):
# Allow objet type which has read() and length methods for duck typing
# CPython's io.BufferedIOBase or io.BufferedReader has __sizeof__, but
# pypy3 don't have. So first check __sizeof__ and then goes to alternative.
# Also allowing objet type which has read() and length methods for duck typing
size = bio.__sizeof__()
elif isinstance(bio, io.BufferedIOBase):
# come here when subtype of io.BufferedIOBase that don't have __sizeof__ (eg. pypy)
# alternative for `size = bio.__sizeof__()`
current = bio.tell()
bio.seek(0, os.SEEK_END)
last = bio.tell()
bio.seek(current, os.SEEK_SET)
size = last - current
else:
raise ValueError("Wrong argument passed for bio.")
raise ValueError("Wrong argument passed for argument bio.")
file_info = self._make_file_info_from_name(bio, size, arcname)
self.header.files_info.files.append(file_info)
self.header.files_info.emptyfiles.append(file_info['emptystream'])
Expand Down

0 comments on commit 0518c80

Please sign in to comment.