[New-bugs-announce] [issue17633] zipimport's handling of namespace packages is incorrect

Phil Connell report at bugs.python.org
Thu Apr 4 12:10:23 CEST 2013

New submission from Phil Connell:

Only one level of namespace package nesting is handled correctly:

$ unzip -l foo.zip
Archive:  foo.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        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/
---------                     -------
        0                     4 files
$ ls
$ 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'

The problem appears to be that check_is_directory constructs the wrong directory path (it should be 'a/b'):

check_is_directory (self=0x7ffff6d3dc88, prefix='a/', path='a.b')
    at ./Modules/zipimport.c:280
280     dirpath = PyUnicode_FromFormat("%U%U%c", prefix, path, SEP);
(gdb) n
281     if (dirpath == NULL)
(gdb) p dirpath
$11 = 'a/a.b/'

I've attached a tentative initial patch that appears to fix the issue, although it probably needs some more thought (and definitely some more testing - the existing tests still pass though).

components: Extension Modules
files: zipimport_ns.diff
keywords: patch
messages: 186025
nosy: brett.cannon, eric.snow, ncoghlan, pconnell
priority: normal
severity: normal
status: open
title: zipimport's handling of namespace packages is incorrect
versions: Python 3.4
Added file: http://bugs.python.org/file29679/zipimport_ns.diff

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list