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
…e site.py is runThis is only available --with-pydebug.
- 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 |
|---|---|---|
| @@ -1100,6 +1100,7 @@ PyConfig | ||
| Set by the :option:`-X pycache_prefix=PATH <-X>` command line option and | ||
| the :envvar:`PYTHONPYCACHEPREFIX` environment variable. | ||
| The command-line option takes precedence. | ||
| If ``NULL``, :data:`sys.pycache_prefix` is set to ``None``. | ||
| @@ -1143,6 +1144,20 @@ PyConfig | ||
| Default: ``NULL``. | ||
| .. c:member:: wchar_t* run_presite | ||
ambv marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| ``package.module`` path to module that should be imported before | ||
| ``site.py`` is run. | ||
| Set by the :option:`-X presite=package.module <-X>` command-line | ||
| option and the :envvar:`PYTHON_PRESITE` environment variable. | ||
| The command-line option takes precedence. | ||
| Need a :ref:`debug build of Python <debug-build>` (the ``Py_DEBUG`` macro | ||
ambv marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| must be defined). | ||
| Default: ``NULL``. | ||
| .. c:member:: int show_ref_count | ||
| Show total reference count at exit (excluding immortal objects)? | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -117,6 +117,9 @@ static const PyConfigSpec PYCONFIG_SPEC[] = { | ||
| SPEC(_is_python_build, UINT), | ||
| #ifdef Py_STATS | ||
| SPEC(_pystats, UINT), | ||
| #endif | ||
| #ifdef Py_DEBUG | ||
| SPEC(run_presite, WSTR_OPT), | ||
| #endif | ||
| {NULL, 0, 0}, | ||
| }; | ||
| @@ -241,6 +244,11 @@ The following implementation-specific options are available:\n\ | ||
| \n\ | ||
| -X pystats: Enable pystats collection at startup." | ||
| #endif | ||
| #ifdef Py_DEBUG | ||
| "\n\ | ||
| \n\ | ||
| -X presite=package.module: import this module before site.py is run." | ||
| #endif | ||
| ; | ||
| /* Envvars that don't have equivalent command-line options are listed first */ | ||
| @@ -790,6 +798,9 @@ PyConfig_Clear(PyConfig *config) | ||
| CLEAR(config->run_module); | ||
| CLEAR(config->run_filename); | ||
| CLEAR(config->check_hash_pycs_mode); | ||
| #ifdef Py_DEBUG | ||
| CLEAR(config->run_presite); | ||
| #endif | ||
| _PyWideStringList_Clear(&config->orig_argv); | ||
| #undef CLEAR | ||
| @@ -1806,6 +1817,36 @@ config_init_pycache_prefix(PyConfig *config) | ||
| } | ||
| #ifdef Py_DEBUG | ||
| static PyStatus | ||
| config_init_run_presite(PyConfig *config) | ||
| { | ||
| assert(config->run_presite == NULL); | ||
| const wchar_t *xoption = config_get_xoption(config, L"presite"); | ||
| if (xoption) { | ||
| const wchar_t *sep = wcschr(xoption, L'='); | ||
| if (sep && wcslen(sep) > 1) { | ||
| config->run_presite = _PyMem_RawWcsdup(sep + 1); | ||
| if (config->run_presite == NULL) { | ||
| return _PyStatus_NO_MEMORY(); | ||
| } | ||
| } | ||
| else { | ||
| // PYTHON_PRESITE env var ignored | ||
| // if "-X presite=" option is used | ||
| config->run_presite = NULL; | ||
| } | ||
| return _PyStatus_OK(); | ||
| } | ||
| return CONFIG_GET_ENV_DUP(config, &config->run_presite, | ||
| L"PYTHON_PRESITE", | ||
| "PYTHON_PRESITE"); | ||
| } | ||
| #endif | ||
| static PyStatus | ||
| config_read_complex_options(PyConfig *config) | ||
| { | ||
| @@ -1861,6 +1902,16 @@ config_read_complex_options(PyConfig *config) | ||
| return status; | ||
| } | ||
| } | ||
| #ifdef Py_DEBUG | ||
| if (config->run_presite== NULL) { | ||
ambv marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| status = config_init_run_presite(config); | ||
| if (_PyStatus_EXCEPTION(status)) { | ||
| return status; | ||
| } | ||
| } | ||
| #endif | ||
| return _PyStatus_OK(); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1157,6 +1157,27 @@ init_interp_main(PyThreadState *tstate) | ||
| return status; | ||
| } | ||
| #ifdef Py_DEBUG | ||
| if (config->run_presite) { | ||
ambv marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| PyObject *presite_modname = PyUnicode_FromWideChar( | ||
| config->run_presite, | ||
| wcslen(config->run_presite) | ||
| ); | ||
| if (presite_modname == NULL) { | ||
| fprintf(stderr, "Could not convert module name to unicode\n"); | ||
ambv marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| Py_DECREF(presite_modname); | ||
| } | ||
| else { | ||
| PyObject *presite = PyImport_Import(presite_modname); | ||
| if (presite == NULL) { | ||
| fprintf(stderr, "pre-site import failed; traceback:\n"); | ||
| _PyErr_Print(tstate); | ||
| } | ||
| Py_XDECREF(presite); | ||
| } | ||
| } | ||
| #endif | ||
| status = add_main_module(interp); | ||
| if (_PyStatus_EXCEPTION(status)) { | ||
| return status; | ||