mirror of
https://github.com/python/cpython.git
synced 2025-08-03 08:34:29 +00:00
[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 160758a574
)
Co-authored-by: Jan Hicken <janhicken@users.noreply.github.com>
This commit is contained in:
parent
fa3ee75e6a
commit
0d3aa25ee4
2 changed files with 2 additions and 1 deletions
|
@ -339,7 +339,7 @@ class Path:
|
|||
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:
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
Improve performances of :meth:`zipfile.Path.open` for non-reading modes.
|
Loading…
Add table
Add a link
Reference in a new issue