Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite166f27

Browse files
committed
No more allow mode 'w'/'x'
- Mode 'w'/'x' may be used on an unseekable file buffer, which disallows truncation.
1 parent3b5055a commite166f27

File tree

3 files changed

+24
-19
lines changed

3 files changed

+24
-19
lines changed

‎Doc/library/zipfile.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ ZipFile Objects
497497
Removes a member from the archive. *zinfo_or_arcname* is either the full
498498
path of the member, or a:class:`ZipInfo` instance.
499499

500-
The archive must be opened with mode ``'w'``, ``'x'`` or ``'a'``.
500+
The archive must be opened with mode ``'a'``.
501501

502502
Calling:meth:`remove` on a closed ZipFile will raise a:exc:`ValueError`.
503503

‎Lib/test/test_zipfile/test_core.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,6 +1290,23 @@ def test_verify(self):
12901290
zh.remove(file)
12911291
mock_fn.assert_not_called()
12921292

1293+
# mode 'w': error and do nothing
1294+
withzipfile.ZipFile(TESTFN,'w',self.compression)aszh:
1295+
zh.writestr(file,data)
1296+
withmock.patch('zipfile.ZipFile._remove_members')asmock_fn:
1297+
withself.assertRaises(ValueError):
1298+
zh.remove(file)
1299+
mock_fn.assert_not_called()
1300+
1301+
# mode 'x': error and do nothing
1302+
os.remove(TESTFN)
1303+
withzipfile.ZipFile(TESTFN,'x',self.compression)aszh:
1304+
zh.writestr(file,data)
1305+
withmock.patch('zipfile.ZipFile._remove_members')asmock_fn:
1306+
withself.assertRaises(ValueError):
1307+
zh.remove(file)
1308+
mock_fn.assert_not_called()
1309+
12931310
# mode 'a': the most general use case
12941311
withzipfile.ZipFile(TESTFN,'w',self.compression)aszh:
12951312
zh.writestr(file,data)
@@ -1318,21 +1335,6 @@ def test_verify(self):
13181335
zh.remove(zinfo)
13191336
mock_fn.assert_not_called()
13201337

1321-
# mode 'w': like 'a'; allows removing a just written member
1322-
withzipfile.ZipFile(TESTFN,'w',self.compression)aszh:
1323-
zh.writestr(file,data)
1324-
withmock.patch('zipfile.ZipFile._remove_members')asmock_fn:
1325-
zh.remove(file)
1326-
mock_fn.assert_called_once_with({zh.getinfo(file)})
1327-
1328-
# mode 'x': like 'w'
1329-
os.remove(TESTFN)
1330-
withzipfile.ZipFile(TESTFN,'x',self.compression)aszh:
1331-
zh.writestr(file,data)
1332-
withmock.patch('zipfile.ZipFile._remove_members')asmock_fn:
1333-
zh.remove(file)
1334-
mock_fn.assert_called_once_with({zh.getinfo(file)})
1335-
13361338
deftest_zip64(self):
13371339
"""Test if members use zip64."""
13381340
file='datafile.txt'

‎Lib/zipfile/__init__.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1692,10 +1692,13 @@ def extractall(self, path=None, members=None, pwd=None):
16921692
self._extract_member(zipinfo,path,pwd)
16931693

16941694
defremove(self,zinfo_or_arcname):
1695-
"""Remove a member from the archive."""
1695+
"""Remove a member from the archive.
16961696
1697-
ifself.modenotin ('w','x','a'):
1698-
raiseValueError("remove() requires mode 'w', 'x', or 'a'")
1697+
The archive must be open with mode 'a', since mode 'w'/'x' may be used
1698+
on an unseekable file buffer, which disallows truncation."""
1699+
1700+
ifself.mode!='a':
1701+
raiseValueError("remove() requires mode 'a'")
16991702
ifnotself.fp:
17001703
raiseValueError(
17011704
"Attempt to write to ZIP archive that was already closed")

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp