Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0d18c15

Browse files
committed
Issue #14209: pkgutil.iter_zipimport_modules ignores the prefix for packages
Patch by James Pickering.
1 parent e7f2748 commit 0d18c15

3 files changed

Lines changed: 81 additions & 4 deletions

File tree

Lib/pkgutil.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ def iter_zipimport_modules(importer, prefix=''):
375375
if len(fn)==2 and fn[1].startswith('__init__.py'):
376376
if fn[0] not in yielded:
377377
yielded[fn[0]] = 1
378-
yield fn[0], True
378+
yield prefix + fn[0], True
379379

380380
if len(fn)!=1:
381381
continue

Lib/test/test_pkgutil.py

Lines changed: 79 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import os
88
import os.path
99
import tempfile
10-
import types
1110
import shutil
1211
import zipfile
1312

@@ -101,6 +100,83 @@ def test_unreadable_dir_on_syspath(self):
101100
for t in pkgutil.walk_packages(path=[self.dirname]):
102101
self.fail("unexpected package found")
103102

103+
def test_walkpackages_filesys(self):
104+
pkg1 = 'test_walkpackages_filesys'
105+
pkg1_dir = os.path.join(self.dirname, pkg1)
106+
os.mkdir(pkg1_dir)
107+
f = open(os.path.join(pkg1_dir, '__init__.py'), "wb")
108+
f.close()
109+
os.mkdir(os.path.join(pkg1_dir, 'sub'))
110+
f = open(os.path.join(pkg1_dir, 'sub', '__init__.py'), "wb")
111+
f.close()
112+
f = open(os.path.join(pkg1_dir, 'sub', 'mod.py'), "wb")
113+
f.close()
114+
115+
# Now, to juice it up, let's add the opposite packages, too.
116+
pkg2 = 'sub'
117+
pkg2_dir = os.path.join(self.dirname, pkg2)
118+
os.mkdir(pkg2_dir)
119+
f = open(os.path.join(pkg2_dir, '__init__.py'), "wb")
120+
f.close()
121+
os.mkdir(os.path.join(pkg2_dir, 'test_walkpackages_filesys'))
122+
f = open(os.path.join(pkg2_dir, 'test_walkpackages_filesys', '__init__.py'), "wb")
123+
f.close()
124+
f = open(os.path.join(pkg2_dir, 'test_walkpackages_filesys', 'mod.py'), "wb")
125+
f.close()
126+
127+
expected = [
128+
'sub',
129+
'sub.test_walkpackages_filesys',
130+
'sub.test_walkpackages_filesys.mod',
131+
'test_walkpackages_filesys',
132+
'test_walkpackages_filesys.sub',
133+
'test_walkpackages_filesys.sub.mod',
134+
]
135+
actual= [e[1] for e in pkgutil.walk_packages([self.dirname])]
136+
self.assertEqual(actual, expected)
137+
138+
for pkg in expected:
139+
if pkg.endswith('mod'):
140+
continue
141+
del sys.modules[pkg]
142+
143+
def test_walkpackages_zipfile(self):
144+
"""Tests the same as test_walkpackages_filesys, only with a zip file."""
145+
146+
zip = 'test_walkpackages_zipfile.zip'
147+
pkg1 = 'test_walkpackages_zipfile'
148+
pkg2 = 'sub'
149+
150+
zip_file = os.path.join(self.dirname, zip)
151+
z = zipfile.ZipFile(zip_file, 'w')
152+
z.writestr(pkg2 + '/__init__.py', "")
153+
z.writestr(pkg2 + '/' + pkg1 + '/__init__.py', "")
154+
z.writestr(pkg2 + '/' + pkg1 + '/mod.py', "")
155+
z.writestr(pkg1 + '/__init__.py', "")
156+
z.writestr(pkg1 + '/' + pkg2 + '/__init__.py', "")
157+
z.writestr(pkg1 + '/' + pkg2 + '/mod.py', "")
158+
z.close()
159+
160+
sys.path.insert(0, zip_file)
161+
expected = [
162+
'sub',
163+
'sub.test_walkpackages_zipfile',
164+
'sub.test_walkpackages_zipfile.mod',
165+
'test_walkpackages_zipfile',
166+
'test_walkpackages_zipfile.sub',
167+
'test_walkpackages_zipfile.sub.mod',
168+
]
169+
actual= [e[1] for e in pkgutil.walk_packages([zip_file])]
170+
self.assertEqual(actual, expected)
171+
del sys.path[0]
172+
173+
for pkg in expected:
174+
if pkg.endswith('mod'):
175+
continue
176+
del sys.modules[pkg]
177+
178+
179+
104180
class PkgutilPEP302Tests(unittest.TestCase):
105181

106182
class MyTestLoader(object):
@@ -324,11 +400,11 @@ def check_deprecated(self):
324400

325401
def test_importer_deprecated(self):
326402
with self.check_deprecated():
327-
x = pkgutil.ImpImporter("")
403+
pkgutil.ImpImporter("")
328404

329405
def test_loader_deprecated(self):
330406
with self.check_deprecated():
331-
x = pkgutil.ImpLoader("", "", "", "")
407+
pkgutil.ImpLoader("", "", "", "")
332408

333409
def test_get_loader_avoids_emulation(self):
334410
with check_warnings() as w:

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,6 +1137,7 @@ Geoff Philbrick
11371137
Gavrie Philipson
11381138
Adrian Phillips
11391139
Christopher J. Phoenix
1140+
James Pickering
11401141
Neale Pickett
11411142
Jim St. Pierre
11421143
Dan Pierson

0 commit comments

Comments
 (0)