Skip to content

Commit cff7345

Browse files
authored
Merge branch '3.12' into bp-312/17d06aeb5476099bc1acd89cd6f69e239e0f9350/enum-131045
2 parents 5602620 + 7f2de07 commit cff7345

Some content is hidden

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

62 files changed

+1132
-562
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/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: 73 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ PyTypeObject Definition
473473
-----------------------
474474

475475
The structure definition for :c:type:`PyTypeObject` can be found in
476-
:file:`Include/object.h`. For convenience of reference, this repeats the
476+
:file:`Include/cpython/object.h`. For convenience of reference, this repeats the
477477
definition found there:
478478

479479
.. XXX Drop this?
@@ -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/glossary.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,10 @@ Glossary
734734
thread removes *key* from *mapping* after the test, but before the lookup.
735735
This issue can be solved with locks or by using the EAFP approach.
736736

737+
lexical analyzer
738+
739+
Formal name for the *tokenizer*; see :term:`token`.
740+
737741
list
738742
A built-in Python :term:`sequence`. Despite its name it is more akin
739743
to an array in other languages than to a linked list since access to
@@ -1212,6 +1216,17 @@ Glossary
12121216
See also :term:`binary file` for a file object able to read and write
12131217
:term:`bytes-like objects <bytes-like object>`.
12141218

