mirror of
https://github.com/python/cpython.git
synced 2025-07-24 11:44:31 +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
|
@ -1612,6 +1612,12 @@ class WriteTest(WriteTestBase, unittest.TestCase):
|
|||
pax_headers={'non': 'empty'})
|
||||
self.assertFalse(f.closed)
|
||||
|
||||
def test_missing_fileobj(self):
|
||||
with tarfile.open(tmpname, self.mode) as tar:
|
||||
tarinfo = tar.gettarinfo(tarname)
|
||||
with self.assertRaises(ValueError):
|
||||
tar.addfile(tarinfo)
|
||||
|
||||
|
||||
class GzipWriteTest(GzipTest, WriteTest):
|
||||
pass
|
||||
|
@ -3283,7 +3289,8 @@ class NoneInfoTests_Misc(unittest.TestCase):
|
|||
tar = tarfile.open(fileobj=bio, mode='w', format=tarformat)
|
||||
tarinfo = tar.gettarinfo(tarname)
|
||||
try:
|
||||
tar.addfile(tarinfo)
|
||||
with open(tarname, 'rb') as f:
|
||||
tar.addfile(tarinfo, f)
|
||||
except Exception:
|
||||
if tarformat == tarfile.USTAR_FORMAT:
|
||||
# In the old, limited format, adding might fail for
|
||||
|
@ -3298,7 +3305,8 @@ class NoneInfoTests_Misc(unittest.TestCase):
|
|||
replaced = tarinfo.replace(**{attr_name: None})
|
||||
with self.assertRaisesRegex(ValueError,
|
||||
f"{attr_name}"):
|
||||
tar.addfile(replaced)
|
||||
with open(tarname, 'rb') as f:
|
||||
tar.addfile(replaced, f)
|
||||
|
||||
def test_list(self):
|
||||
# Change some metadata to None, then compare list() output
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue