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

Skip to content

gh-59022: Added tests for pkgutil.extend_path (#59022) #121673

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Doc/library/pkgutil.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ support.
*name* argument. This feature is similar to :file:`\*.pth` files (see the
:mod:`site` module for more information), except that it doesn't special-case
lines starting with ``import``. A :file:`\*.pkg` file is trusted at face
value: apart from checking for duplicates, all entries found in a
:file:`\*.pkg` file are added to the path, regardless of whether they exist
on the filesystem. (This is a feature.)
value: apart from skipping blank lines and ignoring comments, all entries
found in a :file:`\*.pkg` file are added to the path, regardless of whether
they exist on the filesystem (this is a feature).

If the input path is not a list (as is the case for frozen packages) it is
returned unchanged. The input path is not modified; an extended copy is
Expand Down
38 changes: 37 additions & 1 deletion Lib/test/test_pkgutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,43 @@ def test_mixed_namespace(self):
del sys.modules['foo.bar']
del sys.modules['foo.baz']

# XXX: test .pkg files

def test_extend_path_argument_types(self):
pkgname = 'foo'
dirname_0 = self.create_init(pkgname)

# If the input path is not a list it is returned unchanged
self.assertEqual('notalist', pkgutil.extend_path('notalist', 'foo'))
self.assertEqual(('not', 'a', 'list'), pkgutil.extend_path(('not', 'a', 'list'), 'foo'))
self.assertEqual(123, pkgutil.extend_path(123, 'foo'))
self.assertEqual(None, pkgutil.extend_path(None, 'foo'))

# Cleanup
shutil.rmtree(dirname_0)
del sys.path[0]


def test_extend_path_pkg_files(self):
pkgname = 'foo'
dirname_0 = self.create_init(pkgname)

with open(os.path.join(dirname_0, 'bar.pkg'), 'w') as pkg_file:
pkg_file.write('\n'.join([
'baz',
'/foo/bar/baz',
'',
'#comment'
]))

extended_paths = pkgutil.extend_path(sys.path, 'bar')

self.assertEqual(extended_paths[:-2], sys.path)
self.assertEqual(extended_paths[-2], 'baz')
self.assertEqual(extended_paths[-1], '/foo/bar/baz')
Comment on lines +556 to +557
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also

  • check that extended_paths[:-2] is the same as the original path
  • test that duplicates are removed.

The documentation doesn't make it clear that comments are ignored. Could you note that in the docs?

Perhaps change:

apart from checking for duplicates, all entries ...

to:

blank lines, comments and duplicates are ignored; all other entries ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turns out that extend_path does happily insert duplicates in the list. To not introduce a potentially breaking change, I've adjusted the documentation accordingly. Also, the suggested check that extended_paths[:-2] equals the original path has been added.


# Cleanup
shutil.rmtree(dirname_0)
del sys.path[0]


class NestedNamespacePackageTest(unittest.TestCase):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add tests for :func:`pkgutil.extend_path`. Patch by Andreas Stocker.
Loading