Skip to content

Commit b64de3d

Browse files
committed
Merge branch 'main' into use-attributes-to-guide-inline-values-size
2 parents a7b858b + f201628 commit b64de3d

Some content is hidden

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

64 files changed

+1533
-465
lines changed

Doc/c-api/init.rst

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1904,6 +1904,58 @@ Python-level trace functions in previous versions.
19041904
19051905
.. versionadded:: 3.12
19061906
1907+
Reference tracing
1908+
=================
1909+
1910+
.. versionadded:: 3.13
1911+
1912+
.. c:type:: int (*PyRefTracer)(PyObject *, int event, void* data)
1913+
1914+
The type of the trace function registered using :c:func:`PyRefTracer_SetTracer`.
1915+
The first parameter is a Python object that has been just created (when **event**
1916+
is set to :c:data:`PyRefTracer_CREATE`) or about to be destroyed (when **event**
1917+
is set to :c:data:`PyRefTracer_DESTROY`). The **data** argument is the opaque pointer
1918+
that was provided when :c:func:`PyRefTracer_SetTracer` was called.
1919+
1920+
.. versionadded:: 3.13
1921+
1922+
.. c:var:: int PyRefTracer_CREATE
1923+
1924+
The value for the *event* parameter to :c:type:`PyRefTracer` functions when a Python
1925+
object has been created.
1926+
1927+
.. c:var:: int PyRefTracer_DESTROY
1928+
1929+
The value for the *event* parameter to :c:type:`PyRefTracer` functions when a Python
1930+
object has been destroyed.
1931+
1932+
.. c:function:: int PyRefTracer_SetTracer(PyRefTracer tracer, void *data)
1933+
1934+
Register a reference tracer function. The function will be called when a new
1935+
Python has been created or when an object is going to be destroyed. If
1936+
**data** is provided it must be an opaque pointer that will be provided when
1937+
the tracer function is called. Return ``0`` on success. Set an exception and
1938+
return ``-1`` on error.
1939+
1940+
Not that tracer functions **must not** create Python objects inside or
1941+
otherwise the call will be re-entrant. The tracer also **must not** clear
1942+
any existing exception or set an exception. The GIL will be held every time
1943+
the tracer function is called.
1944+
1945+
The GIL must be held when calling this function.
1946+
1947+
.. versionadded:: 3.13
1948+
1949+
.. c:function:: PyRefTracer PyRefTracer_GetTracer(void** data)
1950+
1951+
Get the registered reference tracer function and the value of the opaque data
1952+
pointer that was registered when :c:func:`PyRefTracer_SetTracer` was called.
1953+
If no tracer was registered this function will return NULL and will set the
1954+
**data** pointer to NULL.
1955+
1956+
The GIL must be held when calling this function.
1957+
1958+
.. versionadded:: 3.13
19071959
19081960
.. _advanced-debugging:
19091961

Doc/library/functions.rst

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -524,11 +524,11 @@ are always available. They are listed here in alphabetical order.
524524

525525
.. _func-eval:
526526

527-
.. function:: eval(expression, globals=None, locals=None)
527+
.. function:: eval(source, /, globals=None, locals=None)
528528

529-
:param expression:
529+
:param source:
530530
A Python expression.
531-
:type expression: :class:`str` | :ref:`code object <code-objects>`
531+
:type source: :class:`str` | :ref:`code object <code-objects>`
532532

533533
:param globals:
534534
The global namespace (default: ``None``).
@@ -583,11 +583,15 @@ are always available. They are listed here in alphabetical order.
583583
Raises an :ref:`auditing event <auditing>` ``exec`` with the code object
584584
as the argument. Code compilation events may also be raised.
585585

586+
.. versionchanged:: 3.13
587+
588+
The *globals* and *locals* arguments can now be passed as keywords.
589+
586590
.. index:: pair: built-in function; exec
587591

588-
.. function:: exec(object, globals=None, locals=None, /, *, closure=None)
592+
.. function:: exec(source, /, globals=None, locals=None, *, closure=None)
589593

590-
This function supports dynamic execution of Python code. *object* must be
594+
This function supports dynamic execution of Python code. *source* must be
591595
either a string or a code object. If it is a string, the string is parsed as
592596
a suite of Python statements which is then executed (unless a syntax error
593597
occurs). [#]_ If it is a code object, it is simply executed. In all cases,
@@ -640,6 +644,10 @@ are always available. They are listed here in alphabetical order.
640644
.. versionchanged:: 3.11
641645
Added the *closure* parameter.
642646

647+
.. versionchanged:: 3.13
648+
649+
The *globals* and *locals* arguments can now be passed as keywords.
650+
643651

644652
.. function:: filter(function, iterable)
645653

@@ -1733,8 +1741,9 @@ are always available. They are listed here in alphabetical order.
17331741
:ref:`function` for details.
17341742

17351743
A static method can be called either on the class (such as ``C.f()``) or on
1736-
an instance (such as ``C().f()``). Moreover, they can be called as regular
1737-
functions (such as ``f()``).
1744+
an instance (such as ``C().f()``).
1745+
Moreover, the static method :term:`descriptor` is also callable, so it can
1746+
be used in the class definition (such as ``f()``).
17381747

17391748
Static methods in Python are similar to those found in Java or C++. Also, see
17401749
:func:`classmethod` for a variant that is useful for creating alternate class

Doc/library/os.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2430,6 +2430,10 @@ features:
24302430
platform-dependent. On some platforms, they are ignored and you should call
24312431
:func:`chmod` explicitly to set them.
24322432

2433+
On Windows, a *mode* of ``0o700`` is specifically handled to apply access
2434+
control to the new directory such that only the current user and
2435+
administrators have access. Other values of *mode* are ignored.
2436+
24332437
This function can also support :ref:`paths relative to directory descriptors
24342438
<dir_fd>`.
24352439

@@ -2444,6 +2448,9 @@ features:
24442448
.. versionchanged:: 3.6
24452449
Accepts a :term:`path-like object`.
24462450

2451+
.. versionchanged:: 3.13
2452+
Windows now handles a *mode* of ``0o700``.
2453+
24472454

24482455
.. function:: makedirs(name, mode=0o777, exist_ok=False)
24492456

Doc/whatsnew/3.13.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,10 @@ Other Language Changes
288288
class scopes are not inlined into their parent scope. (Contributed by
289289
Jelle Zijlstra in :gh:`109118` and :gh:`118160`.)
290290

291+
* ``from __future__ import ...`` statements are now just normal
292+
relative imports if dots are present before the module name.
293+
(Contributed by Jeremiah Gabriel Pascual in :gh:`118216`.)
294+
291295

292296
New Modules
293297
===========
@@ -701,6 +705,9 @@ pdb
701705
command line option or :envvar:`PYTHONSAFEPATH` environment variable).
702706
(Contributed by Tian Gao and Christian Walther in :gh:`111762`.)
703707

708+
* :mod:`zipapp` is supported as a debugging target.
709+
(Contributed by Tian Gao in :gh:`118501`.)
710+
704711
queue
705712
-----
706713

@@ -1957,6 +1964,11 @@ New Features
19571964
* Add :c:func:`PyType_GetModuleByDef` to the limited C API
19581965
(Contributed by Victor Stinner in :gh:`116936`.)
19591966

1967+
* Add two new functions to the C-API, :c:func:`PyRefTracer_SetTracer` and
1968+
:c:func:`PyRefTracer_GetTracer`, that allows to track object creation and
1969+
destruction the same way the :mod:`tracemalloc` module does. (Contributed
1970+
by Pablo Galindo in :gh:`93502`.)
1971+
19601972

19611973
Porting to Python 3.13
19621974
----------------------

Include/cpython/object.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,3 +510,13 @@ PyAPI_FUNC(int) PyType_Unwatch(int watcher_id, PyObject *type);
510510
* assigned, or 0 if a new tag could not be assigned.
511511
*/
512512
PyAPI_FUNC(int) PyUnstable_Type_AssignVersionTag(PyTypeObject *type);
513+
514+
515+
typedef enum {
516+
PyRefTracer_CREATE = 0,
517+
PyRefTracer_DESTROY = 1,
518+
} PyRefTracerEvent;
519+
520+
typedef int (*PyRefTracer)(PyObject *, PyRefTracerEvent event, void *);
521+
PyAPI_FUNC(int) PyRefTracer_SetTracer(PyRefTracer tracer, void *data);
522+
PyAPI_FUNC(PyRefTracer) PyRefTracer_GetTracer(void**);

Include/cpython/optimizer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ void _Py_BloomFilter_Init(_PyBloomFilter *);
142142
void _Py_BloomFilter_Add(_PyBloomFilter *bloom, void *obj);
143143
PyAPI_FUNC(void) _Py_Executor_DependsOn(_PyExecutorObject *executor, void *obj);
144144
PyAPI_FUNC(void) _Py_Executors_InvalidateDependency(PyInterpreterState *interp, void *obj, int is_invalidation);
145-
extern void _Py_Executors_InvalidateAll(PyInterpreterState *interp, int is_invalidation);
145+
PyAPI_FUNC(void) _Py_Executors_InvalidateAll(PyInterpreterState *interp, int is_invalidation);
146146

147147
/* For testing */
148148
PyAPI_FUNC(PyObject *)PyUnstable_Optimizer_NewCounter(void);

Include/internal/pycore_codecs.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@ extern "C" {
88
# error "this header requires Py_BUILD_CORE define"
99
#endif
1010

11+
#include "pycore_lock.h" // PyMutex
12+
13+
/* Initialize codecs-related state for the given interpreter, including
14+
registering the first codec search function. Must be called before any other
15+
PyCodec-related functions, and while only one thread is active. */
16+
extern PyStatus _PyCodec_InitRegistry(PyInterpreterState *interp);
17+
18+
/* Finalize codecs-related state for the given interpreter. No PyCodec-related
19+
functions other than PyCodec_Unregister() may be called after this. */
20+
extern void _PyCodec_Fini(PyInterpreterState *interp);
21+
1122
extern PyObject* _PyCodec_Lookup(const char *encoding);
1223

1324
/* Text codec specific encoding and decoding API.
@@ -48,6 +59,26 @@ extern PyObject* _PyCodecInfo_GetIncrementalEncoder(
4859
PyObject *codec_info,
4960
const char *errors);
5061

62+
// Per-interpreter state used by codecs.c.
63+
struct codecs_state {
64+
// A list of callable objects used to search for codecs.
65+
PyObject *search_path;
66+
67+
// A dict mapping codec names to codecs returned from a callable in
68+
// search_path.
69+
PyObject *search_cache;
70+
71+
// A dict mapping error handling strategies to functions to implement them.
72+
PyObject *error_registry;
73+
74+
#ifdef Py_GIL_DISABLED
75+
// Used to safely delete a specific item from search_path.
76+
PyMutex search_path_mutex;
77+
#endif
78+
79+
// Whether or not the rest of the state is initialized.
80+
int initialized;
81+
};
5182

5283
#ifdef __cplusplus
5384
}

Include/internal/pycore_interp.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ extern "C" {
1414
#include "pycore_atexit.h" // struct atexit_state
1515
#include "pycore_ceval_state.h" // struct _ceval_state
1616
#include "pycore_code.h" // struct callable_cache
17+
#include "pycore_codecs.h" // struct codecs_state
1718
#include "pycore_context.h" // struct _Py_context_state
1819
#include "pycore_crossinterp.h" // struct _xidregistry
1920
#include "pycore_dict_state.h" // struct _Py_dict_state
@@ -182,10 +183,7 @@ struct _is {
182183
possible to facilitate out-of-process observability
183184
tools. */
184185

185-
PyObject *codec_search_path;
186-
PyObject *codec_search_cache;
187-
PyObject *codec_error_registry;
188-
int codecs_initialized;
186+
struct codecs_state codecs;
189187

190188
PyConfig config;
191189
unsigned long feature_flags;

Include/internal/pycore_list.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ _PyList_AppendTakeRef(PyListObject *self, PyObject *newitem)
2828
Py_ssize_t allocated = self->allocated;
2929
assert((size_t)len + 1 < PY_SSIZE_T_MAX);
3030
if (allocated > len) {
31+
#ifdef Py_GIL_DISABLED
32+
_Py_atomic_store_ptr_release(&self->ob_item[len], newitem);
33+
#else
3134
PyList_SET_ITEM(self, len, newitem);
35+
#endif
3236
Py_SET_SIZE(self, len + 1);
3337
return 0;
3438
}

Include/internal/pycore_object.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ extern int _PyDict_CheckConsistency(PyObject *mp, int check_content);
257257
when a memory block is reused from a free list.
258258
259259
Internal function called by _Py_NewReference(). */
260-
extern int _PyTraceMalloc_NewReference(PyObject *op);
260+
extern int _PyTraceMalloc_TraceRef(PyObject *op, PyRefTracerEvent event, void*);
261261

262262
// Fast inlined version of PyType_HasFeature()
263263
static inline int

Include/internal/pycore_opcode_metadata.h

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

Include/internal/pycore_runtime.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,12 @@ typedef struct _Py_DebugOffsets {
132132
} unicode_object;
133133
} _Py_DebugOffsets;
134134

135+
/* Reference tracer state */
136+
struct _reftracer_runtime_state {
137+
PyRefTracer tracer_func;
138+
void* tracer_data;
139+
};
140+
135141
/* Full Python runtime state */
136142

137143
/* _PyRuntimeState holds the global state for the CPython runtime.
@@ -236,6 +242,7 @@ typedef struct pyruntimestate {
236242
struct _fileutils_state fileutils;
237243
struct _faulthandler_runtime_state faulthandler;
238244
struct _tracemalloc_runtime_state tracemalloc;
245+
struct _reftracer_runtime_state ref_tracer;
239246

240247
// The rwmutex is used to prevent overlapping global and per-interpreter
241248
// stop-the-world events. Global stop-the-world events lock the mutex

Include/internal/pycore_runtime_init.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ extern PyTypeObject _PyExc_MemoryError;
128128
}, \
129129
.faulthandler = _faulthandler_runtime_state_INIT, \
130130
.tracemalloc = _tracemalloc_runtime_state_INIT, \
131+
.ref_tracer = { \
132+
.tracer_func = NULL, \
133+
.tracer_data = NULL, \
134+
}, \
131135
.stoptheworld = { \
132136
.is_global = 1, \
133137
}, \

0 commit comments

Comments
 (0)