Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
gh-110722: Add PYTHON_PRESITE to import a module before site.py is run#110769
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes from1 commit
3daa65a1ef282b8a201b4f192e38b89548debcc373bebb59408679d83d3f5d32e1d180File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
…cle.c
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -553,9 +553,10 @@ Miscellaneous options | ||
| container system. See also :envvar:`PYTHON_CPU_COUNT`. | ||
| If *n* is ``default``, nothing is overridden. | ||
| * :samp:`-X presite={package.module}` specifies a module that should be | ||
| imported before ``site.py`` is executed and before the :mod:`__main__` | ||
| module exists. Therefore, the imported module isn't :mod:`__main__`. | ||
| Python needs to be :ref:`built in debug mode <debug-build>` for this | ||
| option to exist. See also :envvar:`PYTHON_PRESITE`. | ||
| It also allows passing arbitrary values and retrieving them through the | ||
| :data:`sys._xoptions` dictionary. | ||
| @@ -1099,19 +1100,22 @@ Debug-mode variables | ||
| Need Python configured with the :option:`--with-trace-refs` build option. | ||
| .. envvar:: PYTHONDUMPREFSFILE | ||
| If set, Python will dump objects and reference counts still alive | ||
| after shutting down the interpreter into a file under the path given | ||
| as the value to this environment variable. | ||
| Need Python configured with the :option:`--with-trace-refs` build option. | ||
| .. versionadded:: 3.11 | ||
| .. envvar:: PYTHON_PRESITE | ||
ambv marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| If this variable is set to a module, that module will be imported | ||
| early in the interpreter lifecycle, before ``site.py`` is executed, | ||
ambv marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| and before the :mod:`__main__` module is created. Therefore, the | ||
| imported module is not treated as :mod:`__main__`. | ||
| See also the :option:`-X presite <-X>` command-line option, | ||
| which takes precedence over this variable. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1076,6 +1076,34 @@ pyinit_main_reconfigure(PyThreadState *tstate) | ||
| } | ||
| static void | ||
| run_presite(PyThreadState *tstate) | ||
| { | ||
| PyInterpreterState *interp = tstate->interp; | ||
| const PyConfig *config = _PyInterpreterState_GetConfig(interp); | ||
| if (config->run_presite) { | ||
| PyObject *presite_modname = PyUnicode_FromWideChar( | ||
| config->run_presite, | ||
| wcslen(config->run_presite) | ||
| ); | ||
| if (presite_modname == NULL) { | ||
| fprintf(stderr, "Could not convert pre-site module name to unicode\n"); | ||
| Py_DECREF(presite_modname); | ||
| } | ||
| else { | ||
| PyObject *presite = PyImport_Import(presite_modname); | ||
| if (presite == NULL) { | ||
| fprintf(stderr, "pre-site import failed:\n"); | ||
| _PyErr_Print(tstate); | ||
| } | ||
| Py_XDECREF(presite); | ||
| Py_DECREF(presite_modname); | ||
| } | ||
| } | ||
ambv marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| } | ||
| static PyStatus | ||
| init_interp_main(PyThreadState *tstate) | ||
| { | ||
| @@ -1158,25 +1186,7 @@ init_interp_main(PyThreadState *tstate) | ||
| } | ||
| #ifdef Py_DEBUG | ||
| run_presite(tstate); | ||
| #endif | ||
| status = add_main_module(interp); | ||