mirror of
https://github.com/python/cpython.git
synced 2025-12-10 11:00:14 +00:00
gh-116931: Add fileobj parameter check for Tarfile.addfile (GH-117988)
Tarfile.addfile now throws an ValueError when the user passes in a non-zero size tarinfo but does not provide a fileobj, instead of writing an incomplete entry.
This commit is contained in:
parent
3e7d990a09
commit
15b3555e4a
4 changed files with 25 additions and 9 deletions
|
|
@ -2214,13 +2214,16 @@ class TarFile(object):
|
|||
self.addfile(tarinfo)
|
||||
|
||||
def addfile(self, tarinfo, fileobj=None):
|
||||
"""Add the TarInfo object `tarinfo' to the archive. If `fileobj' is
|
||||
given, it should be a binary file, and tarinfo.size bytes are read
|
||||
from it and added to the archive. You can create TarInfo objects
|
||||
directly, or by using gettarinfo().
|
||||
"""Add the TarInfo object `tarinfo' to the archive. If `tarinfo' represents
|
||||
a non zero-size regular file, the `fileobj' argument should be a binary file,
|
||||
and tarinfo.size bytes are read from it and added to the archive.
|
||||
You can create TarInfo objects directly, or by using gettarinfo().
|
||||
"""
|
||||
self._check("awx")
|
||||
|
||||
if fileobj is None and tarinfo.isreg() and tarinfo.size != 0:
|
||||
raise ValueError("fileobj not provided for non zero-size regular file")
|
||||
|
||||
tarinfo = copy.copy(tarinfo)
|
||||
|
||||
buf = tarinfo.tobuf(self.format, self.encoding, self.errors)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue