Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-125038: Iterator checks are added for some FOR_ITER bytecodes, crash fixed #125051

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
523f765
gh-125038: PyIter_Checks are added for some FOR_ITER bytecodes
efimov-mikhail Oct 7, 2024
e316cb4
gh-125038: Test on gi_frame.f_locals change for a generator
efimov-mikhail Oct 7, 2024
09df328
gh-125038: News entry is added.
efimov-mikhail Oct 7, 2024
d3d5b39
gh-125038: Raise TypeError on such case instead of silently stopping …
efimov-mikhail Oct 7, 2024
c353cf1
gh-125038: Test improvement, assertRaisesRegex is used
efimov-mikhail Oct 7, 2024
1941ced
Update Misc/NEWS.d/next/Core_and_Builtins/2024-10-07-19-26-50.gh-issu…
efimov-mikhail Oct 8, 2024
3e0b12f
gh-125038: Tests on generator modifying through gi_frame.f_locals are…
efimov-mikhail Oct 9, 2024
25e3a80
Merge branch 'sigsegv_fix_in_iterators' of github.com:efimov-mikhail/…
efimov-mikhail Oct 9, 2024
d0fa5ff
gh-124502: Add PyUnicode_Equal() function (#124504)
vstinner Oct 7, 2024
2e541a7
gh-125072: Add label for assignment expressions; update tracked secti…
emilyemorehouse Oct 7, 2024
4e25977
Docs: make a tutorial example more precise (#125066)
nedbat Oct 7, 2024
c214282
gh-90102: Remove isatty call during regular open (#124922)
cmaloney Oct 8, 2024
7788578
gh-70870: Clarify dual usage of 'free variable' (#122545)
ncoghlan Oct 8, 2024
c0a9b19
gh-69998: Fix decoding error in locale.nl_langinfo() (GH-124963)
serhiy-storchaka Oct 8, 2024
fdf1648
bpo-34206: Improve docs and test coverage for pre-init functions (#8023)
ncoghlan Oct 8, 2024
7c34a23
gh-53203: Improve tests for strptime() (GH-125090)
serhiy-storchaka Oct 8, 2024
0056888
gh-123378: fix a crash in `UnicodeError.__str__` (#124935)
picnixz Oct 8, 2024
c4812df
gh-123961: Convert _curses to a multi-phase init module (PEP-489) (#1…
picnixz Oct 8, 2024
148e3fe
gh-90102: Fix pyio _isatty_open_only() (#125089)
cmaloney Oct 8, 2024
753347e
gh-75898: make use of thread more explicit in the "Socket Programming…
zuo Oct 8, 2024
6002315
gh-121404: typo fix in compile.c: MATADATA -> METADATA (#125101)
efimov-mikhail Oct 8, 2024
8e98322
Doc: Improve description of ``GET_LEN`` opcode (#114583)
Eclips4 Oct 8, 2024
6b6d27f
gh-112433 add versionadded for `ctypes.Structure._align_` (#125087)
monkeyman192 Oct 8, 2024
b926c8e
gh-125096: Don't import _pyrepl in site if PYTHON_BASIC_REPL (#125097)
vstinner Oct 8, 2024
fc4dc44
gh-115999: Stop the world when invalidating function versions (#124997)
mpage Oct 8, 2024
8eec02d
gh-124502: Optimize unicode_eq() (#125105)
vstinner Oct 8, 2024
bd716e0
gh-117721: use PyMutex in `_thread.lock` (#125110)
kumaraditya303 Oct 8, 2024
b2764a9
gh-125084: Resolve paths in generator common code (GH-125085)
cmaloney Oct 8, 2024
320d1ab
gh-125063: Emit slices as constants in the bytecode compiler (#125064)
mdboom Oct 8, 2024
bbe670b
gh-124832: Add a note to indicate that `datetime.now` may return the …
spacemanspiff2007 Oct 8, 2024
7a4b55c
Misc improvements to the itertools docs (gh-125147)
rhettinger Oct 8, 2024
3b823fd
gh-116110: remove extra processing for the __signature__ attribute (G…
skirpichev Oct 8, 2024
0dcbd9b
GH-124478: Cleanup argparse documentation (#124877)
savannahostrowski Oct 8, 2024
5f4da9b
gh-123849: Fix test_sqlite3.test_table_dump when foreign keys are ena…
felixxm Oct 8, 2024
7c10a29
gh-124612: Use ghcr.io/python/autoconf instead of public image (#124657)
corona10 Oct 9, 2024
8192c63
gh-101552: Allow pydoc to display signatures in source format (#124669)
JelleZijlstra Oct 9, 2024
6996d4e
docs: in venv table use executable name (GH-124315)
musvaage Oct 9, 2024
0d0e6f3
gh-124502: Remove _PyUnicode_EQ() function (#125114)
vstinner Oct 9, 2024
376a1d7
gh-125150: Skip test_fma_zero_result on NetBSD due to IEE 754-2008 im…
furkanonder Oct 9, 2024
fbb424b
gh-124969: Fix locale.nl_langinfo(locale.ALT_DIGITS) (GH-124974)
serhiy-storchaka Oct 9, 2024
2c98a00
gh-107562: make_ssl_certs.py: produce test certificates that expire f…
kanavin Oct 9, 2024
8da7ac6
gh-125038: Little change in test, readability slightly improved
efimov-mikhail Oct 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions Doc/c-api/code.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ bound into a function.

.. c:function:: Py_ssize_t PyCode_GetNumFree(PyCodeObject *co)

Return the number of free variables in a code object.
Return the number of :term:`free (closure) variables <closure variable>`
in a code object.

.. c:function:: int PyUnstable_Code_GetFirstFree(PyCodeObject *co)

Return the position of the first free variable in a code object.
Return the position of the first :term:`free (closure) variable <closure variable>`
in a code object.

.. versionchanged:: 3.13

Expand Down Expand Up @@ -144,7 +146,8 @@ bound into a function.

Equivalent to the Python code ``getattr(co, 'co_freevars')``.
Returns a new reference to a :c:type:`PyTupleObject` containing the names of
the free variables. On error, ``NULL`` is returned and an exception is raised.
the :term:`free (closure) variables <closure variable>`. On error, ``NULL`` is returned
and an exception is raised.

.. versionadded:: 3.11

Expand Down
162 changes: 142 additions & 20 deletions Doc/c-api/init.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
Initialization, Finalization, and Threads
*****************************************

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

.. _pre-init-safe:

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

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

* Functions that initialize the interpreter:

* :c:func:`Py_Initialize`
* :c:func:`Py_InitializeEx`
* :c:func:`Py_InitializeFromConfig`
* :c:func:`Py_BytesMain`
* :c:func:`Py_Main`
* the runtime pre-initialization functions covered in :ref:`init-config`

* Configuration functions:

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

* Informative functions:

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

* Utilities:

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

* Memory allocators:

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

.. note::

The following functions **should not be called** before
:c:func:`Py_Initialize`: :c:func:`Py_EncodeLocale`, :c:func:`Py_GetPath`,
Despite their apparent similarity to some of the functions listed above,
the following functions **should not be called** before the interpreter has
been initialized: :c:func:`Py_EncodeLocale`, :c:func:`Py_GetPath`,
:c:func:`Py_GetPrefix`, :c:func:`Py_GetExecPrefix`,
:c:func:`Py_GetProgramFullPath`, :c:func:`Py_GetPythonHome`,
:c:func:`Py_GetProgramName` and :c:func:`PyEval_InitThreads`.
:c:func:`Py_GetProgramName`, :c:func:`PyEval_InitThreads`, and
:c:func:`Py_RunMain`.


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

This initializes
the table of loaded modules (``sys.modules``), and creates the fundamental
modules :mod:`builtins`, :mod:`__main__` and :mod:`sys`. It also initializes
the module search path (``sys.path``). It does not set ``sys.argv``; use
the new :c:type:`PyConfig` API of the :ref:`Python Initialization
Configuration <init-config>` for that. This is a no-op when called for a
second time
(without calling :c:func:`Py_FinalizeEx` first). There is no return value; it is a
fatal error if the initialization fails.

Use the :c:func:`Py_InitializeFromConfig` function to customize the
This initializes the table of loaded modules (``sys.modules``), and creates
the fundamental modules :mod:`builtins`, :mod:`__main__` and :mod:`sys`.
It also initializes the module search path (``sys.path``). It does not set
``sys.argv``; use the :ref:`Python Initialization Configuration <init-config>`
API for that. This is a no-op when called for a second time (without calling
:c:func:`Py_FinalizeEx` first). There is no return value; it is a fatal
error if the initialization fails.

Use :c:func:`Py_InitializeFromConfig` to customize the
:ref:`Python Initialization Configuration <init-config>`.

.. note::
On Windows, changes the console mode from ``O_TEXT`` to ``O_BINARY``, which will
also affect non-Python uses of the console using the C Runtime.
On Windows, changes the console mode from ``O_TEXT`` to ``O_BINARY``,
which will also affect non-Python uses of the console using the C Runtime.


.. c:function:: void Py_InitializeEx(int initsigs)

This function works like :c:func:`Py_Initialize` if *initsigs* is ``1``. If
*initsigs* is ``0``, it skips initialization registration of signal handlers, which
might be useful when Python is embedded.
*initsigs* is ``0``, it skips initialization registration of signal handlers,
which may be useful when CPython is embedded as part of a larger application.

Use the :c:func:`Py_InitializeFromConfig` function to customize the
Use :c:func:`Py_InitializeFromConfig` to customize the
:ref:`Python Initialization Configuration <init-config>`.


.. c:function:: PyStatus Py_InitializeFromConfig(const PyConfig *config)

Initialize Python from *config* configuration, as described in
:ref:`init-from-config`.

See the :ref:`init-config` section for details on pre-initializing the
interpreter, populating the runtime configuration structure, and querying
the returned status structure.


.. c:function:: int Py_IsInitialized()

Return true (nonzero) when the Python interpreter has been initialized, false
Expand Down Expand Up @@ -440,12 +463,111 @@ Initializing and finalizing the interpreter

.. versionadded:: 3.6


.. c:function:: void Py_Finalize()

This is a backwards-compatible version of :c:func:`Py_FinalizeEx` that
disregards the return value.


.. c:function:: int Py_BytesMain(int argc, char **argv)

Similar to :c:func:`Py_Main` but *argv* is an array of bytes strings,
allowing the calling application to delegate the text decoding step to
the CPython runtime.

.. versionadded:: 3.8


.. c:function:: int Py_Main(int argc, wchar_t **argv)

The main program for the standard interpreter, encapsulating a full
initialization/finalization cycle, as well as additional
behaviour to implement reading configurations settings from the environment
and command line, and then executing ``__main__`` in accordance with
:ref:`using-on-cmdline`.

This is made available for programs which wish to support the full CPython
command line interface, rather than just embedding a Python runtime in a
larger application.

The *argc* and *argv* parameters are similar to those which are passed to a
C program's :c:func:`main` function, except that the *argv* entries are first
converted to ``wchar_t`` using :c:func:`Py_DecodeLocale`. It is also
important to note that the argument list entries may be modified to point to
strings other than those passed in (however, the contents of the strings
pointed to by the argument list are not modified).

The return value will be ``0`` if the interpreter exits normally (i.e.,
without an exception), ``1`` if the interpreter exits due to an exception,
or ``2`` if the argument list does not represent a valid Python command
line.

Note that if an otherwise unhandled :exc:`SystemExit` is raised, this
function will not return ``1``, but exit the process, as long as
``Py_InspectFlag`` is not set. If ``Py_InspectFlag`` is set, execution will
drop into the interactive Python prompt, at which point a second otherwise
unhandled :exc:`SystemExit` will still exit the process, while any other
means of exiting will set the return value as described above.

In terms of the CPython runtime configuration APIs documented in the
:ref:`runtime configuration <init-config>` section (and without accounting
for error handling), ``Py_Main`` is approximately equivalent to::

PyConfig config;
PyConfig_InitPythonConfig(&config);
PyConfig_SetArgv(&config, argc, argv);
Py_InitializeFromConfig(&config);
PyConfig_Clear(&config);

Py_RunMain();

In normal usage, an embedding application will call this function
*instead* of calling :c:func:`Py_Initialize`, :c:func:`Py_InitializeEx` or
:c:func:`Py_InitializeFromConfig` directly, and all settings will be applied
as described elsewhere in this documentation. If this function is instead
called *after* a preceding runtime initialization API call, then exactly
which environmental and command line configuration settings will be updated
is version dependent (as it depends on which settings correctly support
being modified after they have already been set once when the runtime was
first initialized).


.. c:function:: int Py_RunMain(void)

Executes the main module in a fully configured CPython runtime.

Executes the command (:c:member:`PyConfig.run_command`), the script
(:c:member:`PyConfig.run_filename`) or the module
(:c:member:`PyConfig.run_module`) specified on the command line or in the
configuration. If none of these values are set, runs the interactive Python
prompt (REPL) using the ``__main__`` module's global namespace.

If :c:member:`PyConfig.inspect` is not set (the default), the return value
will be ``0`` if the interpreter exits normally (that is, without raising
an exception), or ``1`` if the interpreter exits due to an exception. If an
otherwise unhandled :exc:`SystemExit` is raised, the function will immediately
exit the process instead of returning ``1``.

If :c:member:`PyConfig.inspect` is set (such as when the :option:`-i` option
is used), rather than returning when the interpreter exits, execution will
instead resume in an interactive Python prompt (REPL) using the ``__main__``
module's global namespace. If the interpreter exited with an exception, it
is immediately raised in the REPL session. The function return value is
then determined by the way the *REPL session* terminates: returning ``0``
if the session terminates without raising an unhandled exception, exiting
immediately for an unhandled :exc:`SystemExit`, and returning ``1`` for
any other unhandled exception.

This function always finalizes the Python interpreter regardless of whether
it returns a value or immediately exits the process due to an unhandled
:exc:`SystemExit` exception.

See :ref:`Python Configuration <init-python-config>` for an example of a
customized Python that always runs in isolated mode using
:c:func:`Py_RunMain`.


Process-wide parameters
=======================

Expand Down
29 changes: 4 additions & 25 deletions Doc/c-api/init_config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1356,14 +1356,13 @@ the :option:`-X` command line option.
The ``show_alloc_count`` field has been removed.


.. _init-from-config:

Initialization with PyConfig
----------------------------

Function to initialize Python:

.. c:function:: PyStatus Py_InitializeFromConfig(const PyConfig *config)

Initialize Python from *config* configuration.
Initializing the interpreter from a populated configuration struct is handled
by calling :c:func:`Py_InitializeFromConfig`.

The caller is responsible to handle exceptions (error or exit) using
:c:func:`PyStatus_Exception` and :c:func:`Py_ExitStatusException`.
Expand Down Expand Up @@ -1835,26 +1834,6 @@ return ``-1`` on error:
}


Py_RunMain()
============

.. c:function:: int Py_RunMain(void)

Execute the command (:c:member:`PyConfig.run_command`), the script
(:c:member:`PyConfig.run_filename`) or the module
(:c:member:`PyConfig.run_module`) specified on the command line or in the
configuration.

By default and when if :option:`-i` option is used, run the REPL.

Finally, finalizes Python and returns an exit status that can be passed to
the ``exit()`` function.

See :ref:`Python Configuration <init-python-config>` for an example of
customized Python always running in isolated mode using
:c:func:`Py_RunMain`.


Runtime Python configuration API
================================

Expand Down
25 changes: 25 additions & 0 deletions Doc/c-api/unicode.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1438,6 +1438,31 @@ They all return ``NULL`` or ``-1`` if an exception occurs.
This function returns ``-1`` upon failure, so one should call
:c:func:`PyErr_Occurred` to check for errors.

.. seealso::

The :c:func:`PyUnicode_Equal` function.


.. c:function:: int PyUnicode_Equal(PyObject *a, PyObject *b)

Test if two strings are equal:

* Return ``1`` if *a* is equal to *b*.
* Return ``0`` if *a* is not equal to *b*.
* Set a :exc:`TypeError` exception and return ``-1`` if *a* or *b* is not a
:class:`str` object.

The function always succeeds if *a* and *b* are :class:`str` objects.

The function works for :class:`str` subclasses, but does not honor custom
``__eq__()`` method.

.. seealso::

The :c:func:`PyUnicode_Compare` function.

.. versionadded:: 3.14


.. c:function:: int PyUnicode_EqualToUTF8AndSize(PyObject *unicode, const char *string, Py_ssize_t size)

Expand Down
24 changes: 0 additions & 24 deletions Doc/c-api/veryhigh.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,6 @@ are only passed to these functions if it is certain that they were created by
the same library that the Python runtime is using.


.. c:function:: int Py_Main(int argc, wchar_t **argv)

The main program for the standard interpreter. This is made available for
programs which embed Python. The *argc* and *argv* parameters should be
prepared exactly as those which are passed to a C program's :c:func:`main`
function (converted to wchar_t according to the user's locale). It is
important to note that the argument list may be modified (but the contents of
the strings pointed to by the argument list are not). The return value will
be ``0`` if the interpreter exits normally (i.e., without an exception),
``1`` if the interpreter exits due to an exception, or ``2`` if the parameter
list does not represent a valid Python command line.

Note that if an otherwise unhandled :exc:`SystemExit` is raised, this
function will not return ``1``, but exit the process, as long as
:c:member:`PyConfig.inspect` is zero.


.. c:function:: int Py_BytesMain(int argc, char **argv)

Similar to :c:func:`Py_Main` but *argv* is an array of bytes strings.

.. versionadded:: 3.8


.. c:function:: int PyRun_AnyFile(FILE *fp, const char *filename)

This is a simplified interface to :c:func:`PyRun_AnyFileExFlags` below, leaving
Expand Down
1 change: 1 addition & 0 deletions Doc/data/stable_abi.dat

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading