Skip to content

Commit a124b59

Browse files
authored
Merge branch '3.12' into backport-f67ff9e-3.12
2 parents cc1c1d6 + 0405833 commit a124b59

File tree

68 files changed

+689
-286
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+689
-286
lines changed

Doc/c-api/allocation.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ Allocating Objects on the Heap
3535
The size of the memory allocation is determined from the
3636
:c:member:`~PyTypeObject.tp_basicsize` field of the type object.
3737
38+
Note that this function is unsuitable if *typeobj* has
39+
:c:macro:`Py_TPFLAGS_HAVE_GC` set. For such objects,
40+
use :c:func:`PyObject_GC_New` instead.
41+
3842
3943
.. c:macro:: PyObject_NewVar(TYPE, typeobj, size)
4044
@@ -49,6 +53,10 @@ Allocating Objects on the Heap
4953
fields into the same allocation decreases the number of allocations,
5054
improving the memory management efficiency.
5155
56+
Note that this function is unsuitable if *typeobj* has
57+
:c:macro:`Py_TPFLAGS_HAVE_GC` set. For such objects,
58+
use :c:func:`PyObject_GC_NewVar` instead.
59+
5260
5361
.. c:function:: void PyObject_Del(void *op)
5462

Doc/c-api/long.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,16 @@ distinguished from a number. Use :c:func:`PyErr_Occurred` to disambiguate.
135135
.. versionchanged:: 3.10
136136
This function will no longer use :meth:`~object.__int__`.
137137
138+
.. c:namespace:: NULL
139+
140+
.. c:function:: long PyLong_AS_LONG(PyObject *obj)
141+
142+
A :term:`soft deprecated` alias.
143+
Exactly equivalent to the preferred ``PyLong_AsLong``. In particular,
144+
it can fail with :exc:`OverflowError` or another exception.
145+
146+
.. deprecated:: 3.14
147+
The function is soft deprecated.
138148
139149
.. c:function:: long PyLong_AsLongAndOverflow(PyObject *obj, int *overflow)
140150

Doc/c-api/structures.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ under :ref:`reference counting <countingrefs>`.
6363
See documentation of :c:type:`PyVarObject` above.
6464

6565

66+
.. c:var:: PyTypeObject PyBaseObject_Type
67+
68+
The base class of all other objects, the same as :class:`object` in Python.
69+
70+
6671
.. c:function:: int Py_Is(PyObject *x, PyObject *y)
6772
6873
Test if the *x* object is the *y* object, the same as ``x is y`` in Python.

Doc/c-api/type.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,9 @@ The following functions and structs are used to create
397397
class need *in addition* to the superclass.
398398
Use :c:func:`PyObject_GetTypeData` to get a pointer to subclass-specific
399399
memory reserved this way.
400+
For negative :c:member:`!basicsize`, Python will insert padding when
401+
needed to meet :c:member:`~PyTypeObject.tp_basicsize`'s alignment
402+
requirements.
400403
401404
.. versionchanged:: 3.12
402405

Doc/c-api/typeobj.rst

Lines changed: 74 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
.. _type-structs:
44

5-
Type Objects
6-
============
5+
Type Object Structures
6+
======================
77

88
Perhaps one of the most important structures of the Python object system is the
99
structure that defines a new type: the :c:type:`PyTypeObject` structure. Type
@@ -559,6 +559,9 @@ PyVarObject Slots
559559
initialized to zero. For :ref:`dynamically allocated type objects
560560
<heap-types>`, this field has a special internal meaning.
561561

562+
This field should be accessed using the :c:func:`Py_SIZE()` and
563+
:c:func:`Py_SET_SIZE()` macros.
564+
562565
**Inheritance:**
563566

564567
This field is not inherited by subtypes.
@@ -609,47 +612,86 @@ and :c:data:`PyType_Type` effectively act as defaults.)
609612

610613

611614
.. c:member:: Py_ssize_t PyTypeObject.tp_basicsize
612-
Py_ssize_t PyTypeObject.tp_itemsize
615+
Py_ssize_t PyTypeObject.tp_itemsize
613616
614617
These fields allow calculating the size in bytes of instances of the type.
615618

