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
site.main() does not work on Python 3.6 and superior if PYTHONSTARTUP is set #74353
Comments
Apparently, "import site; site.main()" does not seem to work anymore on Python 3.6 and superior. The reason is a change on the behavior of "os.path.abspath(None)". Before Python 3.6, it used to report an AttributeError which is properly caught inside "site.abs_paths" (see: https://github.com/python/cpython/blob/master/Lib/site.py#L99), making it ignore __main__, one of sys.modules, which has __file__ and __cached__ set to None. With Python 3.6 and superior, os.path.abspath(None) reports a TypeError, which makes calling "site.main()" raise an exception and stop. How to reproduce: On python 3.6 or superior, do "import site; site.main()". Expected behavior: Exception is properly caught and treated inside "site.abs_paths", ignoring modules in which __file__ and/or __cached__ are set to None. |
More information: to reproduce the problem, don't use Python's "-c" command-line option. In this case "__main__" won't be inside sys.modules which mitigates the issue. |
After further investigation, the issue can only be reproduced iff the user sets PYTHONSTARTUP which triggers "__main__" to appear in sys.modules and the problem to occur. |
ref to other projects that hit this issue. https://gitlab.idiap.ch/bob/bob.buildout/issues/15 |
Lines 391 to 399 in f822549
__file__ and __cached__ are added here. And, Lines 441 to 442 in f822549
Only __file__ is removed here. I feel __cached__ should be removed too. On the other hand, if None is valid value for __cached__, site.abs_paths() should ignore it. https://www.python.org/dev/peps/pep-3147/#file
It seems it's valid, and recommended for some cases. So PR-6731 looks OK. |
As I noted in the discussion on PR 6731, I think there should be a test for this so we don't break it again. But, since it seems that the problem has affected a number of users and projects and since the fix is easy and easily testable manually, I decided to merge the PR for 3.7.0rc1 and 3.6.6rc1. I am leaving the issue open for someone to supply a PR with a test case and for discussion of PR 7415. Thank you all! |
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: