Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 19c6262

Browse files
authored
Merge branch 'main' into fix/uuid/mac-address-libuuid-132710
2 parents e311181 + fc7f4c3 commit 19c6262

File tree

235 files changed

+4736
-4061
lines changed

Some content is hidden

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

235 files changed

+4736
-4061
lines changed

.github/workflows/mypy.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@ on:
1414
- "Lib/tomllib/**"
1515
- "Misc/mypy/**"
1616
- "Tools/build/compute-changes.py"
17+
- "Tools/build/deepfreeze.py"
1718
- "Tools/build/generate_sbom.py"
19+
- "Tools/build/generate-build-details.py"
1820
- "Tools/build/verify_ensurepip_wheels.py"
1921
- "Tools/build/update_file.py"
22+
- "Tools/build/umarshal.py"
2023
- "Tools/cases_generator/**"
2124
- "Tools/clinic/**"
2225
- "Tools/jit/**"

.github/workflows/reusable-context.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ jobs:
9797
run: python Tools/build/compute-changes.py
9898
env:
9999
GITHUB_DEFAULT_BRANCH: ${{ github.event.repository.default_branch }}
100+
GITHUB_EVENT_NAME: ${{ github.event_name }}
100101
CCF_TARGET_REF: ${{ github.base_ref || github.event.repository.default_branch }}
101102
CCF_HEAD_REF: ${{ github.event.pull_request.head.sha || github.sha }}
102103

.pre-commit-config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ repos:
4747
exclude: Lib/test/tokenizedata/coding20731.py
4848
- id: trailing-whitespace
4949
types_or: [c, inc, python, rst]
50+
- id: trailing-whitespace
51+
files: '\.(gram)$'
5052

5153
- repo: https://github.com/python-jsonschema/check-jsonschema
5254
rev: 0.33.0

Doc/c-api/init.rst

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ to 1 and ``-bb`` sets :c:data:`Py_BytesWarningFlag` to 2.
197197

198198
Set by the :option:`-i` option.
199199

200-
.. deprecated:: 3.12
200+
.. deprecated-removed:: 3.12 3.15
201201

202202
.. c:var:: int Py_IsolatedFlag
203203
@@ -919,8 +919,36 @@ Note that the ``PyGILState_*`` functions assume there is only one global
919919
interpreter (created automatically by :c:func:`Py_Initialize`). Python
920920
supports the creation of additional interpreters (using
921921
:c:func:`Py_NewInterpreter`), but mixing multiple interpreters and the
922-
``PyGILState_*`` API is unsupported.
922+
``PyGILState_*`` API is unsupported. This is because :c:func:`PyGILState_Ensure`
923+
and similar functions default to :term:`attaching <attached thread state>` a
924+
:term:`thread state` for the main interpreter, meaning that the thread can't safely
925+
interact with the calling subinterpreter.
926+
927+
Supporting subinterpreters in non-Python threads
928+
------------------------------------------------
929+
930+
If you would like to support subinterpreters with non-Python created threads, you
931+
must use the ``PyThreadState_*`` API instead of the traditional ``PyGILState_*``
932+
API.
933+
934+
In particular, you must store the interpreter state from the calling
935+
function and pass it to :c:func:`PyThreadState_New`, which will ensure that
936+
the :term:`thread state` is targeting the correct interpreter::
937+
938+
/* The return value of PyInterpreterState_Get() from the
939+
function that created this thread. */
940+
PyInterpreterState *interp = ThreadData->interp;
941+
PyThreadState *tstate = PyThreadState_New(interp);
942+
PyThreadState_Swap(tstate);
943+
944+
/* GIL of the subinterpreter is now held.
945+
Perform Python actions here. */
946+
result = CallSomeFunction();
947+
/* evaluate result or handle exception */
923948
949+
/* Destroy the thread state. No Python API allowed beyond this point. */
950+
PyThreadState_Clear(tstate);
951+
PyThreadState_DeleteCurrent();
924952
925953
.. _fork-and-threads:
926954
@@ -1097,6 +1125,10 @@ code, or when embedding the Python interpreter:
10971125
.. seealso:
10981126
:c:func:`PyEval_ReleaseThread`
10991127
1128+
.. note::
1129+
Similar to :c:func:`PyGILState_Ensure`, this function will hang the
1130+
thread if the runtime is finalizing.
1131+
11001132
11011133
The following functions use thread-local storage, and are not compatible
11021134
with sub-interpreters:
@@ -1123,10 +1155,10 @@ with sub-interpreters:
11231155
When the function returns, there will be an :term:`attached thread state`
11241156
and the thread will be able to call arbitrary Python code. Failure is a fatal error.
11251157
1126-
.. note::
1127-
Calling this function from a thread when the runtime is finalizing will
1128-
hang the thread until the program exits, even if the thread was not
1129-
created by Python. Refer to
1158+
.. warning::
1159+
Calling this function when the runtime is finalizing is unsafe. Doing
1160+
so will either hang the thread until the program ends, or fully crash
1161+
the interpreter in rare cases. Refer to
11301162
:ref:`cautions-regarding-runtime-finalization` for more details.
11311163
11321164
.. versionchanged:: 3.14
@@ -1143,28 +1175,37 @@ with sub-interpreters:
11431175
Every call to :c:func:`PyGILState_Ensure` must be matched by a call to
11441176
:c:func:`PyGILState_Release` on the same thread.
11451177
1146-
11471178
.. c:function:: PyThreadState* PyGILState_GetThisThreadState()
11481179
11491180
Get the :term:`attached thread state` for this thread. May return ``NULL`` if no
11501181
GILState API has been used on the current thread. Note that the main thread
11511182
always has such a thread-state, even if no auto-thread-state call has been
11521183
made on the main thread. This is mainly a helper/diagnostic function.
11531184
1154-
.. seealso: :c:func:`PyThreadState_Get``
1185+
.. note::
1186+
This function does not account for :term:`thread states <thread state>` created
1187+
by something other than :c:func:`PyGILState_Ensure` (such as :c:func:`PyThreadState_New`).
1188+
Prefer :c:func:`PyThreadState_Get` or :c:func:`PyThreadState_GetUnchecked`
1189+
for most cases.
11551190
1191+
.. seealso: :c:func:`PyThreadState_Get``
11561192
11571193
.. c:function:: int PyGILState_Check()
11581194
11591195
Return ``1`` if the current thread is holding the :term:`GIL` and ``0`` otherwise.
11601196
This function can be called from any thread at any time.
1161-
Only if it has had its Python thread state initialized and currently is
1162-
holding the :term:`GIL` will it return ``1``.
1197+
Only if it has had its :term:`thread state <attached thread state>` initialized
1198+
via :c:func:`PyGILState_Ensure` will it return ``1``.
11631199
This is mainly a helper/diagnostic function. It can be useful
11641200
for example in callback contexts or memory allocation functions when
11651201
knowing that the :term:`GIL` is locked can allow the caller to perform sensitive
11661202
actions or otherwise behave differently.
11671203
1204+
.. note::
1205+
If the current Python process has ever created a subinterpreter, this
1206+
function will *always* return ``1``. Prefer :c:func:`PyThreadState_GetUnchecked`
1207+
for most cases.
1208+
11681209
.. versionadded:: 3.4
11691210
11701211

Doc/c-api/unicode.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,17 @@ APIs:
645645
difference being that it decrements the reference count of *right* by one.
646646
647647
648+
.. c:function:: PyObject* PyUnicode_BuildEncodingMap(PyObject* string)
649+
650+
Return a mapping suitable for decoding a custom single-byte encoding.
651+
Given a Unicode string *string* of up to 256 characters representing an encoding
652+
table, returns either a compact internal mapping object or a dictionary
653+
mapping character ordinals to byte values. Raises a :exc:`TypeError` and
654+
return ``NULL`` on invalid input.
655+
656+
.. versionadded:: 3.2
657+
658+
648659
.. c:function:: const char* PyUnicode_GetDefaultEncoding(void)
649660
650661
Return the name of the default string encoding, ``"utf-8"``.

Doc/data/refcounts.dat

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2778,6 +2778,9 @@ PyUnicode_AppendAndDel:void:::
27782778
PyUnicode_AppendAndDel:PyObject**:p_left:0:
27792779
PyUnicode_AppendAndDel:PyObject*:right:-1:
27802780

2781+
PyUnicode_BuildEncodingMap:PyObject*::+1:
2782+
PyUnicode_BuildEncodingMap:PyObject*:string:::
2783+
27812784
PyUnicode_GetDefaultEncoding:const char*:::
27822785
PyUnicode_GetDefaultEncoding::void::
27832786

Doc/data/stable_abi.dat

Lines changed: 0 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Doc/deprecations/pending-removal-in-3.15.rst

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Pending removal in Python 3.15
2020

2121
* :mod:`http.server`:
2222

23-
* The obsolete and rarely used :class:`~http.server.CGIHTTPRequestHandler`
23+
* The obsolete and rarely used :class:`!CGIHTTPRequestHandler`
2424
has been deprecated since Python 3.13.
2525
No direct replacement exists.
2626
*Anything* is better than CGI to interface
@@ -51,7 +51,7 @@ Pending removal in Python 3.15
5151

5252
* :mod:`platform`:
5353

54-
* :func:`~platform.java_ver` has been deprecated since Python 3.13.
54+
* :func:`!platform.java_ver` has been deprecated since Python 3.13.
5555
This function is only useful for Jython support, has a confusing API,
5656
and is largely untested.
5757

@@ -85,15 +85,21 @@ Pending removal in Python 3.15
8585
has been deprecated since Python 3.13.
8686
Use the class-based syntax or the functional syntax instead.
8787

88+
* When using the functional syntax of :class:`~typing.TypedDict`\s, failing
89+
to pass a value to the *fields* parameter (``TD = TypedDict("TD")``) or
90+
passing ``None`` (``TD = TypedDict("TD", None)``) has been deprecated
91+
since Python 3.13.
92+
Use ``class TD(TypedDict): pass`` or ``TD = TypedDict("TD", {})``
93+
to create a TypedDict with zero field.
94+
8895
* The :func:`typing.no_type_check_decorator` decorator function
8996
has been deprecated since Python 3.13.
9097
After eight years in the :mod:`typing` module,
9198
it has yet to be supported by any major type checker.
9299

93100
* :mod:`wave`:
94101

95-
* The :meth:`~wave.Wave_read.getmark`, :meth:`!setmark`,
96-
and :meth:`~wave.Wave_read.getmarkers` methods of
102+
* The ``getmark()``, ``setmark()`` and ``getmarkers()`` methods of
97103
the :class:`~wave.Wave_read` and :class:`~wave.Wave_write` classes
98104
have been deprecated since Python 3.13.
99105

Doc/library/annotationlib.rst

Lines changed: 127 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -127,34 +127,34 @@ Classes
127127

128128
Values are the result of evaluating the annotation expressions.
129129

130-
.. attribute:: FORWARDREF
130+
.. attribute:: VALUE_WITH_FAKE_GLOBALS
131131
:value: 2
132132

133+
Special value used to signal that an annotate function is being
134+
evaluated in a special environment with fake globals. When passed this
135+
value, annotate functions should either return the same value as for
136+
the :attr:`Format.VALUE` format, or raise :exc:`NotImplementedError`
137+
to signal that they do not support execution in this environment.
138+
This format is only used internally and should not be passed to
139+
the functions in this module.
140+
141+
.. attribute:: FORWARDREF
142+
:value: 3
143+
133144
Values are real annotation values (as per :attr:`Format.VALUE` format)
134145
for defined values, and :class:`ForwardRef` proxies for undefined
135146
values. Real objects may contain references to :class:`ForwardRef`
136147
proxy objects.
137148

138149
.. attribute:: STRING
139-
:value: 3
150+
:value: 4
140151

141152
Values are the text string of the annotation as it appears in the
142153
source code, up to modifications including, but not restricted to,
143154
whitespace normalizations and constant values optimizations.
144155

145156
The exact values of these strings may change in future versions of Python.
146157

147-
.. attribute:: VALUE_WITH_FAKE_GLOBALS
148-
:value: 4
149-
150-
Special value used to signal that an annotate function is being
151-
evaluated in a special environment with fake globals. When passed this
152-
value, annotate functions should either return the same value as for
153-
the :attr:`Format.VALUE` format, or raise :exc:`NotImplementedError`
154-
to signal that they do not support execution in this environment.
155-
This format is only used internally and should not be passed to
156-
the functions in this module.
157-
158158
.. versionadded:: 3.14
159159

