-
-
Notifications
You must be signed in to change notification settings - Fork 31.6k
site ignores ImportError when running sitecustomize and usercustomize #70287
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
Comments
If the code of sitecustomize raises an ImportError because the requested module doesn't exist, sitecustomize exception is silently ignored because site.py uses "try: import sitecustomize except ImportError: pass". It's possible to log a warning since ImportError now has a name attribute since Python 3.3. There is a similar issue on usercustomize. Attached patch fixes the issue. |
This is a change in semantics. It might be better to log an ImportWarning when the import fails and keep the current semantics (and be careful about importing warnings). |
Example of the bug: ---- $ cat Lib/sitecustomize.py
print("before")
import xxx
print("after") haypo@smithers$ ./python -q -c pass The line "after" is not executed and *no* error nor warning is raised. With the patch: ---- $ ./python -q -c pass
before
Error in sitecustomize; set PYTHONVERBOSE for traceback:
ImportError: No module named 'xxx' $ PYTHONVERBOSE=1 ./python -q -c pass
(...)
# /home/haypo/prog/python/3.5/Lib/__pycache__/sitecustomize.cpython-35.pyc matches /home/haypo/prog/python/3.5/Lib/sitecustomize.py
# code object from '/home/haypo/prog/python/3.5/Lib/__pycache__/sitecustomize.cpython-35.pyc'
before
Traceback (most recent call last):
File "/home/haypo/prog/python/3.5/Lib/site.py", line 508, in execsitecustomize
import sitecustomize
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 662, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "/home/haypo/prog/python/3.5/Lib/sitecustomize.py", line 2, in <module>
import xxx
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked
ImportError: No module named 'xxx'
# destroy sitecustomize
(...) Brett Cannon: "This is a change in semantics." Can you please elaborate? Even if importing sitecustomize changes, Python continue its execution. Only a warning is emited. Brett Cannon: "It might be better to log an ImportWarning when the import fails and keep the current semantics (and be careful about importing warnings)." With my patch, a message "Error in sitecustomize; set PYTHONVERBOSE for traceback:" is logged. I used the same behaviour then the code to handle site import. Note: I found this issue when I worked on the PEP-511 to register a code transformer at startup. The bug is really annoying: the code transformers may or may not be registered depending if required modules can be important, I expect an error (or at least a warning). |
@brett: Ping? I'm going to push the fix to Python 3.6. Tell me if you are ok to backport the change to Python 2.7 and 3.5 too. |
Semantics are fine (I initially misread what you wanted to do; sorry). And I wouldn't backport since it's an enhancement and not a bugfix. |
New changeset c873a479a6a3 by Victor Stinner in branch 'default': |
Brett: "Semantics are fine (I initially misread what you wanted to do; sorry)." Ok, I pushed my change. Brett: "And I wouldn't backport since it's an enhancement and not a bugfix." Ok, fine. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: