Skip to content

Commit

Permalink
Flip conditionals around package finders in pkg_resources (#3685)
Browse files Browse the repository at this point in the history
- In Python 3.3+ importlib.machinery.FileFinder is always present,
  therefore we no longer need to keep the conditional
- In Python 3.12+ pkgutil.ImpImporter is removed, so we need a conditional
  to avoid using it directly.
  • Loading branch information
abravalheri committed Jan 20, 2023
2 parents d348fdf + 87518dd commit e03f883
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
1 change: 1 addition & 0 deletions changelog.d/3685.change.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix improper usage of deprecated/removed ``pkgutil`` APIs in Python 3.12+.
14 changes: 7 additions & 7 deletions pkg_resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2187,10 +2187,10 @@ def resolve_egg_link(path):
return next(dist_groups, ())


register_finder(pkgutil.ImpImporter, find_on_path)
if hasattr(pkgutil, 'ImpImporter'):
register_finder(pkgutil.ImpImporter, find_on_path)

if hasattr(importlib_machinery, 'FileFinder'):
register_finder(importlib_machinery.FileFinder, find_on_path)
register_finder(importlib_machinery.FileFinder, find_on_path)

_declare_state('dict', _namespace_handlers={})
_declare_state('dict', _namespace_packages={})
Expand Down Expand Up @@ -2344,11 +2344,11 @@ def file_ns_handler(importer, path_item, packageName, module):
return subpath


register_namespace_handler(pkgutil.ImpImporter, file_ns_handler)
register_namespace_handler(zipimport.zipimporter, file_ns_handler)
if hasattr(pkgutil, 'ImpImporter'):
register_namespace_handler(pkgutil.ImpImporter, file_ns_handler)

if hasattr(importlib_machinery, 'FileFinder'):
register_namespace_handler(importlib_machinery.FileFinder, file_ns_handler)
register_namespace_handler(zipimport.zipimporter, file_ns_handler)
register_namespace_handler(importlib_machinery.FileFinder, file_ns_handler)


def null_ns_handler(importer, path_item, packageName, module):
Expand Down

0 comments on commit e03f883

Please sign in to comment.