Skip to content

Commit 300e28b

Browse files
authored
Merge branch 'main' into main
2 parents 2336deb + 79e479c commit 300e28b

19 files changed

+221
-112
lines changed

Doc/c-api/init.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,6 +1287,7 @@ function. You can create and destroy them using the following functions:
12871287
in any thread where the sub-interpreter is currently active.
12881288
Otherwise only multi-phase init extension modules
12891289
(see :pep:`489`) may be imported.
1290+
(Also see :c:macro:`Py_mod_multiple_interpreters`.)
12901291
12911292
This must be ``1`` (non-zero) if
12921293
:c:member:`~PyInterpreterConfig.use_main_obmalloc` is ``0``.

Doc/c-api/memory.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,10 @@ Customize Memory Allocators
476476
thread-safe: the :term:`GIL <global interpreter lock>` is not held when the
477477
allocator is called.
478478
479+
For the remaining domains, the allocator must also be thread-safe:
480+
the allocator may be called in different interpreters that do not
481+
share a ``GIL``.
482+
479483
If the new allocator is not a hook (does not call the previous allocator),
480484
the :c:func:`PyMem_SetupDebugHooks` function must be called to reinstall the
481485
debug hooks on top on the new allocator.
@@ -500,6 +504,8 @@ Customize Memory Allocators
500504
**must** wrap the existing allocator. Substituting the current
501505
allocator for some other arbitrary one is **not supported**.
502506
507+
.. versionchanged:: 3.12
508+
All allocators must be thread-safe.
503509
504510
505511
.. c:function:: void PyMem_SetupDebugHooks(void)

Doc/c-api/module.rst

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,37 @@ The available slot types are:
376376
If multiple ``Py_mod_exec`` slots are specified, they are processed in the
377377
order they appear in the *m_slots* array.
378378
379+
.. c:macro:: Py_mod_multiple_interpreters
380+
381+
Specifies one of the following values:
382+
383+
.. c:macro:: Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED
384+
385+
The module does not support being imported in subinterpreters.
386+
387+
.. c:macro:: Py_MOD_MULTIPLE_INTERPRETERS_SUPPORTED
388+
389+
The module supports being imported in subinterpreters,
390+
but only when they share the main interpreter's GIL.
391+
(See :ref:`isolating-extensions-howto`.)
392+
393+
.. c:macro:: Py_MOD_PER_INTERPRETER_GIL_SUPPORTED
394+
395+
The module supports being imported in subinterpreters,
396+
even when they have their own GIL.
397+
(See :ref:`isolating-extensions-howto`.)
398+
399+
This slot determines whether or not importing this module
400+
in a subinterpreter will fail.
401+
402+
Multiple ``Py_mod_multiple_interpreters`` slots may not be specified
403+
in one module definition.
404+
405+
If ``Py_mod_multiple_interpreters`` is not specified, the import
406+
machinery defaults to ``Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED``.
407+
408+
.. versionadded:: 3.12
409+
379410
See :PEP:`489` for more details on multi-phase initialization.
380411
381412
Low-level module creation functions

Doc/howto/clinic.rst

Lines changed: 4 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ Argument Clinic How-To
1313
.. topic:: Abstract
1414

1515
Argument Clinic is a preprocessor for CPython C files.
16-
Its purpose is to automate all the boilerplate involved
17-
with writing argument parsing code for "builtins",
16+
It was introduced in Python 3.4 with :pep:`436`,
17+
in order to provide introspection signatures,
18+
and to generate performant and tailor-made boilerplate code
19+
for argument parsing in CPython builtins,
1820
module level functions, and class methods.
1921
This document is divided in four major sections:
2022

@@ -44,58 +46,6 @@ Argument Clinic How-To
4446
Background
4547
==========
4648

47-
48-
The goals of Argument Clinic
49-
----------------------------
50-
51-
Argument Clinic's primary goal
52-
is to take over responsibility for all argument parsing code
53-
inside CPython. This means that, when you convert a function
54-
to work with Argument Clinic, that function should no longer
55-
do any of its own argument parsing—the code generated by
56-
Argument Clinic should be a "black box" to you, where CPython
57-
calls in at the top, and your code gets called at the bottom,
58-
with ``PyObject *args`` (and maybe ``PyObject *kwargs``)
59-
magically converted into the C variables and types you need.
60-
61-
In order for Argument Clinic to accomplish its primary goal,
62-
it must be easy to use. Currently, working with CPython's
63-
argument parsing library is a chore, requiring maintaining
64-
redundant information in a surprising number of places.
65-
When you use Argument Clinic, you don't have to repeat yourself.
66-
67-
Obviously, no one would want to use Argument Clinic unless
68-
it's solving their problem—and without creating new problems of
69-
its own.
70-
So it's paramount that Argument Clinic generate correct code.
71-
It'd be nice if the code was faster, too, but at the very least
72-
it should not introduce a major speed regression. (Eventually Argument
73-
Clinic *should* make a major speedup possible—we could
74-
rewrite its code generator to produce tailor-made argument
75-
parsing code, rather than calling the general-purpose CPython
76-
argument parsing library. That would make for the fastest
77-
argument parsing possible!)
78-
79-
Additionally, Argument Clinic must be flexible enough to
80-
work with any approach to argument parsing. Python has
81-
some functions with some very strange parsing behaviors;
82-
Argument Clinic's goal is to support all of them.
83-
84-
Finally, the original motivation for Argument Clinic was
85-
to provide introspection "signatures" for CPython builtins.
86-
It used to be, the introspection query functions would throw
87-
an exception if you passed in a builtin. With Argument
88-
Clinic, that's a thing of the past!
89-
90-
One idea you should keep in mind, as you work with
91-
Argument Clinic: the more information you give it, the
92-
better job it'll be able to do.
93-
Argument Clinic is admittedly relatively simple right
94-
now. But as it evolves it will get more sophisticated,
95-
and it should be able to do many interesting and smart
96-
things with all the information you give it.
97-
98-
9949
Basic concepts
10050
--------------
10151

Doc/howto/isolating-extensions.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
.. highlight:: c
22

3+
.. _isolating-extensions-howto:
4+
35
***************************
46
Isolating Extension Modules
57
***************************

Doc/library/typing.rst

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,31 @@ using ``[]``.
849849
concat(b"foo", b"bar") # OK, output has type 'bytes'
850850
concat("foo", b"bar") # Error, cannot mix str and bytes
851851

852+
Note that, despite its name, ``AnyStr`` has nothing to do with the
853+
:class:`Any` type, nor does it mean "any string". In particular, ``AnyStr``
854+
and ``str | bytes`` are different from each other and have different use
855+
cases::
856+
857+
# Invalid use of AnyStr:
858+
# The type variable is used only once in the function signature,
859+
# so cannot be "solved" by the type checker
860+
def greet_bad(cond: bool) -> AnyStr:
861+
return "hi there!" if cond else b"greetings!"
862+
863+
# The better way of annotating this function:
864+
def greet_proper(cond: bool) -> str | bytes:
865+
return "hi there!" if cond else b"greetings!"
866+
867+
.. deprecated-removed:: 3.13 3.18
868+
Deprecated in favor of the new :ref:`type parameter syntax <type-params>`.
869+
Use ``class A[T: (str, bytes)]: ...`` instead of importing ``AnyStr``. See
870+
:pep:`695` for more details.
871+
872+
In Python 3.16, ``AnyStr`` will be removed from ``typing.__all__``, and
873+
deprecation warnings will be emitted at runtime when it is accessed or
874+
imported from ``typing``. ``AnyStr`` will be removed from ``typing``
875+
in Python 3.18.
876+
852877
.. data:: LiteralString
853878

854879
Special type that includes only literal strings.
@@ -3685,3 +3710,7 @@ convenience. This is subject to change, and not all deprecations are listed.
36853710
- 3.13
36863711
- 3.15
36873712
- :gh:`106309`
3713+
* - :data:`typing.AnyStr`
3714+
- 3.13
3715+
- 3.18
3716+
- :gh:`105578`

Doc/whatsnew/3.12.rst

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ New grammar features:
6969

7070
* :pep:`701`: Syntactic formalization of f-strings
7171

72+
Interpreter improvements:
73+
74+
* :ref:`whatsnew312-pep684`
75+
7276
New typing features:
7377

7478
* :pep:`688`: Making the buffer protocol accessible in Python
@@ -276,6 +280,36 @@ The new :class:`inspect.BufferFlags` enum represents the flags that
276280
can be used to customize buffer creation.
277281
(Contributed by Jelle Zijlstra in :gh:`102500`.)
278282

283+
.. _whatsnew312-pep684:
284+
285+
PEP 684: A Per-Interpreter GIL
286+
------------------------------
287+
288+
Sub-interpreters may now be created with a unique GIL per interpreter.
289+
This allows Python programs to take full advantage of multiple CPU
290+
cores.
291+
292+
Use the new :c:func:`Py_NewInterpreterFromConfig` function to
293+
create an interpreter with its own GIL::
294+
295+
PyInterpreterConfig config = {
296+
.check_multi_interp_extensions = 1,
297+
.gil = PyInterpreterConfig_OWN_GIL,
298+
};
299+
PyThreadState *tstate = NULL;
300+
PyStatus status = Py_NewInterpreterFromConfig(&tstate, &config);
301+
if (PyStatus_Exception(status)) {
302+
return -1;
303+
}
304+
/* The new interpeter is now active in the current thread. */
305+
306+
For further examples how to use the C-API for sub-interpreters with a
307+
per-interpreter GIL, see :source:`Modules/_xxsubinterpretersmodule.c`.
308+
309+
A Python API is anticipated for 3.13. (See :pep:`554`.)
310+
311+
(Contributed by Eric Snow in :gh:`104210`, etc.)
312+
279313
New Features Related to Type Hints
280314
==================================
281315

@@ -1234,6 +1268,10 @@ Removed
12341268

12351269
(Contributed by Pradyun Gedam in :gh:`95299`.)
12361270

1271+
* :mod:`enum`: Remove ``EnumMeta.__getattr__``, which is no longer needed for
1272+
enum attribute access.
1273+
(Contributed by Ethan Furman in :gh:`95083`.)
1274+
12371275
* :mod:`ftplib`: Remove the ``FTP_TLS.ssl_version`` class attribute: use the
12381276
*context* parameter instead.
12391277
(Contributed by Victor Stinner in :gh:`94172`.)
@@ -1738,6 +1776,12 @@ New Features
17381776

17391777
(Contributed by Eddie Elizondo in :gh:`84436`.)
17401778

1779+
* :pep:`684`: Added the new :c:func:`Py_NewInterpreterFromConfig`
1780+
function and :c:type:`PyInterpreterConfig`, which may be used
1781+
to create sub-interpreters with their own GILs.
1782+
(See :ref:`whatsnew312-pep684` for more info.)
1783+
(Contributed by Eric Snow in :gh:`104110`.)
1784+
17411785
* In the limited C API version 3.12, :c:func:`Py_INCREF` and
17421786
:c:func:`Py_DECREF` functions are now implemented as opaque function calls to
17431787
hide implementation details.
@@ -1876,6 +1920,13 @@ Porting to Python 3.12
18761920
* :c:func:`PyUnstable_Long_IsCompact`
18771921
* :c:func:`PyUnstable_Long_CompactValue`
18781922

1923+
* Custom allocators, set via :c:func:`PyMem_SetAllocator`, are now
1924+
required to be thread-safe, regardless of memory domain. Allocators
1925+
that don't have their own state, including "hooks", are not affected.
1926+
If your custom allocator is not already thread-safe and you need
1927+
guidance then please create a new GitHub issue
1928+
and CC ``@ericsnowcurrently``.
1929+
18791930
Deprecated
18801931
----------
18811932

Doc/whatsnew/3.13.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,12 @@ Deprecated
196196
has yet to be supported by any major type checkers.
197197
(Contributed by Alex Waygood in :gh:`106309`.)
198198

199+
* :data:`typing.AnyStr` is deprecated. In Python 3.16, it will be removed from
200+
``typing.__all__``, and a :exc:`DeprecationWarning` will be emitted when it
201+
is imported or accessed. It will be removed entirely in Python 3.18. Use
202+
the new :ref:`type parameter syntax <type-params>` instead.
203+
(Contributed by Michael The in :gh:`107116`.)
204+
199205
* :mod:`wave`: Deprecate the ``getmark()``, ``setmark()`` and ``getmarkers()``
200206
methods of the :class:`wave.Wave_read` and :class:`wave.Wave_write` classes.
201207
They will be removed in Python 3.15.

