Skip to content

Commit 71ddd8d

Browse files
janhickenjaraco
authored andcommitted
Skip zipfile.Path.exists check in write mode (python/cpython#126576)
When `zipfile.Path.open` is called, the implementation will check whether the path already exists in the ZIP file. However, this check is only required when the ZIP file is in read mode. By swapping arguments of the `and` operator, the short-circuiting will prevent the check from being run in write mode. This change will improve the performance of `open()`, because checking whether a file exists is slow in write mode, especially when the archive has many members.
1 parent 9a2705e commit 71ddd8d

File tree

2 files changed

+2
-1
lines changed

2 files changed

+2
-1
lines changed

newsfragments/1a1928d.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve performances of :meth:`zipfile.Path.open` for non-reading modes.

zipp/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ def open(self, mode='r', *args, pwd=None, **kwargs):
343343
if self.is_dir():
344344
raise IsADirectoryError(self)
345345
zip_mode = mode[0]
346-
if not self.exists() and zip_mode == 'r':
346+
if zip_mode == 'r' and not self.exists():
347347
raise FileNotFoundError(self)
348348
stream = self.root.open(self.at, zip_mode, pwd=pwd)
349349
if 'b' in mode:

0 commit comments

Comments
 (0)