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

Skip to content

Commit 3f72821

Browse files
[3.12] gh-59022: Added tests for pkgutil.extend_path (GH-121673) (GH-121950)
This adds tests for the documented behaviour of `pkgutil.extend_path` regarding different argument types as well as for `*.pkg` files. (cherry picked from commit 8f25321) Co-authored-by: Andreas Stocker <[email protected]>
1 parent e80497c commit 3f72821

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

Doc/library/pkgutil.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ support.
3434
*name* argument. This feature is similar to :file:`\*.pth` files (see the
3535
:mod:`site` module for more information), except that it doesn't special-case
3636
lines starting with ``import``. A :file:`\*.pkg` file is trusted at face
37-
value: apart from checking for duplicates, all entries found in a
38-
:file:`\*.pkg` file are added to the path, regardless of whether they exist
39-
on the filesystem. (This is a feature.)
37+
value: apart from skipping blank lines and ignoring comments, all entries
38+
found in a :file:`\*.pkg` file are added to the path, regardless of whether
39+
they exist on the filesystem (this is a feature).
4040

4141
If the input path is not a list (as is the case for frozen packages) it is
4242
returned unchanged. The input path is not modified; an extended copy is

Lib/test/test_pkgutil.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,43 @@ def test_mixed_namespace(self):
522522
del sys.modules['foo.bar']
523523
del sys.modules['foo.baz']
524524

525-
# XXX: test .pkg files
525+
526+
def test_extend_path_argument_types(self):
527+
pkgname = 'foo'
528+
dirname_0 = self.create_init(pkgname)
529+
530+
# If the input path is not a list it is returned unchanged
531+
self.assertEqual('notalist', pkgutil.extend_path('notalist', 'foo'))
532+
self.assertEqual(('not', 'a', 'list'), pkgutil.extend_path(('not', 'a', 'list'), 'foo'))
533+
self.assertEqual(123, pkgutil.extend_path(123, 'foo'))
534+
self.assertEqual(None, pkgutil.extend_path(None, 'foo'))
535+
536+
# Cleanup
537+
shutil.rmtree(dirname_0)
538+
del sys.path[0]
539+
540+
541+
def test_extend_path_pkg_files(self):
542+
pkgname = 'foo'
543+
dirname_0 = self.create_init(pkgname)
544+
545+
with open(os.path.join(dirname_0, 'bar.pkg'), 'w') as pkg_file:
546+
pkg_file.write('\n'.join([
547+
'baz',
548+
'/foo/bar/baz',
549+
'',
550+
'#comment'
551+
]))
552+
553+
extended_paths = pkgutil.extend_path(sys.path, 'bar')
554+
555+
self.assertEqual(extended_paths[:-2], sys.path)
556+
self.assertEqual(extended_paths[-2], 'baz')
557+
self.assertEqual(extended_paths[-1], '/foo/bar/baz')
558+
559+
# Cleanup
560+
shutil.rmtree(dirname_0)
561+
del sys.path[0]
526562

527563

528564
class NestedNamespacePackageTest(unittest.TestCase):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add tests for :func:`pkgutil.extend_path`. Patch by Andreas Stocker.

0 commit comments

Comments
 (0)