Skip to content

Commit fc3a1b4

Browse files
authored
Merge branch 'main' into remove-import-trampoline
2 parents b75fe1f + 3fb7c60 commit fc3a1b4

34 files changed

+362
-168
lines changed

‎Doc/c-api/list.rst‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ List Objects
8686
Macro form of :c:func:`PyList_SetItem` without error checking. This is
8787
normally only used to fill in new lists where there is no previous content.
8888
89+
Bounds checking is performed as an assertion if Python is built in
90+
:ref:`debug mode <debug-build>` or :option:`with assertions
91+
<--with-assertions>`.
92+
8993
.. note::
9094
9195
This macro "steals" a reference to *item*, and, unlike

‎Doc/c-api/tuple.rst‎

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ Tuple Objects
8989
Like :c:func:`PyTuple_SetItem`, but does no error checking, and should *only* be
9090
used to fill in brand new tuples.
9191
92+
Bounds checking is performed as an assertion if Python is built in
93+
:ref:`debug mode <debug-build>` or :option:`with assertions <--with-assertions>`.
94+
9295
.. note::
9396
9497
This function "steals" a reference to *o*, and, unlike
@@ -194,12 +197,17 @@ type.
194197
.. c:function:: PyObject* PyStructSequence_GetItem(PyObject *p, Py_ssize_t pos)
195198
196199
Return the object at position *pos* in the struct sequence pointed to by *p*.
197-
No bounds checking is performed.
200+
201+
Bounds checking is performed as an assertion if Python is built in
202+
:ref:`debug mode <debug-build>` or :option:`with assertions <--with-assertions>`.
198203
199204
200205
.. c:function:: PyObject* PyStructSequence_GET_ITEM(PyObject *p, Py_ssize_t pos)
201206
202-
Macro equivalent of :c:func:`PyStructSequence_GetItem`.
207+
Alias to :c:func:`PyStructSequence_GetItem`.
208+
209+
.. versionchanged:: 3.13
210+
Now implemented as an alias to :c:func:`PyStructSequence_GetItem`.
203211
204212
205213
.. c:function:: void PyStructSequence_SetItem(PyObject *p, Py_ssize_t pos, PyObject *o)
@@ -208,16 +216,17 @@ type.
208216
:c:func:`PyTuple_SET_ITEM`, this should only be used to fill in brand new
209217
instances.
210218
219+
Bounds checking is performed as an assertion if Python is built in
220+
:ref:`debug mode <debug-build>` or :option:`with assertions <--with-assertions>`.
221+
211222
.. note::
212223
213224
This function "steals" a reference to *o*.
214225
215226
216227
.. c:function:: void PyStructSequence_SET_ITEM(PyObject *p, Py_ssize_t *pos, PyObject *o)
217228
218-
Similar to :c:func:`PyStructSequence_SetItem`, but implemented as a static
219-
inlined function.
229+
Alias to :c:func:`PyStructSequence_SetItem`.
220230
221-
.. note::
222-
223-
This function "steals" a reference to *o*.
231+
.. versionchanged:: 3.13
232+
Now implemented as an alias to :c:func:`PyStructSequence_SetItem`.

‎Doc/glossary.rst‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ Glossary
8383

8484
asynchronous context manager
8585
An object which controls the environment seen in an
86-
:keyword:`async with` statement by defining :meth:`__aenter__` and
87-
:meth:`__aexit__` methods. Introduced by :pep:`492`.
86+
:keyword:`async with` statement by defining :meth:`~object.__aenter__` and
87+
:meth:`~object.__aexit__` methods. Introduced by :pep:`492`.
8888

8989
asynchronous generator
9090
A function which returns an :term:`asynchronous generator iterator`. It
@@ -104,26 +104,26 @@ Glossary
104104
An object created by a :term:`asynchronous generator` function.
105105

106106
This is an :term:`asynchronous iterator` which when called using the
107-
:meth:`__anext__` method returns an awaitable object which will execute
107+
:meth:`~object.__anext__` method returns an awaitable object which will execute
108108
the body of the asynchronous generator function until the next
109109
:keyword:`yield` expression.
110110

111111
Each :keyword:`yield` temporarily suspends processing, remembering the
112112
location execution state (including local variables and pending
113113
try-statements). When the *asynchronous generator iterator* effectively
114-
resumes with another awaitable returned by :meth:`__anext__`, it
114+
resumes with another awaitable returned by :meth:`~object.__anext__`, it
115115
picks up where it left off. See :pep:`492` and :pep:`525`.
116116

117117
asynchronous iterable
118118
An object, that can be used in an :keyword:`async for` statement.
119119
Must return an :term:`asynchronous iterator` from its
120-
:meth:`__aiter__` method. Introduced by :pep:`492`.
120+
:meth:`~object.__aiter__` method. Introduced by :pep:`492`.
121121

122122
asynchronous iterator
123-
An object that implements the :meth:`__aiter__` and :meth:`__anext__`
124-
methods. ``__anext__`` must return an :term:`awaitable` object.
123+
An object that implements the :meth:`~object.__aiter__` and :meth:`~object.__anext__`
124+
methods. :meth:`~object.__anext__` must return an :term:`awaitable` object.
125125
:keyword:`async for` resolves the awaitables returned by an asynchronous
126-
iterator's :meth:`__anext__` method until it raises a
126+
iterator's :meth:`~object.__anext__` method until it raises a
127127
:exc:`StopAsyncIteration` exception. Introduced by :pep:`492`.
128128

129129
attribute
@@ -140,7 +140,7 @@ Glossary
140140

141141
awaitable
142142
An object that can be used in an :keyword:`await` expression. Can be
143-
a :term:`coroutine` or an object with an :meth:`__await__` method.
143+
a :term:`coroutine` or an object with an :meth:`~object.__await__` method.
144144
See also :pep:`492`.
145145

146146
BDFL

‎Doc/library/asyncio-eventloop.rst‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,9 @@ TLS Upgrade
895895
object only because the coder caches *protocol*-side data and sporadically
896896
exchanges extra TLS session packets with *transport*.
897897

898+
In some situations (e.g. when the passed transport is already closing) this
899+
may return ``None``.
900+
898901
Parameters:
899902

900903
* *transport* and *protocol* instances that methods like

‎Doc/library/asyncio-task.rst‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -654,16 +654,16 @@ Timeouts
654654
If ``long_running_task`` takes more than 10 seconds to complete,
655655
the context manager will cancel the current task and handle
656656
the resulting :exc:`asyncio.CancelledError` internally, transforming it
657-
into an :exc:`asyncio.TimeoutError` which can be caught and handled.
657+
into a :exc:`TimeoutError` which can be caught and handled.
658658

659659
.. note::
660660

661661
The :func:`asyncio.timeout` context manager is what transforms
662-
the :exc:`asyncio.CancelledError` into an :exc:`asyncio.TimeoutError`,
663-
which means the :exc:`asyncio.TimeoutError` can only be caught
662+
the :exc:`asyncio.CancelledError` into a :exc:`TimeoutError`,
663+
which means the :exc:`TimeoutError` can only be caught
664664
*outside* of the context manager.
665665

666-
Example of catching :exc:`asyncio.TimeoutError`::
666+
Example of catching :exc:`TimeoutError`::
667667

668668
async def main():
669669
try:

‎Doc/library/exceptions.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ The following exceptions are the exceptions that are usually raised.
450450

451451
.. exception:: StopAsyncIteration
452452

453-
Must be raised by :meth:`__anext__` method of an
453+
Must be raised by :meth:`~object.__anext__` method of an
454454
:term:`asynchronous iterator` object to stop the iteration.
455455

456456
.. versionadded:: 3.5

‎Doc/library/optparse.rst‎

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ on the command-line, for example::
4242
<yourscript> --file=outfile -q
4343

4444
As it parses the command line, :mod:`optparse` sets attributes of the
45-
``options`` object returned by :meth:`parse_args` based on user-supplied
46-
command-line values. When :meth:`parse_args` returns from parsing this command
45+
``options`` object returned by :meth:`~OptionParser.parse_args` based on user-supplied
46+
command-line values. When :meth:`~OptionParser.parse_args` returns from parsing this command
4747
line, ``options.filename`` will be ``"outfile"`` and ``options.verbose`` will be
4848
``False``. :mod:`optparse` supports both long and short options, allows short
4949
options to be merged together, and allows options to be associated with their
@@ -285,10 +285,10 @@ program's command line::
285285

286286
(options, args) = parser.parse_args()
287287