616619
There are two kinds of types: types with fixed-length instances have a zero
617-
:c:member:`~PyTypeObject.tp_itemsize` field, types with variable-length instances have a non-zero
618-
:c:member:`~PyTypeObject.tp_itemsize` field. For a type with fixed-length instances, all
619-
instances have the same size, given in :c:member:`~PyTypeObject.tp_basicsize`.
620+
:c:member:`!tp_itemsize` field, types with variable-length instances have a non-zero
621+
:c:member:`!tp_itemsize` field. For a type with fixed-length instances, all
622+
instances have the same size, given in :c:member:`!tp_basicsize`.
623+
(Exceptions to this rule can be made using
624+
:c:func:`PyUnstable_Object_GC_NewWithExtraData`.)
620625

621626
For a type with variable-length instances, the instances must have an
622-
:c:member:`~PyVarObject.ob_size` field, and the instance size is :c:member:`~PyTypeObject.tp_basicsize` plus N
623-
times :c:member:`~PyTypeObject.tp_itemsize`, where N is the "length" of the object. The value of
624-
N is typically stored in the instance's :c:member:`~PyVarObject.ob_size` field. There are
625-
exceptions: for example, ints use a negative :c:member:`~PyVarObject.ob_size` to indicate a
626-
negative number, and N is ``abs(ob_size)`` there. Also, the presence of an
627-
:c:member:`~PyVarObject.ob_size` field in the instance layout doesn't mean that the instance
628-
structure is variable-length (for example, the structure for the list type has
629-
fixed-length instances, yet those instances have a meaningful :c:member:`~PyVarObject.ob_size`
630-
field).
631-
632-
The basic size includes the fields in the instance declared by the macro
633-
:c:macro:`PyObject_HEAD` or :c:macro:`PyObject_VAR_HEAD` (whichever is used to
634-
declare the instance struct) and this in turn includes the :c:member:`~PyObject._ob_prev` and
635-
:c:member:`~PyObject._ob_next` fields if they are present. This means that the only correct
636-
way to get an initializer for the :c:member:`~PyTypeObject.tp_basicsize` is to use the
637-
``sizeof`` operator on the struct used to declare the instance layout.
638-
The basic size does not include the GC header size.
627+
:c:member:`~PyVarObject.ob_size` field, and the instance size is
628+
:c:member:`!tp_basicsize` plus N times :c:member:`!tp_itemsize`,
629+
where N is the "length" of the object.
630+
631+
Functions like :c:func:`PyObject_NewVar` will take the value of N as an
632+
argument, and store in the instance's :c:member:`~PyVarObject.ob_size` field.
633+
Note that the :c:member:`~PyVarObject.ob_size` field may later be used for
634+
other purposes. For example, :py:type:`int` instances use the bits of
635+
:c:member:`~PyVarObject.ob_size` in an implementation-defined
636+
way; the underlying storage and its size should be acessed using
637+
:c:func:`PyLong_Export`.
638+
639+
.. note::
639640

640-
A note about alignment: if the variable items require a particular alignment,
641-
this should be taken care of by the value of :c:member:`~PyTypeObject.tp_basicsize`. Example:
642-
suppose a type implements an array of ``double``. :c:member:`~PyTypeObject.tp_itemsize` is
643-
``sizeof(double)``. It is the programmer's responsibility that
644-
:c:member:`~PyTypeObject.tp_basicsize` is a multiple of ``sizeof(double)`` (assuming this is the
645-
alignment requirement for ``double``).
641+
The :c:member:`~PyVarObject.ob_size` field should be accessed using
642+
the :c:func:`Py_SIZE()` and :c:func:`Py_SET_SIZE()` macros.
646643

