Skip to content

Commit 27ed2ad

Browse files
committed
Merge remote-tracking branch 'upstream/main' into gh-138122
2 parents bf42bd3 + 0dbbf61 commit 27ed2ad

41 files changed

Lines changed: 1053 additions & 412 deletions

Some content is hidden

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

.github/CODEOWNERS

Lines changed: 532 additions & 291 deletions
Large diffs are not rendered by default.

Doc/c-api/init.rst

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2299,12 +2299,20 @@ per-object locks for :term:`free-threaded <free threading>` CPython. They are
22992299
intended to replace reliance on the :term:`global interpreter lock`, and are
23002300
no-ops in versions of Python with the global interpreter lock.
23012301
2302+
Critical sections are intended to be used for custom types implemented
2303+
in C-API extensions. They should generally not be used with built-in types like
2304+
:class:`list` and :class:`dict` because their public C-APIs
2305+
already use critical sections internally, with the notable
2306+
exception of :c:func:`PyDict_Next`, which requires critical section
2307+
to be acquired externally.
2308+
23022309
Critical sections avoid deadlocks by implicitly suspending active critical
2303-
sections and releasing the locks during calls to :c:func:`PyEval_SaveThread`.
2304-
When :c:func:`PyEval_RestoreThread` is called, the most recent critical section
2305-
is resumed, and its locks reacquired. This means the critical section API
2306-
provides weaker guarantees than traditional locks -- they are useful because
2307-
their behavior is similar to the :term:`GIL`.
2310+
sections, hence, they do not provide exclusive access such as provided by
2311+
traditional locks like :c:type:`PyMutex`. When a critical section is started,
2312+
the per-object lock for the object is acquired. If the code executed inside the
2313+
critical section calls C-API functions then it can suspend the critical section thereby
2314+
releasing the per-object lock, so other threads can acquire the per-object lock
2315+
for the same object.
23082316
23092317
Variants that accept :c:type:`PyMutex` pointers rather than Python objects are also
23102318
available. Use these variants to start a critical section in a situation where

Doc/glossary.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ Glossary
107107
statements.
108108

109109
asynchronous generator iterator
110-
An object created by a :term:`asynchronous generator` function.
110+
An object created by an :term:`asynchronous generator` function.
111111

112112
This is an :term:`asynchronous iterator` which when called using the
113113
:meth:`~object.__anext__` method returns an awaitable object which will execute

Doc/library/stdtypes.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1911,6 +1911,14 @@ expression support in the :mod:`re` module).
19111911
Return the lowest index in the string where substring *sub* is found within
19121912
the slice ``s[start:end]``. Optional arguments *start* and *end* are
19131913
interpreted as in slice notation. Return ``-1`` if *sub* is not found.
1914+
For example::
1915+
1916+
>>> 'spam, spam, spam'.find('sp')
1917+
0
1918+
>>> 'spam, spam, spam'.find('sp', 5)
1919+
6
1920+
1921+
See also :meth:`rfind` and :meth:`index`.
19141922

19151923
.. note::
19161924

Include/Python.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@
6060
# endif
6161
#endif // Py_GIL_DISABLED
6262

63+
#ifdef _MSC_VER
64+
// Ignore MSC warning C4201: "nonstandard extension used: nameless
65+
// struct/union". (Only generated for C standard versions less than C11, which
66+
// we don't *officially* support.)
67+
__pragma(warning(push))
68+
__pragma(warning(disable: 4201))
69+
#endif
70+
71+
6372
// Include Python header files
6473
#include "pyport.h"
6574
#include "pymacro.h"
@@ -139,4 +148,8 @@
139148
#include "cpython/pyfpe.h"
140149
#include "cpython/tracemalloc.h"
141150

151+
#ifdef _MSC_VER
152+
__pragma(warning(pop)) // warning(disable: 4201)
153+
#endif
154+
142155
#endif /* !Py_PYTHON_H */