Include/internal/pycore_opcode_metadata.h

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,26 @@
3535
#define _BINARY_OP_ADD_UNICODE 311
3636
#define _LOAD_LOCALS 312
3737
#define _LOAD_FROM_DICT_OR_GLOBALS 313
38-
#define _SKIP_CACHE 314
39-
#define _GUARD_GLOBALS_VERSION 315
40-
#define _GUARD_BUILTINS_VERSION 316
41-
#define _LOAD_GLOBAL_MODULE 317
42-
#define _LOAD_GLOBAL_BUILTINS 318
43-
#define _GUARD_TYPE_VERSION 319
44-
#define _CHECK_MANAGED_OBJECT_HAS_VALUES 320
45-
#define _LOAD_ATTR_INSTANCE_VALUE 321
46-
#define IS_NONE 322
47-
#define _ITER_CHECK_LIST 323
48-
#define _IS_ITER_EXHAUSTED_LIST 324
49-
#define _ITER_NEXT_LIST 325
50-
#define _ITER_CHECK_TUPLE 326
51-
#define _IS_ITER_EXHAUSTED_TUPLE 327
52-
#define _ITER_NEXT_TUPLE 328
53-
#define _ITER_CHECK_RANGE 329
54-
#define _IS_ITER_EXHAUSTED_RANGE 330
55-
#define _ITER_NEXT_RANGE 331
56-
#define _POP_JUMP_IF_FALSE 332
57-
#define _POP_JUMP_IF_TRUE 333
58-
#define JUMP_TO_TOP 334
38+
#define _GUARD_GLOBALS_VERSION 314
39+
#define _GUARD_BUILTINS_VERSION 315
40+
#define _LOAD_GLOBAL_MODULE 316
41+
#define _LOAD_GLOBAL_BUILTINS 317
42+
#define _GUARD_TYPE_VERSION 318
43+
#define _CHECK_MANAGED_OBJECT_HAS_VALUES 319
44+
#define _LOAD_ATTR_INSTANCE_VALUE 320
45+
#define IS_NONE 321
46+
#define _ITER_CHECK_LIST 322
47+
#define _IS_ITER_EXHAUSTED_LIST 323
48+
#define _ITER_NEXT_LIST 324
49+
#define _ITER_CHECK_TUPLE 325
50+
#define _IS_ITER_EXHAUSTED_TUPLE 326
51+
#define _ITER_NEXT_TUPLE 327
52+
#define _ITER_CHECK_RANGE 328
53+
#define _IS_ITER_EXHAUSTED_RANGE 329
54+
#define _ITER_NEXT_RANGE 330
55+
#define _POP_JUMP_IF_FALSE 331
56+
#define _POP_JUMP_IF_TRUE 332
57+
#define JUMP_TO_TOP 333
5958

6059
#ifndef NEED_OPCODE_METADATA
6160
extern int _PyOpcode_num_popped(int opcode, int oparg, bool jump);
@@ -945,7 +944,7 @@ _PyOpcode_num_pushed(int opcode, int oparg, bool jump) {
945944
}
946945
#endif
947946

948-
enum InstructionFormat { INSTR_FMT_IB, INSTR_FMT_IBC, INSTR_FMT_IBC00, INSTR_FMT_IBC000, INSTR_FMT_IBC00000, INSTR_FMT_IBC00000000, INSTR_FMT_IX, INSTR_FMT_IXC, INSTR_FMT_IXC0, INSTR_FMT_IXC00, INSTR_FMT_IXC000 };
947+
enum InstructionFormat { INSTR_FMT_IB, INSTR_FMT_IBC, INSTR_FMT_IBC00, INSTR_FMT_IBC000, INSTR_FMT_IBC00000000, INSTR_FMT_IX, INSTR_FMT_IXC, INSTR_FMT_IXC0, INSTR_FMT_IXC00, INSTR_FMT_IXC000 };
949948

