Skip to content

Commit 5030e81

Browse files
[3.12] pythongh-126565: Skip zipfile.Path.exists check in write mode (pythonGH-126576) (python#126643)
pythongh-126565: Skip `zipfile.Path.exists` check in write mode (pythonGH-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. (cherry picked from commit 160758a) Co-authored-by: Jan Hicken <[email protected]>
1 parent 61e6f09 commit 5030e81

File tree

2 files changed

+2
-1
lines changed

2 files changed

+2
-1
lines changed

Lib/zipfile/_path/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ def open(self, mode='r', *args, pwd=None, **kwargs):
303303
if self.is_dir():
304304
raise IsADirectoryError(self)
305305
zip_mode = mode[0]
306-
if not self.exists() and zip_mode == 'r':
306+
if zip_mode == 'r' and not self.exists():
307307
raise FileNotFoundError(self)
308308
stream = self.root.open(self.at, zip_mode, pwd=pwd)
309309
if 'b' in mode:
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.

0 commit comments

Comments
 (0)