288-
(If you like, you can pass a custom argument list to :meth:`parse_args`, but
288+
(If you like, you can pass a custom argument list to :meth:`~OptionParser.parse_args`, but
289289
that's rarely necessary: by default it uses ``sys.argv[1:]``.)
290290

291-
:meth:`parse_args` returns two values:
291+
:meth:`~OptionParser.parse_args` returns two values:
292292

293293
* ``options``, an object containing values for all of your options---e.g. if
294294
``--file`` takes a single string argument, then ``options.file`` will be the
@@ -339,7 +339,7 @@ Now let's make up a fake command line and ask :mod:`optparse` to parse it::
339339

340340
When :mod:`optparse` sees the option string ``-f``, it consumes the next
341341
argument, ``foo.txt``, and stores it in ``options.filename``. So, after this
342-
call to :meth:`parse_args`, ``options.filename`` is ``"foo.txt"``.
342+
call to :meth:`~OptionParser.parse_args`, ``options.filename`` is ``"foo.txt"``.
343343

344344
Some other option types supported by :mod:`optparse` are ``int`` and ``float``.
345345
Here's an option that expects an integer argument::
@@ -453,7 +453,8 @@ Again, the default value for ``verbose`` will be ``True``: the last default
453453
value supplied for any particular destination is the one that counts.
454454

455455
A clearer way to specify default values is the :meth:`set_defaults` method of
456-
OptionParser, which you can call at any time before calling :meth:`parse_args`::
456+
OptionParser, which you can call at any time before calling
457+
:meth:`~OptionParser.parse_args`::
457458

458459
parser.set_defaults(verbose=True)
459460
parser.add_option(...)
@@ -1338,35 +1339,37 @@ Parsing arguments
13381339
^^^^^^^^^^^^^^^^^
13391340

13401341
The whole point of creating and populating an OptionParser is to call its
1341-
:meth:`parse_args` method::
1342+
:meth:`~OptionParser.parse_args` method.
13421343

1343-
(options, args) = parser.parse_args(args=None, values=None)
1344+
.. method:: OptionParser.parse_args(args=None, values=None)
13441345

1345-
where the input parameters are
1346+
Parse the command-line options found in *args*.
13461347

1347-
``args``
1348-
the list of arguments to process (default: ``sys.argv[1:]``)
1348+
The input parameters are
13491349

1350-
``values``
1351-
an :class:`optparse.Values` object to store option arguments in (default: a
1352-
new instance of :class:`Values`) -- if you give an existing object, the
1353-
option defaults will not be initialized on it
1350+
``args``
1351+
the list of arguments to process (default: ``sys.argv[1:]``)
13541352

1355-
and the return values are
1353+
``values``
1354+
an :class:`Values` object to store option arguments in (default: a
1355+
new instance of :class:`Values`) -- if you give an existing object, the
1356+
option defaults will not be initialized on it
13561357

1357-
``options``
1358-
the same object that was passed in as ``values``, or the optparse.Values
1359-
instance created by :mod:`optparse`
1358+
and the return value is a pair ``(options, args)`` where
13601359

1361-
``args``
1362-
the leftover positional arguments after all options have been processed
1360+
``options``
1361+
the same object that was passed in as *values*, or the ``optparse.Values``
1362+
instance created by :mod:`optparse`
1363+
1364+
``args``
1365+
the leftover positional arguments after all options have been processed
13631366

13641367
The most common usage is to supply neither keyword argument. If you supply
13651368
``values``, it will be modified with repeated :func:`setattr` calls (roughly one
13661369
for every option argument stored to an option destination) and returned by
1367-
:meth:`parse_args`.
1370+
:meth:`~OptionParser.parse_args`.
13681371

1369-
If :meth:`parse_args` encounters any errors in the argument list, it calls the
1372+
If :meth:`~OptionParser.parse_args` encounters any errors in the argument list, it calls the
13701373
OptionParser's :meth:`error` method with an appropriate end-user error message.
13711374
This ultimately terminates your process with an exit status of 2 (the
13721375
traditional Unix exit status for command-line errors).
@@ -1661,7 +1664,7 @@ where
16611664
the current list of leftover arguments, ie. arguments that have been
16621665
consumed but are neither options nor option arguments. Feel free to modify
16631666
``parser.largs``, e.g. by adding more arguments to it. (This list will
1664-
become ``args``, the second return value of :meth:`parse_args`.)
1667+
become ``args``, the second return value of :meth:`~OptionParser.parse_args`.)
16651668

16661669
``parser.rargs``
16671670
the current list of remaining arguments, ie. with ``opt_str`` and

‎Doc/whatsnew/3.13.rst‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,12 @@ New Features
441441
``NULL`` if the referent is no longer live.
442442
(Contributed by Victor Stinner in :gh:`105927`.)
443443

444+
* If Python is built in :ref:`debug mode <debug-build>` or :option:`with
445+
assertions <--with-assertions>`, :c:func:`PyTuple_SET_ITEM` and
446+
:c:func:`PyList_SET_ITEM` now check the index argument with an assertion.
447+
If the assertion fails, make sure that the size is set before.
448+
(Contributed by Victor Stinner in :gh:`106168`.)
449+
444450
Porting to Python 3.13
445451
----------------------
446452

‎Include/cpython/listobject.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ static inline Py_ssize_t PyList_GET_SIZE(PyObject *op) {
4141
static inline void
4242
PyList_SET_ITEM(PyObject *op, Py_ssize_t index, PyObject *value) {
4343
PyListObject *list = _PyList_CAST(op);
44+
assert(0 <= index);
45+
assert(index < Py_SIZE(list));
4446
list->ob_item[index] = value;
4547
}
4648
#define PyList_SET_ITEM(op, index, value) \

‎Include/cpython/tupleobject.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ static inline Py_ssize_t PyTuple_GET_SIZE(PyObject *op) {
3131
static inline void
3232
PyTuple_SET_ITEM(PyObject *op, Py_ssize_t index, PyObject *value) {
3333
PyTupleObject *tuple = _PyTuple_CAST(op);
34+
assert(0 <= index);
35+
assert(index < Py_SIZE(tuple));
3436
tuple->ob_item[index] = value;
3537
}
3638
#define PyTuple_SET_ITEM(op, index, value) \

0 commit comments

Comments
 (0)