647-
For any type with variable-length instances, this field must not be ``NULL``.
644+
Also, the presence of an :c:member:`~PyVarObject.ob_size` field in the
645+
instance layout doesn't mean that the instance structure is variable-length.
646+
For example, the :py:type:`list` type has fixed-length instances, yet those
647+
instances have a :c:member:`~PyVarObject.ob_size` field.
648+
(As with :py:type:`int`, avoid reading lists' :c:member:`!ob_size` directly.
649+
Call :c:func:`PyList_Size` instead.)
650+
651+
The :c:member:`!tp_basicsize` includes size needed for data of the type's
652+
:c:member:`~PyTypeObject.tp_base`, plus any extra data needed
653+
by each instance.
654+
655+
The correct way to set :c:member:`!tp_basicsize` is to use the
656+
``sizeof`` operator on the struct used to declare the instance layout.
657+
This struct must include the struct used to declare the base type.
658+
In other words, :c:member:`!tp_basicsize` must be greater than or equal
659+
to the base's :c:member:`!tp_basicsize`.
660+
661+
Since every type is a subtype of :py:type:`object`, this struct must
662+
include :c:type:`PyObject` or :c:type:`PyVarObject` (depending on
663+
whether :c:member:`~PyVarObject.ob_size` should be included). These are
664+
usually defined by the macro :c:macro:`PyObject_HEAD` or
665+
:c:macro:`PyObject_VAR_HEAD`, respectively.
666+
667+
The basic size does not include the GC header size, as that header is not
668+
part of :c:macro:`PyObject_HEAD`.
669+
670+
For cases where struct used to declare the base type is unknown,
671+
see :c:member:`PyType_Spec.basicsize` and :c:func:`PyType_FromMetaclass`.
672+
673+
Notes about alignment:
674+
675+
- :c:member:`!tp_basicsize` must be a multiple of ``_Alignof(PyObject)``.
676+
When using ``sizeof`` on a ``struct`` that includes
677+
:c:macro:`PyObject_HEAD`, as recommended, the compiler ensures this.
678+
When not using a C ``struct``, or when using compiler
679+
extensions like ``__attribute__((packed))``, it is up to you.
680+
- If the variable items require a particular alignment,
681+
:c:member:`!tp_basicsize` and :c:member:`!tp_itemsize` must each be a
682+
multiple of that alignment.
683+
For example, if a type's variable part stores a ``double``, it is
684+
your responsibility that both fields are a multiple of
685+
``_Alignof(double)``.
648686

649687
**Inheritance:**
650688

651-
These fields are inherited separately by subtypes. If the base type has a
652-
non-zero :c:member:`~PyTypeObject.tp_itemsize`, it is generally not safe to set
689+
These fields are inherited separately by subtypes.
690+
(That is, if the field is set to zero, :c:func:`PyType_Ready` will copy
691+
the value from the base type, indicating that the instances do not
692+
need additional storage.)
693+
694+
If the base type has a non-zero :c:member:`~PyTypeObject.tp_itemsize`, it is generally not safe to set
653695
:c:member:`~PyTypeObject.tp_itemsize` to a different non-zero value in a subtype (though this
654696
depends on the implementation of the base type).
655697

Doc/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787

8888
# Minimum version of sphinx required
8989
# Keep this version in sync with ``Doc/requirements.txt``.
90-
needs_sphinx = '8.1.3'
90+
needs_sphinx = '8.2.0'
9191

9292
# Create table of contents entries for domain objects (e.g. functions, classes,
9393
# attributes, etc.). Default is True.

Doc/extending/embedding.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,8 @@ interesting part with respect to embedding Python starts with ::
177177

178178
After initializing the interpreter, the script is loaded using
179179
:c:func:`PyImport_Import`. This routine needs a Python string as its argument,
180-
which is constructed using the :c:func:`PyUnicode_FromString` data conversion
181-
routine. ::
180+
which is constructed using the :c:func:`PyUnicode_DecodeFSDefault` data
181+
conversion routine. ::
182182

183183
pFunc = PyObject_GetAttrString(pModule, argv[2]);
184184
/* pFunc is a new reference */

Doc/library/asyncio-subprocess.rst

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ Creating Subprocesses
6868
Create a subprocess.
6969

7070
The *limit* argument sets the buffer limit for :class:`StreamReader`
71-
wrappers for :attr:`Process.stdout` and :attr:`Process.stderr`
71+
wrappers for :attr:`~asyncio.subprocess.Process.stdout` and :attr:`~asyncio.subprocess.Process.stderr`
7272
(if :const:`subprocess.PIPE` is passed to *stdout* and *stderr* arguments).
7373

7474
Return a :class:`~asyncio.subprocess.Process` instance.
@@ -87,7 +87,7 @@ Creating Subprocesses
8787
Run the *cmd* shell command.
8888

8989
The *limit* argument sets the buffer limit for :class:`StreamReader`
90-
wrappers for :attr:`Process.stdout` and :attr:`Process.stderr`
90+
wrappers for :attr:`~asyncio.subprocess.Process.stdout` and :attr:`~asyncio.subprocess.Process.stderr`
9191
(if :const:`subprocess.PIPE` is passed to *stdout* and *stderr* arguments).
9292

9393
Return a :class:`~asyncio.subprocess.Process` instance.
@@ -132,12 +132,12 @@ Constants
132132

133133
If *PIPE* is passed to *stdin* argument, the
134134
:attr:`Process.stdin <asyncio.subprocess.Process.stdin>` attribute
135-
will point to a :class:`StreamWriter` instance.
135+
will point to a :class:`~asyncio.StreamWriter` instance.
136136