160160
.. class:: ForwardRef
@@ -485,3 +485,117 @@ annotations from the class and puts them in a separate attribute:
485485
typ.classvars = classvars # Store the ClassVars in a separate attribute
486486
return typ
487487
488+
489+
Limitations of the ``STRING`` format
490+
------------------------------------
491+
492+
The :attr:`~Format.STRING` format is meant to approximate the source code
493+
of the annotation, but the implementation strategy used means that it is not
494+
always possible to recover the exact source code.
495+
496+
First, the stringifier of course cannot recover any information that is not present in
497+
the compiled code, including comments, whitespace, parenthesization, and operations that
498+
get simplified by the compiler.
499+
500+
Second, the stringifier can intercept almost all operations that involve names looked
501+
up in some scope, but it cannot intercept operations that operate fully on constants.
502+
As a corollary, this also means it is not safe to request the ``STRING`` format on
503+
untrusted code: Python is powerful enough that it is possible to achieve arbitrary
504+
code execution even with no access to any globals or builtins. For example:
505+
506+
.. code-block:: pycon
507+
508+
>>> def f(x: (1).__class__.__base__.__subclasses__()[-1].__init__.__builtins__["print"]("Hello world")): pass
509+
...
510+
>>> annotationlib.get_annotations(f, format=annotationlib.Format.SOURCE)
511+
Hello world
512+
{'x': 'None'}
513+
514+
.. note::
515+
This particular example works as of the time of writing, but it relies on
516+
implementation details and is not guaranteed to work in the future.
517+
518+
Among the different kinds of expressions that exist in Python,
519+
as represented by the :mod:`ast` module, some expressions are supported,
520+
meaning that the ``STRING`` format can generally recover the original source code;
521+
others are unsupported, meaning that they may result in incorrect output or an error.
522+
523+
The following are supported (sometimes with caveats):
524+
525+
* :class:`ast.BinOp`
526+
* :class:`ast.UnaryOp`
527+
528+
* :class:`ast.Invert` (``~``), :class:`ast.UAdd` (``+``), and :class:`ast.USub` (``-``) are supported
529+
* :class:`ast.Not` (``not``) is not supported
530+
531+
* :class:`ast.Dict` (except when using ``**`` unpacking)
532+
* :class:`ast.Set`
533+
* :class:`ast.Compare`
534+
535+
* :class:`ast.Eq` and :class:`ast.NotEq` are supported
536+
* :class:`ast.Lt`, :class:`ast.LtE`, :class:`ast.Gt`, and :class:`ast.GtE` are supported, but the operand may be flipped
537+
* :class:`ast.Is`, :class:`ast.IsNot`, :class:`ast.In`, and :class:`ast.NotIn` are not supported
538+
539+
* :class:`ast.Call` (except when using ``**`` unpacking)
540+
* :class:`ast.Constant` (though not the exact representation of the constant; for example, escape
541+
sequences in strings are lost; hexadecimal numbers are converted to decimal)
542+
* :class:`ast.Attribute` (assuming the value is not a constant)
543+
* :class:`ast.Subscript` (assuming the value is not a constant)
544+
* :class:`ast.Starred` (``*`` unpacking)
545+
* :class:`ast.Name`
546+
* :class:`ast.List`
547+
* :class:`ast.Tuple`
548+
* :class:`ast.Slice`
549+
550+
The following are unsupported, but throw an informative error when encountered by the
551+
stringifier:
552+
553+
* :class:`ast.FormattedValue` (f-strings; error is not detected if conversion specifiers like ``!r``
554+
are used)
555+
* :class:`ast.JoinedStr` (f-strings)
556+
557+
The following are unsupported and result in incorrect output:
558+
559+
* :class:`ast.BoolOp` (``and`` and ``or``)
560+
* :class:`ast.IfExp`
561+
* :class:`ast.Lambda`
562+
* :class:`ast.ListComp`
563+
* :class:`ast.SetComp`
564+
* :class:`ast.DictComp`
565+
* :class:`ast.GeneratorExp`
566+
567+
The following are disallowed in annotation scopes and therefore not relevant:
568+
569+
* :class:`ast.NamedExpr` (``:=``)
570+
* :class:`ast.Await`
571+
* :class:`ast.Yield`
572+
* :class:`ast.YieldFrom`
573+
574+
575+
Limitations of the ``FORWARDREF`` format
576+
----------------------------------------
577+
578+
The :attr:`~Format.FORWARDREF` format aims to produce real values as much
579+
as possible, with anything that cannot be resolved replaced with
580+
:class:`ForwardRef` objects. It is affected by broadly the same Limitations
581+
as the :attr:`~Format.STRING` format: annotations that perform operations on
582+
literals or that use unsupported expression types may raise exceptions when
583+
evaluated using the :attr:`~Format.FORWARDREF` format.
584+
585+
Below are a few examples of the behavior with unsupported expressions:
586+
587+
.. code-block:: pycon
588+
589+
>>> from annotationlib import get_annotations, Format
590+
>>> def zerodiv(x: 1 / 0): ...
591+
>>> get_annotations(zerodiv, format=Format.STRING)
592+
Traceback (most recent call last):
593+
...
594+
ZeroDivisionError: division by zero
595+
>>> get_annotations(zerodiv, format=Format.FORWARDREF)
596+
Traceback (most recent call last):
597+
...
598+
ZeroDivisionError: division by zero
599+
>>> def ifexp(x: 1 if y else 0): ...
600+
>>> get_annotations(ifexp, format=Format.STRING)
601+
{'x': '1'}

0 commit comments

Comments
 (0)