Skip to content

Commit 4fedf3e

Browse files
committed
Fix pkgutil.iter_modules regression
1 parent bf99b71 commit 4fedf3e

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

Lib/pkgutil.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,7 @@ def get_importer(path_item):
413413
The cache (or part of it) can be cleared manually if a
414414
rescan of sys.path_hooks is necessary.
415415
"""
416+
path_item = os.fsdecode(path_item)
416417
try:
417418
importer = sys.path_importer_cache[path_item]
418419
except KeyError:

Lib/test/test_pkgutil.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from pathlib import Path
12
from test.support import run_unittest
23
from test.support.import_helper import unload, CleanImport
34
from test.support.warnings_helper import check_warnings
@@ -92,6 +93,15 @@ def test_getdata_zipfile(self):
9293

9394
del sys.modules[pkg]
9495

96+
def test_iter_modules(self):
97+
#see: issue44061
98+
actual = list(pkgutil.iter_modules([Path("/home")], 'somepackage.somesubpackage'))
99+
self.assertListEqual([], actual)
100+
101+
expected_msg = "path must be None or list of paths to look for modules in"
102+
with self.assertRaisesRegex(ValueError, expected_msg):
103+
list(pkgutil.iter_modules("invalid_path"))
104+
95105
def test_unreadable_dir_on_syspath(self):
96106
# issue7367 - walk_packages failed if unreadable dir on sys.path
97107
package_name = "unreadable_package"
@@ -574,6 +584,12 @@ def test_get_importer_avoids_emulation(self):
574584
self.assertIsNone(pkgutil.get_importer("*??"))
575585
self.assertEqual(len(w.warnings), 0)
576586

587+
def test_issue44061(self):
588+
try:
589+
pkgutil.get_importer(Path("/home"))
590+
except AttributeError:
591+
self.fail("Unexpected AttributeError when calling get_importer")
592+
577593
def test_iter_importers_avoids_emulation(self):
578594
with check_warnings() as w:
579595
for importer in pkgutil.iter_importers(): pass
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix regression when calling :func:`pkgutil.iter_modules` with a list of
2+
:class:`pathlib.Path` objects

0 commit comments

Comments
 (0)