Skip to content

Commit

Permalink
pre-safe-import-hooks: create six.move runtime package only if six is…
Browse files Browse the repository at this point in the history
… available

If the pre-safe-import hook for `six` (or one of its vendored
versions) cannot import `six` (or the corresponding vendored
version of it), do not create the `six.move` run-time
package in the modulegraph.
  • Loading branch information
rokm committed Dec 4, 2023
1 parent ac91826 commit 8c4d099
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ def real_to_six_module_name():
try:
import setuptools.extern.six as six
except ImportError:
return {} # unavailable
return None # unavailable

return {
moved.mod: 'setuptools.extern.six.moves.' + moved.name
for moved in six._moved_attributes if isinstance(moved, (six.MovedModule, six.MovedAttribute))
}

api.add_runtime_package(api.module_name)
for real_module_name, six_module_name in real_to_six_module_name.items():
api.add_alias_module(real_module_name, six_module_name)
if real_to_six_module_name is not None:
api.add_runtime_package(api.module_name)
for real_module_name, six_module_name in real_to_six_module_name.items():
api.add_alias_module(real_module_name, six_module_name)
9 changes: 5 additions & 4 deletions PyInstaller/hooks/pre_safe_import_module/hook-six.moves.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def real_to_six_module_name():
try:
import six
except ImportError:
return {} # unavailable
return None # unavailable

# Iterate over the "six._moved_attributes" list rather than the "six._importer.known_modules" dictionary, as
# "urllib"-specific moved modules are overwritten in the latter with unhelpful "LazyModule" objects. If this is
Expand All @@ -56,6 +56,7 @@ def real_to_six_module_name():
# * Attributes imported from packages could be submodules. To disambiguate non-ignorable submodules from ignorable
# non-submodules (e.g., classes, variables), ModuleGraph first attempts to import these attributes as submodules.
# This is exactly what we want.
api.add_runtime_package(api.module_name)
for real_module_name, six_module_name in real_to_six_module_name.items():
api.add_alias_module(real_module_name, six_module_name)
if real_to_six_module_name is not None:
api.add_runtime_package(api.module_name)
for real_module_name, six_module_name in real_to_six_module_name.items():
api.add_alias_module(real_module_name, six_module_name)
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ def real_to_six_module_name():
try:
import urllib3.packages.six as six
except ImportError:
return {} # unavailable
return None # unavailable

return {
moved.mod: 'urllib3.packages.six.moves.' + moved.name
for moved in six._moved_attributes if isinstance(moved, (six.MovedModule, six.MovedAttribute))
}

api.add_runtime_package(api.module_name)
for real_module_name, six_module_name in real_to_six_module_name.items():
api.add_alias_module(real_module_name, six_module_name)
if real_to_six_module_name is not None:
api.add_runtime_package(api.module_name)
for real_module_name, six_module_name in real_to_six_module_name.items():
api.add_alias_module(real_module_name, six_module_name)

0 comments on commit 8c4d099

Please sign in to comment.