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
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
10 commits Select commitHold shift + click to select a range
3daa65a gh-110722: Add PYTHON_PRESITE=package.module to import a module befor…
ambv1ef282b Add Blurb and fix a refleak before anybody notices
ambv8a201b4 Fix versionadded
ambvf192e38 Merge branch 'main' into gh-110722-presite
ambvb89548d Add test, refine docs, add whatsnew, separate run_presite in pylifecy…
ambvebcc373 PEP 7
ambvbebb594 Only define the static function --with-pydebug
ambv08679d8 Fix visual indentation
ambv3d3f5d3 Mention PYTHON_PRESITE needs a pydebug build
ambv2e1d180 Address Victor's nits
ambvFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -716,7 +716,7 @@ PyConfig | ||
| Set to ``1`` by the :envvar:`PYTHONDUMPREFS` environment variable. | ||
| Needs a special build of Python with the ``Py_TRACE_REFS`` macro defined: | ||
| see the :option:`configure --with-trace-refs option <--with-trace-refs>`. | ||
| Default: ``0``. | ||
| @@ -1048,7 +1048,7 @@ PyConfig | ||
| Incremented by the :option:`-d` command line option. Set to the | ||
| :envvar:`PYTHONDEBUG` environment variable value. | ||
| Needs a :ref:`debug build of Python <debug-build>` (the ``Py_DEBUG`` macro | ||
| must be defined). | ||
| Default: ``0``. | ||
| @@ -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,13 +1144,27 @@ 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. | ||
| Needs a :ref:`debug build of Python <debug-build>` (the ``Py_DEBUG`` macro | ||
| must be defined). | ||
| Default: ``NULL``. | ||
| .. c:member:: int show_ref_count | ||
| Show total reference count at exit (excluding immortal objects)? | ||
| Set to ``1`` by :option:`-X showrefcount <-X>` command line option. | ||
| Needs a :ref:`debug build of Python <debug-build>` (the ``Py_REF_DEBUG`` | ||
| macro must be defined). | ||
| Default: ``0``. | ||
37 changes: 33 additions & 4 deletionsDoc/using/cmdline.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -552,6 +552,12 @@ Miscellaneous options | ||
| This option may be useful for users who need to limit CPU resources of a | ||
| 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 the :mod:`site` module is executed and before the | ||
| :mod:`__main__` module exists. Therefore, the imported module isn't | ||
| :mod:`__main__`. This can be used to execute code early during Python | ||
| initialization. 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. | ||
| @@ -602,6 +608,9 @@ Miscellaneous options | ||
| .. versionadded:: 3.13 | ||
| The ``-X cpu_count`` option. | ||
| .. versionadded:: 3.13 | ||
| The ``-X presite`` option. | ||
| Options you shouldn't use | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| @@ -1090,13 +1099,33 @@ Debug-mode variables | ||
| If set, Python will dump objects and reference counts still alive after | ||
| shutting down the interpreter. | ||
| Needs 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. | ||
| Needs 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 the :mod:`site` module is | ||
| executed, and before the :mod:`__main__` module is created. | ||
| Therefore, the imported module is not treated as :mod:`__main__`. | ||
| This can be used to execute code early during Python initialization. | ||
| To import a submodule, use ``package.module`` as the value, like in | ||
| an import statement. | ||
| See also the :option:`-X presite <-X>` command-line option, | ||
| which takes precedence over this variable. | ||
| Needs Python configured with the :option:`--with-pydebug` build option. | ||
| .. versionadded:: 3.13 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletionsLib/test/test_embed.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletionsMisc/NEWS.d/next/Core and Builtins/2023-10-12-17-15-23.gh-issue-110722.sjMwQe.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Add :envvar:`PYTHON_PRESITE=package.module` to import a module early in the | ||
| interpreter lifecycle before ``site.py`` is executed. Python needs to be | ||
| :ref:`built in debug mode <debug-build>` for this option to exist. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1076,6 +1076,38 @@ pyinit_main_reconfigure(PyThreadState *tstate) | ||
| } | ||
| #ifdef Py_DEBUG | ||
| static void | ||
| run_presite(PyThreadState *tstate) | ||
| { | ||
| PyInterpreterState *interp = tstate->interp; | ||
| const PyConfig *config = _PyInterpreterState_GetConfig(interp); | ||
| if (!config->run_presite) { | ||
| return; | ||
| } | ||
| 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. | ||
| #endif | ||
| static PyStatus | ||
| init_interp_main(PyThreadState *tstate) | ||
| { | ||
| @@ -1157,6 +1189,10 @@ init_interp_main(PyThreadState *tstate) | ||
| return status; | ||
| } | ||
| #ifdef Py_DEBUG | ||
| run_presite(tstate); | ||
| #endif | ||
| status = add_main_module(interp); | ||
| if (_PyStatus_EXCEPTION(status)) { | ||
| return status; | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.