Skip to content

Commit 2e7fe7b

Browse files
Merge branch 'main' into locale-nl_langinfo-alt_digits
2 parents 157c23e + 19984fe commit 2e7fe7b

17 files changed

+399
-163
lines changed

Doc/c-api/init.rst

Lines changed: 142 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
Initialization, Finalization, and Threads
88
*****************************************
99

10-
See also the :ref:`Python Initialization Configuration <init-config>`.
10+
See :ref:`Python Initialization Configuration <init-config>` for details
11+
on how to configure the interpreter prior to initialization.
1112

1213
.. _pre-init-safe:
1314

@@ -21,6 +22,15 @@ a few functions and the :ref:`global configuration variables
2122

2223
The following functions can be safely called before Python is initialized:
2324

25+
* Functions that initialize the interpreter:
26+
27+
* :c:func:`Py_Initialize`
28+
* :c:func:`Py_InitializeEx`
29+
* :c:func:`Py_InitializeFromConfig`
30+
* :c:func:`Py_BytesMain`
31+
* :c:func:`Py_Main`
32+
* the runtime pre-initialization functions covered in :ref:`init-config`
33+
2434
* Configuration functions:
2535

2636
* :c:func:`PyImport_AppendInittab`
@@ -32,6 +42,7 @@ The following functions can be safely called before Python is initialized:
3242
* :c:func:`Py_SetProgramName`
3343
* :c:func:`Py_SetPythonHome`
3444
* :c:func:`PySys_ResetWarnOptions`
45+
* the configuration functions covered in :ref:`init-config`
3546

3647
* Informative functions:
3748

@@ -43,10 +54,12 @@ The following functions can be safely called before Python is initialized:
4354
* :c:func:`Py_GetCopyright`
4455
* :c:func:`Py_GetPlatform`
4556
* :c:func:`Py_GetVersion`
57+
* :c:func:`Py_IsInitialized`
4658

4759
* Utilities:
4860

4961
* :c:func:`Py_DecodeLocale`
62+
* the status reporting and utility functions covered in :ref:`init-config`
5063

5164
* Memory allocators:
5265

@@ -62,11 +75,13 @@ The following functions can be safely called before Python is initialized:
6275

6376
.. note::
6477

65-
The following functions **should not be called** before
66-
:c:func:`Py_Initialize`: :c:func:`Py_EncodeLocale`, :c:func:`Py_GetPath`,
78+
Despite their apparent similarity to some of the functions listed above,
79+
the following functions **should not be called** before the interpreter has
80+
been initialized: :c:func:`Py_EncodeLocale`, :c:func:`Py_GetPath`,
6781
:c:func:`Py_GetPrefix`, :c:func:`Py_GetExecPrefix`,
6882
:c:func:`Py_GetProgramFullPath`, :c:func:`Py_GetPythonHome`,
69-
:c:func:`Py_GetProgramName` and :c:func:`PyEval_InitThreads`.
83+
:c:func:`Py_GetProgramName`, :c:func:`PyEval_InitThreads`, and
84+
:c:func:`Py_RunMain`.
7085

7186

7287
.. _global-conf-vars:
@@ -346,34 +361,42 @@ Initializing and finalizing the interpreter
346361
this should be called before using any other Python/C API functions; see
347362
:ref:`Before Python Initialization <pre-init-safe>` for the few exceptions.
348363

349-
This initializes
350-
the table of loaded modules (``sys.modules``), and creates the fundamental
351-
modules :mod:`builtins`, :mod:`__main__` and :mod:`sys`. It also initializes
352-
the module search path (``sys.path``). It does not set ``sys.argv``; use
353-
the new :c:type:`PyConfig` API of the :ref:`Python Initialization
354-
Configuration <init-config>` for that. This is a no-op when called for a
355-
second time
356-
(without calling :c:func:`Py_FinalizeEx` first). There is no return value; it is a
357-
fatal error if the initialization fails.
358-
359-
Use the :c:func:`Py_InitializeFromConfig` function to customize the
364+
This initializes the table of loaded modules (``sys.modules``), and creates
365+
the fundamental modules :mod:`builtins`, :mod:`__main__` and :mod:`sys`.
366+
It also initializes the module search path (``sys.path``). It does not set
367+
``sys.argv``; use the :ref:`Python Initialization Configuration <init-config>`
368+
API for that. This is a no-op when called for a second time (without calling
369+
:c:func:`Py_FinalizeEx` first). There is no return value; it is a fatal
370+
error if the initialization fails.
371+
372+
Use :c:func:`Py_InitializeFromConfig` to customize the
360373
:ref:`Python Initialization Configuration <init-config>`.
361374

362375
.. note::
363-
On Windows, changes the console mode from ``O_TEXT`` to ``O_BINARY``, which will
364-
also affect non-Python uses of the console using the C Runtime.
376+
On Windows, changes the console mode from ``O_TEXT`` to ``O_BINARY``,
377+
which will also affect non-Python uses of the console using the C Runtime.
365378

366379

367380
.. c:function:: void Py_InitializeEx(int initsigs)
368381
369382
This function works like :c:func:`Py_Initialize` if *initsigs* is ``1``. If
370-
*initsigs* is ``0``, it skips initialization registration of signal handlers, which
371-
might be useful when Python is embedded.
383+
*initsigs* is ``0``, it skips initialization registration of signal handlers,
384+
which may be useful when CPython is embedded as part of a larger application.
372385
373-
Use the :c:func:`Py_InitializeFromConfig` function to customize the
386+
Use :c:func:`Py_InitializeFromConfig` to customize the
374387
:ref:`Python Initialization Configuration <init-config>`.
375388
376389
390+
.. c:function:: PyStatus Py_InitializeFromConfig(const PyConfig *config)
391+
392+
Initialize Python from *config* configuration, as described in
393+
:ref:`init-from-config`.
394+
395+
See the :ref:`init-config` section for details on pre-initializing the
396+
interpreter, populating the runtime configuration structure, and querying
397+
the returned status structure.
398+
399+
377400
.. c:function:: int Py_IsInitialized()
378401
379402
Return true (nonzero) when the Python interpreter has been initialized, false
@@ -440,12 +463,111 @@ Initializing and finalizing the interpreter
440463
441464
.. versionadded:: 3.6
442465
466+
443467
.. c:function:: void Py_Finalize()
444468
445469
This is a backwards-compatible version of :c:func:`Py_FinalizeEx` that
446470
disregards the return value.
447471
448472
473+
.. c:function:: int Py_BytesMain(int argc, char **argv)
474+
475+
Similar to :c:func:`Py_Main` but *argv* is an array of bytes strings,
476+
allowing the calling application to delegate the text decoding step to
477+
the CPython runtime.
478+
479+
.. versionadded:: 3.8
480+
481+
482+
.. c:function:: int Py_Main(int argc, wchar_t **argv)
483+
484+
The main program for the standard interpreter, encapsulating a full
485+
initialization/finalization cycle, as well as additional
486+
behaviour to implement reading configurations settings from the environment
487+
and command line, and then executing ``__main__`` in accordance with
488+
:ref:`using-on-cmdline`.
489+
490+
This is made available for programs which wish to support the full CPython
491+
command line interface, rather than just embedding a Python runtime in a
492+
larger application.
493+
494+
The *argc* and *argv* parameters are similar to those which are passed to a
495+
C program's :c:func:`main` function, except that the *argv* entries are first
496+
converted to ``wchar_t`` using :c:func:`Py_DecodeLocale`. It is also
497+
important to note that the argument list entries may be modified to point to
498+
strings other than those passed in (however, the contents of the strings
499+
pointed to by the argument list are not modified).
500+
501+
The return value will be ``0`` if the interpreter exits normally (i.e.,
502+
without an exception), ``1`` if the interpreter exits due to an exception,
503+
or ``2`` if the argument list does not represent a valid Python command
504+
line.
505+
506+
Note that if an otherwise unhandled :exc:`SystemExit` is raised, this
507+
function will not return ``1``, but exit the process, as long as
508+
``Py_InspectFlag`` is not set. If ``Py_InspectFlag`` is set, execution will
509+
drop into the interactive Python prompt, at which point a second otherwise
510+
unhandled :exc:`SystemExit` will still exit the process, while any other
511+
means of exiting will set the return value as described above.
512+
513+
In terms of the CPython runtime configuration APIs documented in the
514+
:ref:`runtime configuration <init-config>` section (and without accounting
515+
for error handling), ``Py_Main`` is approximately equivalent to::
516+
517+
PyConfig config;
518+
PyConfig_InitPythonConfig(&config);
519+
PyConfig_SetArgv(&config, argc, argv);
520+
Py_InitializeFromConfig(&config);
521+
PyConfig_Clear(&config);
522+
523+
Py_RunMain();
524+
525+
In normal usage, an embedding application will call this function
526+
*instead* of calling :c:func:`Py_Initialize`, :c:func:`Py_InitializeEx` or
527+
:c:func:`Py_InitializeFromConfig` directly, and all settings will be applied
528+
as described elsewhere in this documentation. If this function is instead
529+
called *after* a preceding runtime initialization API call, then exactly
530+
which environmental and command line configuration settings will be updated
531+
is version dependent (as it depends on which settings correctly support
532+
being modified after they have already been set once when the runtime was
533+
first initialized).
534+
535+
536+
.. c:function:: int Py_RunMain(void)
537+
538+
Executes the main module in a fully configured CPython runtime.
539+
540+
Executes the command (:c:member:`PyConfig.run_command`), the script
541+
(:c:member:`PyConfig.run_filename`) or the module
542+
(:c:member:`PyConfig.run_module`) specified on the command line or in the
543+
configuration. If none of these values are set, runs the interactive Python
544+
prompt (REPL) using the ``__main__`` module's global namespace.
545+
546+
If :c:member:`PyConfig.inspect` is not set (the default), the return value
547+
will be ``0`` if the interpreter exits normally (that is, without raising
548+
an exception), or ``1`` if the interpreter exits due to an exception. If an
549+
otherwise unhandled :exc:`SystemExit` is raised, the function will immediately
550+
exit the process instead of returning ``1``.
551+
552+
If :c:member:`PyConfig.inspect` is set (such as when the :option:`-i` option
553+
is used), rather than returning when the interpreter exits, execution will
554+
instead resume in an interactive Python prompt (REPL) using the ``__main__``
555+
module's global namespace. If the interpreter exited with an exception, it
556+
is immediately raised in the REPL session. The function return value is
557+
then determined by the way the *REPL session* terminates: returning ``0``
558+
if the session terminates without raising an unhandled exception, exiting
559+
immediately for an unhandled :exc:`SystemExit`, and returning ``1`` for
560+
any other unhandled exception.
561+
562+
This function always finalizes the Python interpreter regardless of whether
563+
it returns a value or immediately exits the process due to an unhandled
564+
:exc:`SystemExit` exception.
565+
566+
See :ref:`Python Configuration <init-python-config>` for an example of a
567+
customized Python that always runs in isolated mode using
568+
:c:func:`Py_RunMain`.
569+
570+
449571
Process-wide parameters
450572
=======================
451573

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/pickletester.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from test import support
2727
from test.support import os_helper
2828
from test.support import (
29-
TestFailed, run_with_locale, no_tracing,
29+
TestFailed, run_with_locales, no_tracing,
3030
_2G, _4G, bigmemtest
3131
)
3232
from test.support.import_helper import forget
@@ -2895,7 +2895,7 @@ def test_float(self):
28952895
got = self.loads(pickle)
28962896
self.assert_is_copy(value, got)
28972897

2898-
@run_with_locale('LC_ALL', 'de_DE', 'fr_FR')
2898+
@run_with_locales('LC_ALL', 'de_DE', 'fr_FR', '')
28992899
def test_float_format(self):
29002900
# make sure that floats are formatted locale independent with proto 0
29012901
self.assertEqual(self.dumps(1.2, 0)[0:3], b'F1.')

0 commit comments

Comments
 (0)