|
1 | 1 | from test.support import run_unittest, unload, check_warnings, CleanImport
|
| 2 | +from pathlib import Path |
2 | 3 | import unittest
|
3 | 4 | import sys
|
4 | 5 | import importlib
|
@@ -90,6 +91,45 @@ def test_getdata_zipfile(self):
|
90 | 91 |
|
91 | 92 | del sys.modules[pkg]
|
92 | 93 |
|
| 94 | + def test_issue44061_iter_modules(self): |
| 95 | + #see: issue44061 |
| 96 | + zip = 'test_getdata_zipfile.zip' |
| 97 | + pkg = 'test_getdata_zipfile' |
| 98 | + |
| 99 | + # Include a LF and a CRLF, to test that binary data is read back |
| 100 | + RESOURCE_DATA = b'Hello, world!\nSecond line\r\nThird line' |
| 101 | + |
| 102 | + # Make a package with some resources |
| 103 | + zip_file = os.path.join(self.dirname, zip) |
| 104 | + z = zipfile.ZipFile(zip_file, 'w') |
| 105 | + |
| 106 | + # Empty init.py |
| 107 | + z.writestr(pkg + '/__init__.py', "") |
| 108 | + # Resource files, res.txt |
| 109 | + z.writestr(pkg + '/res.txt', RESOURCE_DATA) |
| 110 | + z.close() |
| 111 | + |
| 112 | + # Check we can read the resources |
| 113 | + sys.path.insert(0, zip_file) |
| 114 | + try: |
| 115 | + res = pkgutil.get_data(pkg, 'res.txt') |
| 116 | + self.assertEqual(res, RESOURCE_DATA) |
| 117 | + |
| 118 | + # make sure iter_modules accepts Path objects |
| 119 | + names = [] |
| 120 | + for moduleinfo in pkgutil.iter_modules([Path(zip_file)]): |
| 121 | + self.assertIsInstance(moduleinfo, pkgutil.ModuleInfo) |
| 122 | + names.append(moduleinfo.name) |
| 123 | + self.assertEqual(names, [pkg]) |
| 124 | + finally: |
| 125 | + del sys.path[0] |
| 126 | + sys.modules.pop(pkg, None) |
| 127 | + |
| 128 | + # assert path must be None or list of paths |
| 129 | + expected_msg = "path must be None or list of paths to look for modules in" |
| 130 | + with self.assertRaisesRegex(ValueError, expected_msg): |
| 131 | + list(pkgutil.iter_modules("invalid_path")) |
| 132 | + |
93 | 133 | def test_unreadable_dir_on_syspath(self):
|
94 | 134 | # issue7367 - walk_packages failed if unreadable dir on sys.path
|
95 | 135 | package_name = "unreadable_package"
|
@@ -480,6 +520,12 @@ def test_get_importer_avoids_emulation(self):
|
480 | 520 | self.assertIsNone(pkgutil.get_importer("*??"))
|
481 | 521 | self.assertEqual(len(w.warnings), 0)
|
482 | 522 |
|
| 523 | + def test_issue44061(self): |
| 524 | + try: |
| 525 | + pkgutil.get_importer(Path("/home")) |
| 526 | + except AttributeError: |
| 527 | + self.fail("Unexpected AttributeError when calling get_importer") |
| 528 | + |
483 | 529 | def test_iter_importers_avoids_emulation(self):
|
484 | 530 | with check_warnings() as w:
|
485 | 531 | for importer in pkgutil.iter_importers(): pass
|
|
0 commit comments