Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=Nonecreated_at=<Date2012-05-25.00:27:06.530>labels= ['3.8', 'type-feature', 'library']
title="zipimport needs to support namespace packages when no 'directory' entry exists"updated_at=<Date2020-08-13.13:47:03.805>user='https://github.com/ericvsmith'
See also test_namespace_pkgs.py ZipWithMissingDirectory.test_missing_directory which is currently marked as expectedFailure.
jerub commented on Jul 7, 2012
jerubmannequin
Mannequin
More actions
Here is a patch that synthesises the directory names at the point where file names are read in. The unit test now passes, and has had the expected failure removed.
How common are such broken zip files? Like Serhiy, I'm concerned about the possible negative impact on the interpreter startup time as we try to second guess the contents of the zip file manifest.
It seems better to be explicit that we consider such zipfiles broken and they need to be regenerated with full manifests (perhaps providing a script in Tools that fixes them).
OTOH, the scan time should be short relative to the time needed to read the manifest in the first place - an appropriate microbenchmark may also be adequate to address my concerns.
Why are zipfiles without entries for directories broken? When you don't care about directory permissions (such as when the zipfile won't be extracted at all) the entries for directories are not necessary. Also, AFAIK the zipfile specification <http://www.pkware.com/documents/casestudies/APPNOTE.TXT\> does not require adding directory entries to the zipfile.
FWIW: the zipfiles created by py2app do no contain entries for directories at the moment. I'll probably add entries for directories in the next update to work around this issue.
Reacted by Jason R. Coombs
pjeby commented on Apr 2, 2013
pjebymannequin
Mannequin
More actions
Just a note: the zip files produced by the distutils and friends (sdist, bdist_dumb, eggs) do not include entries for plain directories. I would guess that this is also true for wheels at the moment, unless something was specifically done to work around this property of distutils-generated zip files. So ISTM the right thing to do is to synthesize the entries at directory read time, when they're being looped over anyway.
Reviewing the patch, there is a performance optimization possible by making a slight change to the algorithm. Currently the patch loops from the start of the string to the end, looking for path prefixes. This means that the total overall performance is determined by the length of the strings and especially the average directory depth.
However, there is a significant shortcut possible: looping from the *end* of each string to the beginning, it's possible to break out of the loop if the prefix has already been seen -- thus saving (depth-1) dictionary lookups in the average case, and only looking at the characters in the base filename, unless a new directory is encountered... for a typical overhead of one unicode substring, dictionary lookup, and strrchr per zipfile directory entry. (Which is very small compared to what else is going on at that point in the process.)
To elaborate, if you have paths of the form:
x/y/a
x/y/b
x/y/c/d
Then when processing 'x/y/a', you would first process x/y -- it's not in the dict, add it. Then x -- not in the dict, add it. Then you go to x/y/b, your first parent is x/y again -- but since it's in the dict you skip it, and don't even bother with the x. Next you see x/y/c, which is not in the dict, so you add it, then x/y, which is, so you break out of the loop for that item.
Basically, about all that would change would be the for() loop starting at the end of the string and going to the beginning, with the loop position still representing the end of the prefix to be extracted. And the PyDict_Contains check would result in a break rather than a continue.
So, if the only concern keeping the patch from being accepted is that it adds to startup time, this approach would cut down quite a bit on the overhead for generating the path information, in cases of repeated prefixes. (And in the common cases for zipfile use on sys.path, one would expect to see a lot of common prefixes, if only for package names.)
phmc commented on Apr 3, 2013
phmcmannequin
Mannequin
More actions
The problem appears to be more general. zipimport fails for deeper hierarchies, even with directory entries.
With the supplied patch (zipimport-issue14905-2.patch) I see the following:
$ unzip -l foo.zip
Archive: foo.zip
Length Date Time Name
--------- ---------- -----
0 4 files
$ ls
foo.zip
$ PYTHONPATH=foo.zip ~/dev/cpython/python
Python 3.4.0a0 (default:3b1dbe7a2aa0+, Apr 3 2013, 17:31:54)
[GCC 4.8.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import a
>>> import a.b
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'a.b'
>>>
phmc commented on Apr 4, 2013
phmcmannequin
Mannequin
More actions
I've raised bpo-17633 to track the issue in my last message.
#121233 is slightly different from the patches proposed earlier, but the effect is the same. It may be faster for large archives with deep structure, although the difference may be insignificant. It also fixes get_data() and adds many new tests.
Activity
ericvsmith commented on May 25, 2012
If a zip file contains "pkg/foo.py" but no "pkg/" entry, it will not be possible for "pkg" to be a namespace package portion.
ericvsmith commented on May 25, 2012
For a (very) brief discussion on the strategy to implement this, see: http://mail.python.org/pipermail/import-sig/2012-May/000528.html
ericvsmith commented on May 25, 2012
See also test_namespace_pkgs.py ZipWithMissingDirectory.test_missing_directory which is currently marked as expectedFailure.
jerub commented on Jul 7, 2012
Here is a patch that synthesises the directory names at the point where file names are read in. The unit test now passes, and has had the expected failure removed.
Patch collaboration with Diarmuid Bourke <diarmuidbourke@gmail.com> at the europython sprint.
jerub commented on Aug 19, 2012
Please see attached new patch, based on review comments.
serhiy-storchaka commented on Feb 16, 2013
This can significant slowdown zipimport. I think we shouldn't support such broken zip files in zipimport.
ncoghlan commented on Feb 17, 2013
How common are such broken zip files? Like Serhiy, I'm concerned about the possible negative impact on the interpreter startup time as we try to second guess the contents of the zip file manifest.
It seems better to be explicit that we consider such zipfiles broken and they need to be regenerated with full manifests (perhaps providing a script in Tools that fixes them).
ncoghlan commented on Feb 17, 2013
OTOH, the scan time should be short relative to the time needed to read the manifest in the first place - an appropriate microbenchmark may also be adequate to address my concerns.
ericvsmith commented on Feb 17, 2013
I don't think such files are common: I've never seen such a file "in the wild". I created one, by accident, while testing PEP-420.
OTOH, it was surprisingly easy to create the malformed file with zipfile.
ronaldoussoren commented on Feb 18, 2013
Why are zipfiles without entries for directories broken? When you don't care about directory permissions (such as when the zipfile won't be extracted at all) the entries for directories are not necessary. Also, AFAIK the zipfile specification <http://www.pkware.com/documents/casestudies/APPNOTE.TXT\> does not require adding directory entries to the zipfile.
FWIW: the zipfiles created by py2app do no contain entries for directories at the moment. I'll probably add entries for directories in the next update to work around this issue.
pjeby commented on Apr 2, 2013
Just a note: the zip files produced by the distutils and friends (sdist, bdist_dumb, eggs) do not include entries for plain directories. I would guess that this is also true for wheels at the moment, unless something was specifically done to work around this property of distutils-generated zip files. So ISTM the right thing to do is to synthesize the entries at directory read time, when they're being looped over anyway.
Reviewing the patch, there is a performance optimization possible by making a slight change to the algorithm. Currently the patch loops from the start of the string to the end, looking for path prefixes. This means that the total overall performance is determined by the length of the strings and especially the average directory depth.
However, there is a significant shortcut possible: looping from the *end* of each string to the beginning, it's possible to break out of the loop if the prefix has already been seen -- thus saving (depth-1) dictionary lookups in the average case, and only looking at the characters in the base filename, unless a new directory is encountered... for a typical overhead of one unicode substring, dictionary lookup, and strrchr per zipfile directory entry. (Which is very small compared to what else is going on at that point in the process.)
To elaborate, if you have paths of the form:
x/y/a
x/y/b
x/y/c/d
Then when processing 'x/y/a', you would first process x/y -- it's not in the dict, add it. Then x -- not in the dict, add it. Then you go to x/y/b, your first parent is x/y again -- but since it's in the dict you skip it, and don't even bother with the x. Next you see x/y/c, which is not in the dict, so you add it, then x/y, which is, so you break out of the loop for that item.
Basically, about all that would change would be the for() loop starting at the end of the string and going to the beginning, with the loop position still representing the end of the prefix to be extracted. And the PyDict_Contains check would result in a break rather than a continue.
So, if the only concern keeping the patch from being accepted is that it adds to startup time, this approach would cut down quite a bit on the overhead for generating the path information, in cases of repeated prefixes. (And in the common cases for zipfile use on sys.path, one would expect to see a lot of common prefixes, if only for package names.)
phmc commented on Apr 3, 2013
The problem appears to be more general. zipimport fails for deeper hierarchies, even with directory entries.
With the supplied patch (zipimport-issue14905-2.patch) I see the following:
0 2013-04-03 17:28 a/b/c/foo.py 0 2013-04-03 17:34 a/ 0 2013-04-03 17:34 a/b/ 0 2013-04-03 17:34 a/b/c/--------- -------
phmc commented on Apr 4, 2013
I've raised bpo-17633 to track the issue in my last message.
11 remaining items
In zip namespace fixtures, explicitly generate the directory entries …
pypi-importlib_resources: Autospec creation for update from version 6…
serhiy-storchaka commented on Jul 4, 2024
#121233 is slightly different from the patches proposed earlier, but the effect is the same. It may be faster for large archives with deep structure, although the difference may be insignificant. It also fixes get_data() and adds many new tests.
gh-59110: zipimport: support namespace packages when no directory ent…
pythongh-59110: Fix a debug output for implicit directories
pythongh-59110: Extract function for _add_implicit_dirs.
gh-59110: Fix a debug output for implicit directories (GH-121375)
pythongh-59110: zipimport: support namespace packages when no directo…
pythongh-59110: Fix a debug output for implicit directories (pythonGH…
pythongh-59110: zipimport: support namespace packages when no directo…
pythongh-59110: Fix a debug output for implicit directories (pythonGH…