bpo-34206: Improve docs and test coverage for pre-init functions (#8023) · python/cpython@7c4b6a6 · GitHub
Skip to content

Commit 7c4b6a6

Browse files
ncoghlanwillingc
andauthored
bpo-34206: Improve docs and test coverage for pre-init functions (#8023)
- move the Py_Main documentation from the very high level API section to the initialization and finalization section - make it clear that it encapsulates a full Py_Initialize/Finalize cycle of its own - point out that exactly which settings will be read and applied correctly when Py_Main is called after a separate runtime initialization call is version dependent - be explicit that Py_IsInitialized can be called prior to initialization - actually test that Py_IsInitialized can be called prior to initialization - flush stdout in the embedding tests that run code so it appears in the expected order when running with "-vv" - make "-vv" on the subinterpreter embedding tests less spammy --------- Co-authored-by: Carol Willing <carolcode@willingconsulting.com>
1 parent 93b9e6b commit 7c4b6a6

7 files changed

Lines changed: 199 additions & 78 deletions

File tree

Doc/c-api/init.rst

Lines changed: 142 additions & 20 deletions

Doc/c-api/init_config.rst

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,14 +1356,13 @@ the :option:`-X` command line option.
13561356
The ``show_alloc_count`` field has been removed.
13571357
13581358
1359+
.. _init-from-config:
1360+
13591361
Initialization with PyConfig
13601362
----------------------------
13611363
1362-
Function to initialize Python:
1363-
1364-
.. c:function:: PyStatus Py_InitializeFromConfig(const PyConfig *config)
1365-
1366-
Initialize Python from *config* configuration.
1364+
Initializing the interpreter from a populated configuration struct is handled
1365+
by calling :c:func:`Py_InitializeFromConfig`.
13671366
13681367
The caller is responsible to handle exceptions (error or exit) using
13691368
:c:func:`PyStatus_Exception` and :c:func:`Py_ExitStatusException`.
@@ -1835,26 +1834,6 @@ return ``-1`` on error:
18351834
}
18361835
18371836
1838-
Py_RunMain()
1839-
============
1840-
1841-
.. c:function:: int Py_RunMain(void)
1842-
1843-
Execute the command (:c:member:`PyConfig.run_command`), the script
1844-
(:c:member:`PyConfig.run_filename`) or the module
1845-
(:c:member:`PyConfig.run_module`) specified on the command line or in the
1846-
configuration.
1847-
1848-
By default and when if :option:`-i` option is used, run the REPL.
1849-
1850-
Finally, finalizes Python and returns an exit status that can be passed to
1851-
the ``exit()`` function.
1852-
1853-
See :ref:`Python Configuration <init-python-config>` for an example of
1854-
customized Python always running in isolated mode using
1855-
:c:func:`Py_RunMain`.
1856-
1857-
18581837
Runtime Python configuration API
18591838
================================
18601839

Doc/c-api/veryhigh.rst

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,6 @@ are only passed to these functions if it is certain that they were created by
2525
the same library that the Python runtime is using.
2626

2727

28-
.. c:function:: int Py_Main(int argc, wchar_t **argv)
29-
30-
The main program for the standard interpreter. This is made available for
31-
programs which embed Python. The *argc* and *argv* parameters should be
32-
prepared exactly as those which are passed to a C program's :c:func:`main`
33-
function (converted to wchar_t according to the user's locale). It is
34-
important to note that the argument list may be modified (but the contents of
35-
the strings pointed to by the argument list are not). The return value will
36-
be ``0`` if the interpreter exits normally (i.e., without an exception),
37-
``1`` if the interpreter exits due to an exception, or ``2`` if the parameter
38-
list does not represent a valid Python command line.
39-
40-
Note that if an otherwise unhandled :exc:`SystemExit` is raised, this
41-
function will not return ``1``, but exit the process, as long as
42-
:c:member:`PyConfig.inspect` is zero.
43-
44-
45-
.. c:function:: int Py_BytesMain(int argc, char **argv)
46-
47-
Similar to :c:func:`Py_Main` but *argv* is an array of bytes strings.
48-
49-
.. versionadded:: 3.8
50-
51-
5228
.. c:function:: int PyRun_AnyFile(FILE *fp, const char *filename)
5329
5430
This is a simplified interface to :c:func:`PyRun_AnyFileExFlags` below, leaving

Lib/test/test_embed.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,8 @@ def run_repeated_init_and_subinterpreters(self):
168168
# Parse the line from the loop. The first line is the main
169169
# interpreter and the 3 afterward are subinterpreters.
170170
interp = Interp(*match.groups())
171-
if support.verbose > 1:
171+
if support.verbose > 2:
172+
# 5 lines per pass is super-spammy, so limit that to -vvv
172173
print(interp)
173174
self.assertTrue(interp.interp)
174175
self.assertTrue(interp.tstate)
@@ -279,6 +280,10 @@ def test_pre_initialization_api(self):
279280
"""
280281
env = dict(os.environ, PYTHONPATH=os.pathsep.join(sys.path))
281282
out, err = self.run_embedded_interpreter("test_pre_initialization_api", env=env)
283+
if support.verbose > 1:
284+
print()
285+
print(out)
286+
print(err)
282287
if MS_WINDOWS:
283288
expected_path = self.test_exe
284289
else:
@@ -296,6 +301,10 @@ def test_pre_initialization_sys_options(self):
296301
env['PYTHONPATH'] = os.pathsep.join(sys.path)
297302
out, err = self.run_embedded_interpreter(
298303
"test_pre_initialization_sys_options", env=env)
304+
if support.verbose > 1:
305+
print()
306+
print(out)
307+
print(err)
299308
expected_output = (
300309
"sys.warnoptions: ['once', 'module', 'default']\n"
301310
"sys._xoptions: {'not_an_option': '1', 'also_not_an_option': '2'}\n"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Added ``Py_IsInitialized`` to the list of APIs that are safe to call before
2+
the interpreter is initialized, and updated the embedding tests to cover it.
Lines changed: 8 additions & 0 deletions

0 commit comments

Comments
 (0)