137137
If *PIPE* is passed to *stdout* or *stderr* arguments, the
138138
:attr:`Process.stdout <asyncio.subprocess.Process.stdout>` and
139139
:attr:`Process.stderr <asyncio.subprocess.Process.stderr>`
140-
attributes will point to :class:`StreamReader` instances.
140+
attributes will point to :class:`~asyncio.StreamReader` instances.
141141

142142
.. data:: asyncio.subprocess.STDOUT
143143
:module:
@@ -165,7 +165,7 @@ their completion.
165165
:module:
166166

167167
An object that wraps OS processes created by the
168-
:func:`create_subprocess_exec` and :func:`create_subprocess_shell`
168+
:func:`~asyncio.create_subprocess_exec` and :func:`~asyncio.create_subprocess_shell`
169169
functions.
170170

171171
This class is designed to have a similar API to the
@@ -263,24 +263,24 @@ their completion.
263263

264264
Kill the child process.
265265

266-
On POSIX systems this method sends :py:data:`SIGKILL` to the child
266+
On POSIX systems this method sends :py:data:`~signal.SIGKILL` to the child
267267
process.
268268

269269
On Windows this method is an alias for :meth:`terminate`.
270270

271271
.. attribute:: stdin
272272

273-
Standard input stream (:class:`StreamWriter`) or ``None``
273+
Standard input stream (:class:`~asyncio.StreamWriter`) or ``None``
274274
if the process was created with ``stdin=None``.
275275

276276
.. attribute:: stdout
277277

278-
Standard output stream (:class:`StreamReader`) or ``None``
278+
Standard output stream (:class:`~asyncio.StreamReader`) or ``None``
279279
if the process was created with ``stdout=None``.
280280

281281
.. attribute:: stderr
282282

283-
Standard error stream (:class:`StreamReader`) or ``None``
283+
Standard error stream (:class:`~asyncio.StreamReader`) or ``None``
284284
if the process was created with ``stderr=None``.
285285

286286
.. warning::
@@ -296,7 +296,7 @@ their completion.
296296

297297
Process identification number (PID).
298298

299-
Note that for processes created by the :func:`create_subprocess_shell`
299+
Note that for processes created by the :func:`~asyncio.create_subprocess_shell`
300300
function, this attribute is the PID of the spawned shell.
301301

302302
.. attribute:: returncode

Doc/library/cmdline.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ The following modules have a command-line interface.
1313
* :mod:`cProfile`: see :ref:`profile <profile-cli>`
1414
* :ref:`difflib <difflib-interface>`
1515
* :ref:`dis <dis-cli>`
16-
* :mod:`doctest`
16+
* :ref:`doctest <doctest-cli>`
1717
* :mod:`!encodings.rot_13`
1818
* :mod:`ensurepip`
1919
* :mod:`filecmp`

Doc/library/copy.rst

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,22 @@ pickle functions from the :mod:`copyreg` module.
8080
single: __deepcopy__() (copy protocol)
8181

8282
In order for a class to define its own copy implementation, it can define
83-
special methods :meth:`__copy__` and :meth:`__deepcopy__`. The former is called
84-
to implement the shallow copy operation; no additional arguments are passed.
85-
The latter is called to implement the deep copy operation; it is passed one
86-
argument, the ``memo`` dictionary. If the :meth:`__deepcopy__` implementation needs
87-
to make a deep copy of a component, it should call the :func:`deepcopy` function
88-
with the component as first argument and the memo dictionary as second argument.
89-
The memo dictionary should be treated as an opaque object.
83+
special methods :meth:`~object.__copy__` and :meth:`~object.__deepcopy__`.
84+
85+
.. method:: object.__copy__(self)
86+
:noindexentry:
87+
88+
Called to implement the shallow copy operation;
89+
no additional arguments are passed.
90+
91+
.. method:: object.__deepcopy__(self, memo)
92+
:noindexentry:
93+
94+
Called to implement the deep copy operation; it is passed one
95+
argument, the *memo* dictionary. If the ``__deepcopy__`` implementation needs
96+
to make a deep copy of a component, it should call the :func:`deepcopy` function
97+
with the component as first argument and the *memo* dictionary as second argument.
98+
The *memo* dictionary should be treated as an opaque object.
9099

91100

92101
.. seealso::

0 commit comments

Comments
 (0)