Include/object.h

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -123,18 +123,7 @@ whose size is determined when the object is allocated.
123123
/* PyObject is opaque */
124124
#elif !defined(Py_GIL_DISABLED)
125125
struct _object {
126-
#if (defined(__GNUC__) || defined(__clang__)) \
127-
&& !(defined __STDC_VERSION__ && __STDC_VERSION__ >= 201112L)
128-
// On C99 and older, anonymous union is a GCC and clang extension
129-
__extension__
130-
#endif
131-
#ifdef _MSC_VER
132-
// Ignore MSC warning C4201: "nonstandard extension used:
133-
// nameless struct/union"
134-
__pragma(warning(push))
135-
__pragma(warning(disable: 4201))
136-
#endif
137-
union {
126+
_Py_ANONYMOUS union {
138127
#if SIZEOF_VOID_P > 4
139128
PY_INT64_T ob_refcnt_full; /* This field is needed for efficient initialization with Clang on ARM */
140129
struct {
@@ -153,9 +142,6 @@ struct _object {
153142
#endif
154143
_Py_ALIGNED_DEF(_PyObject_MIN_ALIGNMENT, char) _aligner;
155144
};
156-
#ifdef _MSC_VER
157-
__pragma(warning(pop))
158-
#endif
159145

160146
PyTypeObject *ob_type;
161147
};

Include/pymacro.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,28 @@
8686
# endif
8787
#endif
8888

89+
90+
// _Py_ANONYMOUS: modifier for declaring an anonymous union.
91+
// Usage: _Py_ANONYMOUS union { ... };
92+
// Standards/compiler support:
93+
// - C++ allows anonymous unions, but not structs
94+
// - C11 and above allows anonymous unions and structs
95+
// - MSVC has warning(disable: 4201) "nonstandard extension used : nameless
96+
// struct/union". This is specific enough that we disable it for all of
97+
// Python.h.
98+
// - GCC & clang needs __extension__ before C11
99+
// To allow unsupported platforms which need other spellings, we use a
100+
// predefined value of _Py_ANONYMOUS if it exists.
101+
#ifndef _Py_ANONYMOUS
102+
# if (defined(__GNUC__) || defined(__clang__)) \
103+
&& !(defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L)
104+
# define _Py_ANONYMOUS __extension__
105+
# else
106+
# define _Py_ANONYMOUS
107+
# endif
108+
#endif
109+
110+
89111
/* Minimum value between x and y */
90112
#define Py_MIN(x, y) (((x) > (y)) ? (y) : (x))
91113

Lib/hashlib.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,21 @@
3636
efficiently compute the digests of data that share a common
3737
initial substring.
3838
39-
Assuming that Python has been built with MD5 support, the following computes
40-
the MD5 digest of the byte string b'Nobody inspects the spammish repetition':
39+
Assuming that Python has been built with SHA-2 support, the SHA-256 digest
40+
of the byte string b'Nobody inspects the spammish repetition' is computed
41+
as follows:
4142
4243
>>> import hashlib
43-
>>> m = hashlib.md5()
44+
>>> m = hashlib.sha256()
4445
>>> m.update(b"Nobody inspects")
4546
>>> m.update(b" the spammish repetition")
46-
>>> m.digest()
47-
b'\xbbd\x9c\x83\xdd\x1e\xa5\xc9\xd9\xde\xc9\xa1\x8d\xf0\xff\xe9'
47+
>>> m.digest() # doctest: +ELLIPSIS
48+
b'\x03\x1e\xdd}Ae\x15\x93\xc5\xfe\\\x00o\xa5u+7...'
4849
4950
More condensed:
5051
51-
>>> hashlib.md5(b"Nobody inspects the spammish repetition").hexdigest()
52-
'bb649c83dd1ea5c9d9dec9a18df0ffe9'
53-
52+
>>> hashlib.sha256(b"Nobody inspects the spammish repetition").hexdigest()
53+
'031edd7d41651593c5fe5c006fa5752b37fddff7bc4e843aa6af0c950f4b9406'
5454
"""
5555

5656
# This tuple and __get_builtin_constructor() must be modified if a new

0 commit comments

Comments
 (0)