Description
Bug report
The standard library module pkgutil.py
performs unsafe imports of discovered packages, which can lead to a SystemExit
if triggered by an import.
When pkgutil.walk_packages()
attempts to import each package (to scan for subpackages), a call to sys.exit()
inside the package will trigger an exit of the interpreter. This is in contrast to pydoc
, which was made safe with its own pydoc.safeimport()
. Attempting to load the documentation for a module that calls sys.exit()
will not exit the interpreter.
pkgutil
should be made safe in the same manner.
To Reproduce
Create a directory my_pkg
and a file my_pkg/__init__.py
containing:
# my_pkg/__init__.py
import sys
try:
import does_not_exist
except ImportError:
sys.exit(1)
Create a file in the current directory, repro.py
, or simply start a REPL and enter:
# repro.py
import pkgutil
import sys
sys.path.insert(0, '.')
def err_handler(modname):
print(f"Error scanning {modname!r}")
pkgs = [pkgutil.walk_packages(onerror=err_handler)]
Result
The interpreter will exit as soon as it attempts to examine my_pkg
.
Expected result
"Error scanning 'my_pkg'" is printed on the console, and the scan continues.
Background
See the recent discussion regarding package-scanning and sys.exit.
Your environment
- CPython versions tested on:
Python 3.11.2 (main, Feb 8 2023, 00:00:00) [GCC 12.2.1 20221121 (Red Hat 12.2.1-4)] on linux
- Operating system and architecture: Fedora 37 x86_64