Skip to content

Commit b2ab967

Browse files
authored
Merge branch 'main' into issue-65329
2 parents d1ea577 + 6441365 commit b2ab967

File tree

80 files changed

+1280
-699
lines changed

Some content is hidden

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

80 files changed

+1280
-699
lines changed

Doc/extending/newtypes_tutorial.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ standard Python floats::
8888
The second bit is the definition of the type object. ::
8989

9090
static PyTypeObject CustomType = {
91-
PyVarObject_HEAD_INIT(NULL, 0)
91+
.ob_base = PyVarObject_HEAD_INIT(NULL, 0)
9292
.tp_name = "custom.Custom",
9393
.tp_doc = PyDoc_STR("Custom objects"),
9494
.tp_basicsize = sizeof(CustomObject),
@@ -109,7 +109,7 @@ common practice to not specify them explicitly unless you need them.
109109

110110
We're going to pick it apart, one field at a time::
111111

112-
PyVarObject_HEAD_INIT(NULL, 0)
112+
.ob_base = PyVarObject_HEAD_INIT(NULL, 0)
113113

114114
This line is mandatory boilerplate to initialize the ``ob_base``
115115
field mentioned above. ::

Doc/howto/enum.rst

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -988,12 +988,11 @@ but remain normal attributes.
988988
""""""""""""""""""""
989989

990990
Enum members are instances of their enum class, and are normally accessed as
991-
``EnumClass.member``. In Python versions starting with ``3.5`` you could access
992-
members from other members -- this practice is discouraged, is deprecated
993-
in ``3.12``, and will be removed in ``3.14``.
991+
``EnumClass.member``. In certain situations, such as writing custom enum
992+
behavior, being able to access one member directly from another is useful,
993+
and is supported.
994994

995995
.. versionchanged:: 3.5
996-
.. versionchanged:: 3.12
997996

998997

999998
Creating members that are mixed with other data types

Doc/includes/custom.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ typedef struct {
77
} CustomObject;
88

99
static PyTypeObject CustomType = {
10-
PyVarObject_HEAD_INIT(NULL, 0)
10+
.ob_base = PyVarObject_HEAD_INIT(NULL, 0)
1111
.tp_name = "custom.Custom",
1212
.tp_doc = PyDoc_STR("Custom objects"),
1313
.tp_basicsize = sizeof(CustomObject),
@@ -17,7 +17,7 @@ static PyTypeObject CustomType = {
1717
};
1818

1919
static PyModuleDef custommodule = {
20-
PyModuleDef_HEAD_INIT,
20+
.m_base = PyModuleDef_HEAD_INIT,
2121
.m_name = "custom",
2222
.m_doc = "Example module that creates an extension type.",
2323
.m_size = -1,

Doc/includes/custom2.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ static PyMethodDef Custom_methods[] = {
9090
};
9191

9292
static PyTypeObject CustomType = {
93-
PyVarObject_HEAD_INIT(NULL, 0)
93+
.ob_base = PyVarObject_HEAD_INIT(NULL, 0)
9494
.tp_name = "custom2.Custom",
9595
.tp_doc = PyDoc_STR("Custom objects"),
9696
.tp_basicsize = sizeof(CustomObject),
@@ -104,7 +104,7 @@ static PyTypeObject CustomType = {
104104
};
105105

106106
static PyModuleDef custommodule = {
107-
PyModuleDef_HEAD_INIT,
107+
.m_base =PyModuleDef_HEAD_INIT,
108108
.m_name = "custom2",
109109
.m_doc = "Example module that creates an extension type.",
110110
.m_size = -1,

Doc/includes/custom3.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ static PyMethodDef Custom_methods[] = {
130130
};
131131

132132
static PyTypeObject CustomType = {
133-
PyVarObject_HEAD_INIT(NULL, 0)
133+
.ob_base = PyVarObject_HEAD_INIT(NULL, 0)
134134
.tp_name = "custom3.Custom",
135135
.tp_doc = PyDoc_STR("Custom objects"),
136136
.tp_basicsize = sizeof(CustomObject),
@@ -145,7 +145,7 @@ static PyTypeObject CustomType = {
145145
};
146146

147147
static PyModuleDef custommodule = {
148-
PyModuleDef_HEAD_INIT,
148+
.m_base = PyModuleDef_HEAD_INIT,
149149
.m_name = "custom3",
150150
.m_doc = "Example module that creates an extension type.",
151151
.m_size = -1,

Doc/includes/custom4.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ static PyMethodDef Custom_methods[] = {
146146
};
147147

148148
static PyTypeObject CustomType = {
149-
PyVarObject_HEAD_INIT(NULL, 0)
149+
.ob_base = PyVarObject_HEAD_INIT(NULL, 0)
150150
.tp_name = "custom4.Custom",
151151
.tp_doc = PyDoc_STR("Custom objects"),
152152
.tp_basicsize = sizeof(CustomObject),
@@ -163,7 +163,7 @@ static PyTypeObject CustomType = {
163163
};
164164

165165
static PyModuleDef custommodule = {
166-
PyModuleDef_HEAD_INIT,
166+
.m_base = PyModuleDef_HEAD_INIT,
167167
.m_name = "custom4",
168168
.m_doc = "Example module that creates an extension type.",
169169
.m_size = -1,

Doc/library/bisect.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,10 @@ records in a table::
210210
>>> Movie = namedtuple('Movie', ('name', 'released', 'director'))
211211

212212
>>> movies = [
213-
... Movie('Jaws', 1975, 'Speilberg'),
213+
... Movie('Jaws', 1975, 'Spielberg'),
214214
... Movie('Titanic', 1997, 'Cameron'),
215215
... Movie('The Birds', 1963, 'Hitchcock'),
216-
... Movie('Aliens', 1986, 'Scott')
216+
... Movie('Aliens', 1986, 'Cameron')
217217
... ]
218218

219219
>>> # Find the first movie released after 1960
@@ -228,8 +228,8 @@ records in a table::
228228
>>> pprint(movies)
229229
[Movie(name='The Birds', released=1963, director='Hitchcock'),
230230
Movie(name='Love Story', released=1970, director='Hiller'),
231-
Movie(name='Jaws', released=1975, director='Speilberg'),
232-
Movie(name='Aliens', released=1986, director='Scott'),
231+
Movie(name='Jaws', released=1975, director='Spielberg'),
232+
Movie(name='Aliens', released=1986, director='Cameron'),
233233
Movie(name='Titanic', released=1997, director='Cameron')]
234234

235235
If the key function is expensive, it is possible to avoid repeated function

Doc/library/dis.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ the following command can be used to display the disassembly of
5959
3 2 LOAD_GLOBAL 1 (NULL + len)
6060
12 LOAD_FAST 0 (alist)
6161
14 CALL 1
62-
24 RETURN_VALUE
62+
22 RETURN_VALUE
6363

6464
(The "2" is a line number).
6565

Doc/library/typing.rst

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1598,15 +1598,6 @@ These are not used in annotations. They are building blocks for creating generic
15981598
import threading
15991599
assert isinstance(threading.Thread(name='Bob'), Named)
16001600

1601-
.. versionchanged:: 3.12
1602-
The internal implementation of :func:`isinstance` checks against
1603-
runtime-checkable protocols now uses :func:`inspect.getattr_static`
1604-
to look up attributes (previously, :func:`hasattr` was used).
1605-
As a result, some objects which used to be considered instances
1606-
of a runtime-checkable protocol may no longer be considered instances
1607-
of that protocol on Python 3.12+, and vice versa.
1608-
Most users are unlikely to be affected by this change.
1609-
16101601
.. note::
16111602

16121603
:func:`!runtime_checkable` will check only the presence of the required
@@ -1628,6 +1619,24 @@ These are not used in annotations. They are building blocks for creating generic
16281619

16291620
.. versionadded:: 3.8
16301621

1622+
.. versionchanged:: 3.12
1623+
The internal implementation of :func:`isinstance` checks against
1624+
runtime-checkable protocols now uses :func:`inspect.getattr_static`
1625+
to look up attributes (previously, :func:`hasattr` was used).
1626+
As a result, some objects which used to be considered instances
1627+
of a runtime-checkable protocol may no longer be considered instances
1628+
of that protocol on Python 3.12+, and vice versa.
1629+
Most users are unlikely to be affected by this change.
1630+
1631+
.. versionchanged:: 3.12
1632+
The members of a runtime-checkable protocol are now considered "frozen"
1633+
at runtime as soon as the class has been created. Monkey-patching
1634+
attributes onto a runtime-checkable protocol will still work, but will
1635+
have no impact on :func:`isinstance` checks comparing objects to the
1636+
protocol. See :ref:`"What's new in Python 3.12" <whatsnew-typing-py312>`
1637+
for more details.
1638+
1639+
16311640
Other special directives
16321641
""""""""""""""""""""""""
16331642

Doc/whatsnew/3.12.rst

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,10 @@ inspect
255255
for determining the current state of asynchronous generators.
256256
(Contributed by Thomas Krennwallner in :issue:`35759`.)
257257

258+
* The performance of :func:`inspect.getattr_static` has been considerably
259+
improved. Most calls to the function should be around 2x faster than they
260+
were in Python 3.11. (Contributed by Alex Waygood in :gh:`103193`.)
261+
258262
pathlib
259263
-------
260264

@@ -418,6 +422,8 @@ tempfile
418422
The :class:`tempfile.NamedTemporaryFile` function has a new optional parameter
419423
*delete_on_close* (Contributed by Evgeny Zorin in :gh:`58451`.)
420424

425+
.. _whatsnew-typing-py312:
426+
421427
typing
422428
------
423429

@@ -437,6 +443,39 @@ typing
437443
vice versa. Most users are unlikely to be affected by this change.
438444
(Contributed by Alex Waygood in :gh:`102433`.)
439445

446+
* The members of a runtime-checkable protocol are now considered "frozen" at
447+
runtime as soon as the class has been created. Monkey-patching attributes
448+
onto a runtime-checkable protocol will still work, but will have no impact on
449+
:func:`isinstance` checks comparing objects to the protocol. For example::
450+
451+
>>> from typing import Protocol, runtime_checkable
452+
>>> @runtime_checkable
453+
... class HasX(Protocol):
454+
... x = 1
455+
...
456+
>>> class Foo: ...
457+
...
458+
>>> f = Foo()
459+
>>> isinstance(f, HasX)
460+
False
461+
>>> f.x = 1
462+
>>> isinstance(f, HasX)
463+
True
464+
>>> HasX.y = 2
465+
>>> isinstance(f, HasX) # unchanged, even though HasX now also has a "y" attribute
466+
True
467+
468+
This change was made in order to speed up ``isinstance()`` checks against
469+
runtime-checkable protocols.
470+
471+
* The performance profile of :func:`isinstance` checks against
472+
:func:`runtime-checkable protocols <typing.runtime_checkable>` has changed
473+
significantly. Most ``isinstance()`` checks against protocols with only a few
474+
members should be at least 2x faster than in 3.11, and some may be 20x
475+
faster or more. However, ``isinstance()`` checks against protocols with seven
476+
or more members may be slower than in Python 3.11. (Contributed by Alex
477+
Waygood in :gh:`74690` and :gh:`103193`.)
478+
440479
sys
441480
---
442481

0 commit comments

Comments
 (0)