950949
#define IS_VALID_OPCODE(OP) \
951950
(((OP) >= 0) && ((OP) < OPCODE_METADATA_SIZE) && \
@@ -1279,8 +1278,8 @@ const struct opcode_macro_expansion _PyOpcode_macro_expansion[OPCODE_MACRO_EXPAN
12791278
[LOAD_NAME] = { .nuops = 2, .uops = { { _LOAD_LOCALS, 0, 0 }, { _LOAD_FROM_DICT_OR_GLOBALS, 0, 0 } } },
12801279
[LOAD_FROM_DICT_OR_GLOBALS] = { .nuops = 1, .uops = { { _LOAD_FROM_DICT_OR_GLOBALS, 0, 0 } } },
12811280
[LOAD_GLOBAL] = { .nuops = 1, .uops = { { LOAD_GLOBAL, 0, 0 } } },
1282-
[LOAD_GLOBAL_MODULE] = { .nuops = 4, .uops = { { _SKIP_CACHE, 0, 0 }, { _GUARD_GLOBALS_VERSION, 1, 1 }, { _SKIP_CACHE, 0, 0 }, { _LOAD_GLOBAL_MODULE, 1, 3 } } },
1283-
[LOAD_GLOBAL_BUILTIN] = { .nuops = 4, .uops = { { _SKIP_CACHE, 0, 0 }, { _GUARD_GLOBALS_VERSION, 1, 1 }, { _GUARD_BUILTINS_VERSION, 1, 2 }, { _LOAD_GLOBAL_BUILTINS, 1, 3 } } },
1281+
[LOAD_GLOBAL_MODULE] = { .nuops = 2, .uops = { { _GUARD_GLOBALS_VERSION, 1, 1 }, { _LOAD_GLOBAL_MODULE, 1, 3 } } },
1282+
[LOAD_GLOBAL_BUILTIN] = { .nuops = 3, .uops = { { _GUARD_GLOBALS_VERSION, 1, 1 }, { _GUARD_BUILTINS_VERSION, 1, 2 }, { _LOAD_GLOBAL_BUILTINS, 1, 3 } } },
12841283
[DELETE_FAST] = { .nuops = 1, .uops = { { DELETE_FAST, 0, 0 } } },
12851284
[DELETE_DEREF] = { .nuops = 1, .uops = { { DELETE_DEREF, 0, 0 } } },
12861285
[LOAD_FROM_DICT_OR_DEREF] = { .nuops = 1, .uops = { { LOAD_FROM_DICT_OR_DEREF, 0, 0 } } },
@@ -1302,7 +1301,7 @@ const struct opcode_macro_expansion _PyOpcode_macro_expansion[OPCODE_MACRO_EXPAN
13021301
[LOAD_SUPER_ATTR_ATTR] = { .nuops = 1, .uops = { { LOAD_SUPER_ATTR_ATTR, 0, 0 } } },
13031302
[LOAD_SUPER_ATTR_METHOD] = { .nuops = 1, .uops = { { LOAD_SUPER_ATTR_METHOD, 0, 0 } } },
13041303
[LOAD_ATTR] = { .nuops = 1, .uops = { { LOAD_ATTR, 0, 0 } } },
1305-
[LOAD_ATTR_INSTANCE_VALUE] = { .nuops = 4, .uops = { { _SKIP_CACHE, 0, 0 }, { _GUARD_TYPE_VERSION, 2, 1 }, { _CHECK_MANAGED_OBJECT_HAS_VALUES, 0, 0 }, { _LOAD_ATTR_INSTANCE_VALUE, 1, 3 } } },
1304+
[LOAD_ATTR_INSTANCE_VALUE] = { .nuops = 3, .uops = { { _GUARD_TYPE_VERSION, 2, 1 }, { _CHECK_MANAGED_OBJECT_HAS_VALUES, 0, 0 }, { _LOAD_ATTR_INSTANCE_VALUE, 1, 3 } } },
13061305
[COMPARE_OP] = { .nuops = 1, .uops = { { COMPARE_OP, 0, 0 } } },
13071306
[COMPARE_OP_FLOAT] = { .nuops = 1, .uops = { { COMPARE_OP_FLOAT, 0, 0 } } },
13081307
[COMPARE_OP_INT] = { .nuops = 1, .uops = { { COMPARE_OP_INT, 0, 0 } } },
@@ -1356,7 +1355,6 @@ const char * const _PyOpcode_uop_name[OPCODE_UOP_NAME_SIZE] = {
13561355
[_BINARY_OP_ADD_UNICODE] = "_BINARY_OP_ADD_UNICODE",
13571356
[_LOAD_LOCALS] = "_LOAD_LOCALS",
13581357
[_LOAD_FROM_DICT_OR_GLOBALS] = "_LOAD_FROM_DICT_OR_GLOBALS",
1359-
[_SKIP_CACHE] = "_SKIP_CACHE",
13601358
[_GUARD_GLOBALS_VERSION] = "_GUARD_GLOBALS_VERSION",
13611359
[_GUARD_BUILTINS_VERSION] = "_GUARD_BUILTINS_VERSION",
13621360
[_LOAD_GLOBAL_MODULE] = "_LOAD_GLOBAL_MODULE",

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy