8000 Merge branch 'main' into test-lazy-import · python/cpython@aa19ba5 · GitHub
[go: up one dir, main page]

Skip to content

Commit aa19ba5

Browse files
authored
Merge branch 'main' into test-lazy-import
2 parents 4628f14 + 893034c commit aa19ba5

File tree

87 files changed

+2291
-457
lines changed

Some content is hidden

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

87 files changed

+2291
-457
lines changed

Doc/c-api/object.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,3 +737,21 @@ Object Protocol
737737
caller must hold a :term:`strong reference` to *obj* when calling this.
738738
739739
.. versionadded:: 3.14
740+
741+
.. c:function:: int PyUnstable_Object_IsUniquelyReferenced(PyObject *op)
742+
743+
Determine if *op* only has one reference.
744+
745+
On GIL-enabled builds, this function is equivalent to
746+
:c:expr:`Py_REFCNT(op) == 1`.
747+
748+
On a :term:`free threaded <free threading>` build, this checks if *op*'s
749+
:term:`reference count` is equal to one and additionally checks if *op*
750+
is only used by this thread. :c:expr:`Py_REFCNT(op) == 1` is **not**
751+
thread-safe on free threaded builds; prefer this function.
752+
753+
The caller must hold an :term:`attached thread state`, despite the fact
754+
that this function doesn't call into the Python interpreter. This function
755+
< 433 span class="pl-s1"> cannot fail.
756+
757+
.. versionadded:: 3.14

Doc/c-api/refcounting.rst

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,14 @@ of Python objects.
2323
2424
Use the :c:func:`Py_SET_REFCNT()` function to set an object reference count.
2525
26-
See also the function :c:func:`PyUnstable_Object_IsUniqueReferencedTemporary()`.
26+
.. note::
27+
28+
On :term:`free threaded <free threading>` builds of Python, returning 1
29+
isn't sufficient to determine if it's safe to treat *o* as having no
30+
access by other threads. Use :c:func:`PyUnstable_Object_IsUniquelyReferenced`
31+
for that instead.
32+
33+
See also the function :c:func:`PyUnstable_Object_IsUniqueReferencedTemporary()`.
2734
2835
.. versionchanged:: 3.10
2936
:c:func:`Py_REFCNT()` is changed to the inline static function.

Doc/library/ast.rst

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
:mod:`!ast` --- Abstract Syntax Trees
1+
:mod:`!ast` --- Abstract syntax trees
22
=====================================
33

44
.. module:: ast
@@ -29,7 +29,7 @@ compiled into a Python code object using the built-in :func:`compile` function.
2929

3030
.. _abstract-grammar:
3131

32-
Abstract Grammar
32+
Abstract grammar
3333
----------------
3434

3535
The abstract grammar is currently defined as follows:
@@ -2156,10 +2156,10 @@ Async and await
21562156
of :class:`ast.operator`, :class:`ast.unaryop`, :class:`ast.cmpop`,
21572157
:class:`ast.boolop` and :class:`ast.expr_context`) on the returned tree
21582158
will be singletons. Changes to one will be reflected in all other
2159-
occurrences of the same value (e.g. :class:`ast.Add`).
2159+
occurrences of the same value (for example, :class:`ast.Add`).
21602160

21612161

2162-
:mod:`ast` Helpers
2162+
:mod:`ast` helpers
21632163
------------------
21642164

21652165
Apart from the node classes, the :mod:`ast` module defines these utility functions
@@ -2484,7 +2484,7 @@ and classes for traversing abstract syntax trees:
24842484

24852485
.. _ast-compiler-flags:
24862486

2487-
Compiler Flags
2487+
Compiler flags
24882488
--------------
24892489

24902490
The following flags may be passed to :func:`compile` in order to change
@@ -2533,7 +2533,7 @@ effects on the compilation of a program:
25332533

25342534
.. _ast-cli:
25352535

2536-
Command-Line Usage
2536+
Command-line usage
25372537
------------------
25382538

25392539
.. versionadded:: 3.9
@@ -2572,6 +2572,28 @@ The following options are accepted:
25722572

25732573
Indentation of nodes in AST (number of spaces).
25742574

2575+
.. option:: --feature-version <version>
2576+
2577+
Python version in the format 3.x (for example, 3.10). Defaults to the
2578+
current version of the interpreter.
2579+
2580+
.. versionadded:: next
2581+
2582+
.. option:: -O <level>
2583+
--optimize <level>
2584+
2585+
Optimization level for parser. Defaults to no optimization.
2586+
2587+
.. versionadded:: next
2588+
2589+
.. option:: --show-empty
2590+
2591+
Show empty lists and fields that are ``None``. Defaults to not showing empty
2592+
objects.
2593+
2594+
.. versionadded:: next
2595+
2596+
25752597
If :file:`infile` is specified its contents are parsed to AST and dumped
25762598
to stdout. Otherwise, the content is read from stdin.
25772599

Doc/library/gc.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@ The :mod:`gc` module provides the following functions:
128128
starts. For each collection, all the objects in the young generation and some
129129
fraction of the old generation is collected.
130130

131+
In the free-threaded build, the increase in process memory usage is also
132+
checked before running the collector. If the memory usage has not increased
133+
by 10% since the last collection and the net number of object allocations
134+
has not exceeded 40 times *threshold0*, the collection is not run.
135+
131136
The fraction of the old generation that is collected is **inversely** proportional
132137
to *threshold1*. The larger *threshold1* is, the slower objects in the old generation
133138
are collected.

Doc/library/pathlib.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -872,10 +872,10 @@ conforming to :rfc:`8089`.
872872
.. versionadded:: 3.13
873873

874874
.. versionchanged:: next
875 10000 -
If a URL authority (e.g. a hostname) is present and resolves to a local
876-
address, it is discarded. If an authority is present and *doesn't*
877-
resolve to a local address, then on Windows a UNC path is returned (as
878-
before), and on other platforms a :exc:`ValueError` is raised.
875+
The URL authority is discarded if it matches the local hostname.
876+
Otherwise, if the authority isn't empty or ``localhost``, then on
877+
Windows a UNC path is returned (as before), and on other platforms a
878+
:exc:`ValueError` is raised.
879879

880880

881881
.. method:: Path.as_uri()

Doc/library/urllib.request.rst

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,10 @@ The :mod:`urllib.request` module defines the following functions:
172172
the URL ``///etc/hosts``.
173173

174174
.. versionchanged:: next
175-
The *add_scheme* argument was added.
175+
The *add_scheme* parameter was added.
176176

177177

178-
.. function:: url2pathname(url, *, require_scheme=False)
178+
.. function:: url2pathname(url, *, require_scheme=False, resolve_host=False)
179179

180180
Convert the given ``file:`` URL to a local path. This function uses
181181
:func:`~urllib.parse.unquote` to decode the URL.
@@ -185,6 +185,13 @@ The :mod:`urllib.request` module defines the following functions:
185185
value should include the prefix; a :exc:`~urllib.error.URLError` is raised
186186
if it doesn't.
187187

188+
The URL authority is discarded if it is empty, ``localhost``, or the local
189+
hostname. Otherwise, if *resolve_host* is set to true, the authority is
190+
resolved using :func:`socket.gethostbyname` and discarded if it matches a
191+
local IP address (as per :rfc:`RFC 8089 §3 <8089#section-3>`). If the
192+
authority is still unhandled, then on Windows a UNC path is returned, and
193+
on other platforms a :exc:`~urllib.error.URLError` is raised.
194+
188195
This example shows the function being used on Windows::
189196

190197
>>> from urllib.request import url2pathname
@@ -198,14 +205,13 @@ The :mod:`urllib.request` module defines the following functions:
198205
:exc:`OSError` exception to be raised on Windows.
199206

200207
.. versionchanged:: next
201-
This function calls :func:`socket.gethostbyname` if the URL authority
202-
isn't empty, ``localhost``, or the machine hostname. If the authority
203-
resolves to a local IP address then it is discarded; otherwise, on
208+
The URL authority is discarded if it matches the local hostname.
209+
Otherwise, if the authority isn't empty or ``localhost``, then on
204210
Windows a UNC path is returned (as before), and on other platforms a
205211
:exc:`~urllib.error.URLError` is raised.
206212

207213
.. versionchanged:: next
208-
The *require_scheme* argument was added.
214+
The *require_scheme* and *resolve_host* parameters were added.
209215

210216

211217
.. function:: getproxies()

Doc/whatsnew/3.14.rst

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,11 @@ ast
875875
that the root node type is appropriate.
876876
(Contributed by Irit Katriel in :gh:`130139`.)
877877

878+
* Add new ``--feature-version``, ``--optimize``, ``--show-empty`` options to
879+
command-line interface.
880+
(Contributed by Semyon Moroz in :gh:`133367`.)
881+
882+
878883
bdb
879884
---
880885

@@ -1438,6 +1443,11 @@ pdb
14381443
fill in a 4-space indentation now, instead of inserting a ``\t`` character.
14391444
(Contributed by Tian Gao in :gh:`130471`.)
14401445

1446+
* Auto-indent is introduced in :mod:`pdb` multi-line input. It will either
1447+
keep the indentation of the last line or insert a 4-space indentation when
1448+
it detects a new code block.
1449+
(Contributed by Tian Gao in :gh:`133350`.)
1450+
14411451
* ``$_asynctask`` is added to access the current asyncio task if applicable.
14421452
(Contributed by Tian Gao in :gh:`124367`.)
14431453

@@ -1703,9 +1713,11 @@ urllib
17031713

17041714
- Accept a complete URL when the new *require_scheme* argument is set to
17051715
true.
1706-
- Discard URL authorities that resolve to a local IP address.
1707-
- Raise :exc:`~urllib.error.URLError` if a URL authority doesn't resolve
1708-
to a local IP address, except on Windows where we return a UNC path.
1716+
- Discard URL authority if it matches the local hostname.
1717+
- Discard URL authority if it resolves to a local IP address when the new
1718+
*resolve_host* argument is set to true.
1719+
- Raise :exc:`~urllib.error.URLError` if a URL authority isn't local,
1720+
except on Windows where we return a UNC path as before.
17091721

17101722
In :func:`urllib.request.pathname2url`:
17111723

@@ -2457,6 +2469,10 @@ New features
24572469
be used in some cases as a replacement for checking if :c:func:`Py_REFCNT`
24582470
is ``1`` for Python objects passed as arguments to C API functions.
24592471

2472+
* Add :c:func:`PyUnstable_Object_IsUniquelyReferenced` as a replacement for
2473+
``Py_REFCNT(op) == 1`` on :term:`free threaded <free threading>` builds.
2474+
(Contributed by Peter Bierma in :gh:`133140`.)
2475+
24602476

24612477
Limited C API changes
24622478
---------------------

Include/cpython/funcobject.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ static inline PyObject* PyFunction_GET_GLOBALS(PyObject *func) {
9797
}
9898
#define PyFunction_GET_GLOBALS(func) PyFunction_GET_GLOBALS(_PyObject_CAST(func))
9999

100+
static inline PyObject* PyFunction_GET_BUILTINS(PyObject *func) {
101+
return _PyFunction_CAST(func)->func_builtins;
102+
}
103+
#define PyFunction_GET_BUILTINS(func) PyFunction_GET_BUILTINS(_PyObject_CAST(func))
104+
100105
static inline PyObject* PyFunction_GET_MODULE(PyObject *func) {
101106
return _PyFunction_CAST(func)->func_module;
102107
}

Include/cpython/object.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,11 @@ typedef struct {
143143
* backwards-compatibility */
144144
typedef Py_ssize_t printfunc;
145145

146+
/* Specialize a binary op by setting the descriptor pointer */
147+
struct _PyBinopSpecializationDescr;
148+
typedef int (*binop_specialize_func)(PyObject *v, PyObject *w, int oparg,
149+
struct _PyBinopSpecializationDescr **descr);
150+
146151
// If this structure is modified, Doc/includes/typestruct.h should be updated
147152
// as well.
148153
struct _typeobject {
@@ -233,6 +238,13 @@ struct _typeobject {
233238
/* bitset of which type-watchers care about this type */
234239
unsigned char tp_watched;
235240

241+
/* callback that may specialize BINARY_OP
242+
* this is an experimental API based on the ideas in the paper
243+
* Cross Module Quickening - The Curious Case of C Extensions
244+
* by Felix Berlakovich and Stefan Brunthaler.
245+
*/
246+
binop_specialize_func tp_binop_specialize;
247+
236248
/* Number of tp_version_tag values used.
237249
* Set to _Py_ATTR_CACHE_UNUSED if the attribute cache is
238250
* disabled for this type (e.g. due to custom MRO entries).
@@ -489,3 +501,5 @@ PyAPI_FUNC(int) PyUnstable_IsImmortal(PyObject *);
489501
// before calling this function in order to avoid spurious failures.
490502
PyAPI_FUNC(int) PyUnstable_TryIncRef(PyObject *);
491503
PyAPI_FUNC(void) PyUnstable_EnableTryIncRef(PyObject *);
504+
505+
PyAPI_FUNC(int) PyUnstable_Object_IsUniquelyReferenced(PyObject *);

Include/internal/pycore_code.h

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -482,13 +482,18 @@ adaptive_counter_backoff(_Py_BackoffCounter counter) {
482482
/* Specialization Extensions */
483483

484484
/* callbacks for an external specialization */
485+
486+
struct _PyBinopSpecializationDescr;
487+
485488
typedef int (*binaryopguardfunc)(PyObject *lhs, PyObject *rhs);
486-
typedef PyObject *(*binaryopactionfunc)(PyObject *lhs, PyObject *rhs);
489+
typedef PyObject* (*binaryopactionfunc)(PyObject *lhs, PyObject *rhs);
490+
typedef void (*binaryopfreefunc)(struct _PyBinopSpecializationDescr *descr);
487491

488-
typedef struct {
492+
typedef struct _PyBinopSpecializationDescr {
489493
int oparg;
490494
binaryopguardfunc guard;
491495
binaryopactionfunc action;
496+
binaryopfreefunc free;
492497
} _PyBinaryOpSpecializationDescr;
493498

494499
/* Comparison bit masks. */
@@ -565,6 +570,57 @@ extern int _Py_ClearUnusedTLBC(PyInterpreterState *interp);
565570
#endif
566571

567572

573+
typedef struct {
574+
int total;
575+
struct co_locals_counts {
576+
int total;
577+
struct {
578+
int total;
579+
int numposonly;
580+
int numposorkw;
581+
int numkwonly;
582+
int varargs;
583+
int varkwargs;
584+
} args;
585+
int numpure;
586+
struct {
587+
int total;
588+
// numargs does not contribute to locals.total.
589+
int numargs;
590+
int numothers;
591+
} cells;
592+
struct {
593+
int total;
594+
int numpure;
595+
int numcells;
596+
} hidden;
597+
} locals;
598+
int numfree; // nonlocal
599+
struct co_unbound_counts {
600+
int total;
601+
struct {
602+
int total;
603+
int numglobal;
604+
int numbuiltin;
605+
int numunknown;
606+
} globals;
607+
int numattrs;
608+
int numunknown;
609+
} unbound;
610+
} _PyCode_var_counts_t;
611+
612+
PyAPI_FUNC(void) _PyCode_GetVarCounts(
613+
PyCodeObject *,
614+
_PyCode_var_counts_t *);
615+
PyAPI_FUNC(int) _PyCode_SetUnboundVarCounts(
616+
PyThreadState *,
617+
PyCodeObject *,
618+
_PyCode_var_counts_t *,
619+
PyObject *globalnames,
620+
PyObject *attrnames,
621+
PyObject *globalsns,
622+
PyObject *builtinsns);
623+
568624
PyAPI_FUNC(int) _PyCode_ReturnsOnlyNone(PyCodeObject *);
569625

570626

Include/internal/pycore_interp_structs.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,16 @@ struct _gc_runtime_state {
245245

246246
/* True if gc.freeze() has been used. */
247247
int freeze_active;
248+
249+
/* Memory usage of the process (RSS + swap) after last GC. */
250+
Py_ssize_t last_mem;
251+
252+
/* This accumulates the new object count whenever collection is deferred
253+
due to the RSS increase condition not being meet. Reset on collection. */
254+
Py_ssize_t deferred_count;
255+
256+
/* Mutex held for gc_should_collect_mem_usage(). */
257+
PyMutex mutex;
248258
#endif
249259
};
250260

Include/internal/pycore_opcode_metadata.h

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
0