diff options
author | Miguel Brito <5544985+miguendes@users.noreply.github.com> | 2021-05-11 23:27:22 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-05-11 23:27:22 (GMT) |
commit | e9d7f88d5643f7e6387bf994c130503766d7eb92 (patch) | |
tree | e42e6c812aa4f7b16ecb79e6dbda897d1ed736e2 /Lib/test/test_pkgutil.py | |
parent | 8563a7052ccd98e6a381d361664ce567afd5eb6e (diff) | |
download | cpython-e9d7f88d5643f7e6387bf994c130503766d7eb92.zip cpython-e9d7f88d5643f7e6387bf994c130503766d7eb92.tar.gz cpython-e9d7f88d5643f7e6387bf994c130503766d7eb92.tar.bz2 |
bpo-44061: Fix pkgutil.iter_modules regression when passed a pathlib.Path object (GH-25964)
Diffstat (limited to 'Lib/test/test_pkgutil.py')
-rw-r--r-- | Lib/test/test_pkgutil.py | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/Lib/test/test_pkgutil.py b/Lib/test/test_pkgutil.py index 6e3618f..3c29080 100644 --- a/Lib/test/test_pkgutil.py +++ b/Lib/test/test_pkgutil.py @@ -1,3 +1,4 @@ +from pathlib import Path from test.support import run_unittest from test.support.import_helper import unload, CleanImport from test.support.warnings_helper import check_warnings @@ -92,6 +93,45 @@ class PkgutilTests(unittest.TestCase): del sys.modules[pkg] + def test_issue44061_iter_modules(self): + #see: issue44061 + zip = 'test_getdata_zipfile.zip' + pkg = 'test_getdata_zipfile' + + # Include a LF and a CRLF, to test that binary data is read back + RESOURCE_DATA = b'Hello, world!\nSecond line\r\nThird line' + + # Make a package with some resources + zip_file = os.path.join(self.dirname, zip) + z = zipfile.ZipFile(zip_file, 'w') + + # Empty init.py + z.writestr(pkg + '/__init__.py', "") + # Resource files, res.txt + z.writestr(pkg + '/res.txt', RESOURCE_DATA) + z.close() + + # Check we can read the resources + sys.path.insert(0, zip_file) + try: + res = pkgutil.get_data(pkg, 'res.txt') + self.assertEqual(res, RESOURCE_DATA) + + # make sure iter_modules accepts Path objects + names = [] + for moduleinfo in pkgutil.iter_modules([Path(zip_file)]): + self.assertIsInstance(moduleinfo, pkgutil.ModuleInfo) + names.append(moduleinfo.name) + self.assertEqual(names, [pkg]) + finally: + del sys.path[0] + sys.modules.pop(pkg, None) + + # assert path must be None or list of paths + expected_msg = "path must be None or list of paths to look for modules in" + with self.assertRaisesRegex(ValueError, expected_msg): + list(pkgutil.iter_modules("invalid_path")) + def test_unreadable_dir_on_syspath(self): # issue7367 - walk_packages failed if unreadable dir on sys.path package_name = "unreadable_package" @@ -574,6 +614,12 @@ class ImportlibMigrationTests(unittest.TestCase): self.assertIsNone(pkgutil.get_importer("*??")) self.assertEqual(len(w.warnings), 0) + def test_issue44061(self): + try: + pkgutil.get_importer(Path("/home")) + except AttributeError: + self.fail("Unexpected AttributeError when calling get_importer") + def test_iter_importers_avoids_emulation(self): with check_warnings() as w: for importer in pkgutil.iter_importers(): pass |