https://github.com/python/cpython/commit/0d3aa25ee41d03bb8b4e6d90beee15835321f724
commit: 0d3aa25ee41d03bb8b4e6d90beee15835321f724
branch: 3.13
author: Miss Islington (bot) <[email protected]>
committer: jaraco <[email protected]>
date: 2024-11-10T15:21:47Z
summary:

[3.13] gh-126565: Skip `zipfile.Path.exists` check in write mode (GH-126576) 
(#126642)

gh-126565: Skip `zipfile.Path.exists` check in write mode (GH-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 160758a574d12bf0d965d8206136e7da4f4fd6c3)

Co-authored-by: Jan Hicken <[email protected]>

files:
A Misc/NEWS.d/next/Library/2024-11-08-11-06-14.gh-issue-126565.dFFO22.rst
M Lib/zipfile/_path/__init__.py

diff --git a/Lib/zipfile/_path/__init__.py b/Lib/zipfile/_path/__init__.py
index 5079db90648006..b7d4f8906e8bed 100644
--- a/Lib/zipfile/_path/__init__.py
+++ b/Lib/zipfile/_path/__init__.py
@@ -339,7 +339,7 @@ def open(self, mode='r', *args, pwd=None, **kwargs):
         if self.is_dir():
             raise IsADirectoryError(self)
         zip_mode = mode[0]
-        if not self.exists() and zip_mode == 'r':
+        if zip_mode == 'r' and not self.exists():
             raise FileNotFoundError(self)
         stream = self.root.open(self.at, zip_mode, pwd=pwd)
         if 'b' in mode:
diff --git 
a/Misc/NEWS.d/next/Library/2024-11-08-11-06-14.gh-issue-126565.dFFO22.rst 
b/Misc/NEWS.d/next/Library/2024-11-08-11-06-14.gh-issue-126565.dFFO22.rst
new file mode 100644
index 00000000000000..22858570bbe03c
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-11-08-11-06-14.gh-issue-126565.dFFO22.rst
@@ -0,0 +1 @@
+Improve performances of :meth:`zipfile.Path.open` for non-reading modes.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to