Skip to content

Commit 1d42f0a

Browse files
committed
Find the archive from the left and skip directories named like archives
_get_zip_path_end() now checks .zip and .egg names together, from left to right, and the first archive found ends the search. A path such as lib.egg/pkg/data.zip/mod.py is split after lib.egg and pkg/data.zip/mod.py is the path inside it. A segment which is a directory on disk, such as outer.zip in outer.zip/lib.zip/pkg/mod.py, is skipped so that lib.zip is used. Paths which aren't on disk are still split at the first archive name.
1 parent 495e266 commit 1d42f0a

2 files changed

Lines changed: 49 additions & 11 deletions

File tree

‎src/debugpy/_vendored/pydevd/pydevd_file_utils.py‎

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949

5050
import json
5151
import os.path
52+
import re
5253
import sys
5354
import itertools
5455
import ntpath
@@ -484,7 +485,11 @@ def _get_relative_filename_abs_path(filename, func, os_path_exists=os_path_exist
484485
return r
485486

486487

487-
def _get_zip_path_end(filename):
488+
# A .zip/.egg extension (optionally followed by "!") which ends a path segment.
489+
_ZIP_PATH_END_RE = re.compile(r"\.(?:zip|egg)!?(?=[/\\]|\Z)")
490+
491+
492+
def _get_zip_path_end(filename, os_path_isdir=os.path.isdir):
488493
"""
489494
:return int:
490495
The index just after the .zip/.egg file name in the given filename (so that
@@ -493,17 +498,16 @@ def _get_zip_path_end(filename):
493498
494499
The extension (optionally followed by "!") must end a path segment, so names such
495500
as "archive.zip_", "foo.zipx" or "bar.egg-info" are not taken as archives.
501+
502+
Path segments are checked from left to right and the first archive found ends the
503+
search, so the path inside it is kept as is even if it has other .zip/.egg names.
504+
A segment which is a directory on disk (such as "outer.zip" in
505+
"outer.zip/lib.zip/mod.py") is skipped, while a path which isn't on disk is only
506+
checked by name.
496507
"""
497-
for ext in (".zip", ".egg"):
498-
ind = filename.find(ext)
499-
while ind != -1:
500-
ind += 4
501-
end = ind
502-
if filename[end : end + 1] == "!":
503-
end += 1
504-
if end == len(filename) or filename[end] in ("/", "\\"):
505-
return ind
506-
ind = filename.find(ext, ind)
508+
for match in _ZIP_PATH_END_RE.finditer(filename):
509+
if not os_path_isdir(filename[: match.end()]):
510+
return match.start() + 4
507511
return -1
508512

509513

‎src/debugpy/_vendored/pydevd/tests_python/test_convert_utilities.py‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,10 @@ def path(*parts):
480480
assert _split_at_archive(path("foo.zipx", "a.zip", "b.py")) == (path("foo.zipx", "a.zip"), "b.py")
481481
assert _split_at_archive(path("archive.zip_", "a.egg", "b.py")) == (path("archive.zip_", "a.egg"), "b.py")
482482

483+
# Paths not on disk are split at the first archive name and the rest is kept as is.
484+
assert _split_at_archive(path("a.egg", "pkg", "b.zip", "c.py")) == (path("a.egg"), sep.join(("pkg", "b.zip", "c.py")))
485+
assert _split_at_archive(path("a.zip", "b.zip", "c.py")) == (path("a.zip"), sep.join(("b.zip", "c.py")))
486+
483487

484488
@pytest.mark.parametrize("name", ["archive.zip_", "foo.zipx", "x.zip.bak", "bar.egg-info"])
485489
def test_zip_like_directory_names(tmpdir, name):
@@ -514,6 +518,36 @@ def test_exists_does_not_look_into_zip_with_similar_name(tmpdir):
514518
assert not pydevd_file_utils.exists(str(tmpdir.join("archive.zip_", "mod.py")))
515519

516520

521+
def test_zip_path_inside_archive_is_not_split_again(tmpdir):
522+
import zipfile
523+
524+
import pydevd_file_utils
525+
526+
egg_path = str(tmpdir.join("lib.egg"))
527+
with zipfile.ZipFile(egg_path, "w") as zip_file:
528+
zip_file.writestr("pkg/data.zip/mod.py", "")
529+
530+
# "lib.egg" is the archive and "pkg/data.zip/mod.py" is the path inside it.
531+
filename = os.path.join(egg_path, "pkg", "data.zip", "mod.py")
532+
assert _split_at_archive(filename) == (egg_path, os.path.join("pkg", "data.zip", "mod.py"))
533+
assert pydevd_file_utils.exists(filename)
534+
535+
536+
def test_zip_path_skips_directory_with_archive_name(tmpdir):
537+
import zipfile
538+
539+
import pydevd_file_utils
540+
541+
zip_path = str(tmpdir.mkdir("outer.zip").join("lib.zip"))
542+
with zipfile.ZipFile(zip_path, "w") as zip_file:
543+
zip_file.writestr("pkg/mod.py", "")
544+
545+
# "outer.zip" is a directory, so the archive is "lib.zip".
546+
filename = os.path.join(zip_path, "pkg", "mod.py")
547+
assert _split_at_archive(filename) == (zip_path, os.path.join("pkg", "mod.py"))
548+
assert pydevd_file_utils.exists(filename)
549+
550+
517551
def test_source_mapping():
518552
from _pydevd_bundle.pydevd_source_mapping import SourceMapping, SourceMappingEntry
519553
from _pydevd_bundle import pydevd_api

0 commit comments

Comments
 (0)