1219+
token
1220+
1221+
A small unit of source code, generated by the
1222+
:ref:`lexical analyzer <lexical>` (also called the *tokenizer*).
1223+
Names, numbers, strings, operators,
1224+
newlines and similar are represented by tokens.
1225+
1226+
The :mod:`tokenize` module exposes Python's lexical analyzer.
1227+
The :mod:`token` module contains information on the various types
1228+
of tokens.
1229+
12151230
triple-quoted string
12161231
A string which is bound by three instances of either a quotation mark
12171232
(") or an apostrophe ('). While they don't provide any functionality

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::

Doc/library/datetime.rst

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2526,7 +2526,24 @@ Broadly speaking, ``d.strftime(fmt)`` acts like the :mod:`time` module's
25262526

25272527
For the :meth:`.datetime.strptime` class method, the default value is
25282528
``1900-01-01T00:00:00.000``: any components not specified in the format string
2529-
will be pulled from the default value. [#]_
2529+
will be pulled from the default value.
2530+
2531+
.. note::
2532+
When used to parse partial dates lacking a year, :meth:`~.datetime.strptime`
2533+
will raise when encountering February 29 because its default year of 1900 is
2534+
*not* a leap year. Always add a default leap year to partial date strings
2535+
before parsing.
2536+
2537+
.. doctest::
2538+
2539+
>>> from datetime import datetime
2540+
>>> value = "2/29"
2541+
>>> datetime.strptime(value, "%m/%d")
2542+
Traceback (most recent call last):
2543+
...
2544+
ValueError: day is out of range for month
2545+
>>> datetime.strptime(f"1904 {value}", "%Y %m/%d")
2546+
datetime.datetime(1904, 2, 29, 0, 0)
25302547

25312548
Using ``datetime.strptime(date_string, format)`` is equivalent to::
25322549

@@ -2652,6 +2669,11 @@ Notes:
26522669
for formats ``%d``, ``%m``, ``%H``, ``%I``, ``%M``, ``%S``, ``%j``, ``%U``,
26532670
``%W``, and ``%V``. Format ``%y`` does require a leading zero.
26542671

2672+
(10)
2673+
Parsing dates without a year using :meth:`~.datetime.strptime` will fail on
2674+
representations of February 29 as that date does not exist in the default
2675+
year of 1900.
2676+
26552677
.. rubric:: Footnotes
26562678

26572679
.. [#] If, that is, we ignore the effects of Relativity
@@ -2665,5 +2687,3 @@ Notes:
26652687
.. [#] See R. H. van Gent's `guide to the mathematics of the ISO 8601 calendar
26662688
<https://web.archive.org/web/20220531051136/https://webspace.science.uu.nl/~gent0113/calendar/isocalendar.htm>`_
26672689
for a good explanation.
2668-
2669-
.. [#] Passing ``datetime.strptime('Feb 29', '%b %d')`` will fail since 1900 is not a leap year.

Doc/library/doctest.rst

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -173,15 +173,8 @@ prohibit it by passing ``verbose=False``. In either of those cases,
173173
``sys.argv`` is not examined by :func:`testmod` (so passing ``-v`` or not
174174
has no effect).
175175

176-
There is also a command line shortcut for running :func:`testmod`. You can
177-
instruct the Python interpreter to run the doctest module directly from the
178-
standard library and pass the module name(s) on the command line::
179-
180-
python -m doctest -v example.py
181-
182-
This will import :file:`example.py` as a standalone module and run
183-
:func:`testmod` on it. Note that this may not work correctly if the file is
184-
part of a package and imports other submodules from that package.
176+
There is also a command line shortcut for running :func:`testmod`, see section
177+
:ref:`doctest-cli`.
185178

186179
For more information on :func:`testmod`, see section :ref:`doctest-basic-api`.
187180

@@ -244,16 +237,53 @@ Like :func:`testmod`, :func:`testfile`'s verbosity can be set with the
244237
``-v`` command-line switch or with the optional keyword argument
245238
*verbose*.
246239

247-
There is also a command line shortcut for running :func:`testfile`. You can
248-
instruct the Python interpreter to run the doctest module directly from the
249-
standard library and pass the file name(s) on the command line::
240+
There is also a command line shortcut for running :func:`testfile`, see section
241+
:ref:`doctest-cli`.
250242

251-
python -m doctest -v example.txt
243+
For more information on :func:`testfile`, see section :ref:`doctest-basic-api`.
252244

253-
Because the file name does not end with :file:`.py`, :mod:`doctest` infers that
254-
it must be run with :func:`testfile`, not :func:`testmod`.
255245

256-
For more information on :func:`testfile`, see section :ref:`doctest-basic-api`.
246+
.. _doctest-cli:
247+
248+
Command-line Usage
249+
------------------
250+
251+
The :mod:`doctest` module can be invoked as a script from the command line:
252+
253+
.. code-block:: bash
254+
255+
python -m doctest [-v] [-o OPTION] [-f] file [file ...]
256+
257+
.. program:: doctest
258+
259+
.. option:: -v, --verbose
260+
261+
Detailed report of all examples tried is printed to standard output,
262+
along with assorted summaries at the end::
263+
264+
python -m doctest -v example.py
265+
266+
This will import :file:`example.py` as a standalone module and run
267+
:func:`testmod` on it. Note that this may not work correctly if the
268+
file is part of a package and imports other submodules from that package.
269+
270+
If the file name does not end with :file:`.py`, :mod:`!doctest` infers
271+
that it must be run with :func:`testfile` instead::
272+
273+
python -m doctest -v example.txt
274+
275+
.. option:: -o, --option <option>
276+
277+
Option flags control various aspects of doctest's behavior, see section
278+
:ref:`doctest-options`.
279+
280+
.. versionadded:: 3.4
281+
282+
.. option:: -f, --fail-fast
283+
284+
This is shorthand for ``-o FAIL_FAST``.
285+
286+
.. versionadded:: 3.4
257287

258288

259289
.. _doctest-how-it-works:
@@ -536,9 +566,6 @@ Symbolic names for the flags are supplied as module constants, which can be
536566
The names can also be used in :ref:`doctest directives <doctest-directives>`,
537567
and may be passed to the doctest command line interface via the ``-o`` option.
538568

539-
.. versionadded:: 3.4
540-
The ``-o`` command line option.
541-
542569
The first group of options define test semantics, controlling aspects of how
543570
doctest decides whether actual output matches an example's expected output:
544571

@@ -678,11 +705,6 @@ The second group of options controls how test failures are reported:
678705
1. This flag may be useful during debugging, since examples after the first
679706
failure won't even produce debugging output.
680707

681-
The doctest command line accepts the option ``-f`` as a shorthand for ``-o
682-
FAIL_FAST``.
683-
684-
.. versionadded:: 3.4
685-
686708

687709
.. data:: REPORTING_FLAGS
688710

0 commit comments

Comments
 (0)