diff --git a/Doc/library/pathlib.rst b/Doc/library/pathlib.rst index 6713589700ab2a..6bf4b1fae9016d 100644 --- a/Doc/library/pathlib.rst +++ b/Doc/library/pathlib.rst @@ -1618,7 +1618,7 @@ Copying, renaming and deleting .. method:: Path.unlink(missing_ok=False) Remove this file or symbolic link. If the path points to a directory, - use :func:`Path.rmdir` instead. + use :func:`Path.rmdir` or :func:`Path.delete` instead. If *missing_ok* is false (the default), :exc:`FileNotFoundError` is raised if the path does not exist. @@ -1632,33 +1632,40 @@ Copying, renaming and deleting .. method:: Path.rmdir() - Remove this directory. The directory must be empty. + Remove this directory. The directory must be empty; use + :meth:`Path.delete` to remove a non-empty directory. -.. method:: Path.rmtree(ignore_errors=False, on_error=None) +.. method:: Path.delete(ignore_errors=False, on_error=None) - Recursively delete this entire directory tree. The path must not refer to a symlink. + Delete this file or directory. If this path refers to a non-empty + directory, its files and sub-directories are deleted recursively. - If *ignore_errors* is true, errors resulting from failed removals will be - ignored. If *ignore_errors* is false or omitted, and a function is given to - *on_error*, it will be called each time an exception is raised. If neither - *ignore_errors* nor *on_error* are supplied, exceptions are propagated to - the caller. + If *ignore_errors* is true, errors resulting from failed deletions will be + ignored. If *ignore_errors* is false or omitted, and a callable is given as + the optional *on_error* argument, it will be called with one argument of + type :exc:`OSError` each time an exception is raised. The callable can + handle the error to continue the deletion process or re-raise it to stop. + Note that the filename is available as the :attr:`~OSError.filename` + attribute of the exception object. If neither *ignore_errors* nor + *on_error* are supplied, exceptions are propagated to the caller. .. note:: - On platforms that support the necessary fd-based functions, a symlink - attack-resistant version of :meth:`~Path.rmtree` is used by default. On - other platforms, the :func:`~Path.rmtree` implementation is susceptible - to a symlink attack: given proper timing and circumstances, attackers - can manipulate symlinks on the filesystem to delete files they would not - be able to access otherwise. - - If the optional argument *on_error* is specified, it should be a callable; - it will be called with one argument of type :exc:`OSError`. The - callable can handle the error to continue the deletion process or re-raise - it to stop. Note that the filename is available as the :attr:`~OSError.filename` - attribute of the exception object. + When deleting non-empty directories on platforms that lack the necessary + file descriptor-based functions, the :meth:`~Path.delete` implementation + is susceptible to a symlink attack: given proper timing and + circumstances, attackers can manipulate symlinks on the filesystem to + delete files they would not be able to access otherwise. Applications + can use the :data:`~Path.delete.avoids_symlink_attacks` method attribute + to determine whether the implementation is immune to this attack. + + .. attribute:: delete.avoids_symlink_attacks + + Indicates whether the current platform and implementation provides a + symlink attack resistant version of :meth:`~Path.delete`. Currently + this is only true for platforms supporting fd-based directory access + functions. .. versionadded:: 3.14 diff --git a/Doc/library/site.rst b/Doc/library/site.rst index 1c420419568a90..871cfefc8de310 100644 --- a/Doc/library/site.rst +++ b/Doc/library/site.rst @@ -32,7 +32,10 @@ It starts by constructing up to four directories from a head and a tail part. For the head part, it uses ``sys.prefix`` and ``sys.exec_prefix``; empty heads are skipped. For the tail part, it uses the empty string and then :file:`lib/site-packages` (on Windows) or -:file:`lib/python{X.Y}/site-packages` (on Unix and macOS). For each +:file:`lib/python{X.Y[t]}/site-packages` (on Unix and macOS). (The +optional suffix "t" indicates the :term:`free threading` build, and is +appended if ``"t"`` is present in the :attr:`sys.abiflags` constant.) +For each of the distinct head-tail combinations, it sees if it refers to an existing directory, and if so, adds it to ``sys.path`` and also inspects the newly added path for configuration files. @@ -40,6 +43,11 @@ added path for configuration files. .. versionchanged:: 3.5 Support for the "site-python" directory has been removed. +.. versionchanged:: 3.13 + On Unix, :term:`Free threading ` Python installations are + identified by the "t" suffix in the version-specific directory name, such as + :file:`lib/python3.13t/`. + If a file named "pyvenv.cfg" exists one directory above sys.executable, sys.prefix and sys.exec_prefix are set to that directory and it is also checked for site-packages (sys.base_prefix and @@ -188,11 +196,12 @@ Module contents Path to the user site-packages for the running Python. Can be ``None`` if :func:`getusersitepackages` hasn't been called yet. Default value is - :file:`~/.local/lib/python{X.Y}/site-packages` for UNIX and non-framework + :file:`~/.local/lib/python{X.Y}[t]/site-packages` for UNIX and non-framework macOS builds, :file:`~/Library/Python/{X.Y}/lib/python/site-packages` for macOS framework builds, and :file:`{%APPDATA%}\\Python\\Python{XY}\\site-packages` - on Windows. This directory is a site directory, which means that - :file:`.pth` files in it will be processed. + on Windows. The optional "t" indicates the free-threaded build. This + directory is a site directory, which means that :file:`.pth` files in it + will be processed. .. data:: USER_BASE diff --git a/Doc/whatsnew/3.14.rst b/Doc/whatsnew/3.14.rst index 908bec64b6aeda..98f813d7720afe 100644 --- a/Doc/whatsnew/3.14.rst +++ b/Doc/whatsnew/3.14.rst @@ -139,8 +139,7 @@ pathlib * :meth:`~pathlib.Path.copy` copies a file or directory tree to a given destination. - * :meth:`~pathlib.Path.rmtree` recursively removes a directory tree, like - :func:`shutil.rmtree`. + * :meth:`~pathlib.Path.delete` removes a file or directory tree. (Contributed by Barney Gale in :gh:`73991`.) diff --git a/Include/cpython/object.h b/Include/cpython/object.h index 90cd7b54b34161..e1024ddbdf6062 100644 --- a/Include/cpython/object.h +++ b/Include/cpython/object.h @@ -270,6 +270,9 @@ typedef struct _heaptypeobject { PyObject *ht_module; char *_ht_tpname; // Storage for "tp_name"; see PyType_FromModuleAndSpec struct _specialization_cache _spec_cache; // For use by the specializer. +#ifdef Py_GIL_DISABLED + Py_ssize_t unique_id; // ID used for thread-local refcounting +#endif /* here are optional user slots, followed by the members. */ } PyHeapTypeObject; diff --git a/Include/internal/pycore_gc.h b/Include/internal/pycore_gc.h index b4bf36e82e376a..5dd5b0c78d42fa 100644 --- a/Include/internal/pycore_gc.h +++ b/Include/internal/pycore_gc.h @@ -381,10 +381,6 @@ extern void _PyGC_ClearAllFreeLists(PyInterpreterState *interp); extern void _Py_ScheduleGC(PyThreadState *tstate); extern void _Py_RunGC(PyThreadState *tstate); -#ifdef Py_GIL_DISABLED -// gh-117783: Immortalize objects that use deferred reference counting -extern void _PyGC_ImmortalizeDeferredObjects(PyInterpreterState *interp); -#endif #ifdef __cplusplus } diff --git a/Include/internal/pycore_interp.h b/Include/internal/pycore_interp.h index 4a83862ac13e26..a1c1dd0c957230 100644 --- a/Include/internal/pycore_interp.h +++ b/Include/internal/pycore_interp.h @@ -35,6 +35,7 @@ extern "C" { #include "pycore_qsbr.h" // struct _qsbr_state #include "pycore_tstate.h" // _PyThreadStateImpl #include "pycore_tuple.h" // struct _Py_tuple_state +#include "pycore_typeid.h" // struct _Py_type_id_pool #include "pycore_typeobject.h" // struct types_state #include "pycore_unicodeobject.h" // struct _Py_unicode_state #include "pycore_warnings.h" // struct _warnings_runtime_state @@ -220,6 +221,7 @@ struct _is { #if defined(Py_GIL_DISABLED) struct _mimalloc_interp_state mimalloc; struct _brc_state brc; // biased reference counting state + struct _Py_type_id_pool type_ids; PyMutex weakref_locks[NUM_WEAKREF_LIST_LOCKS]; #endif diff --git a/Include/internal/pycore_object.h b/Include/internal/pycore_object.h index 155810d00bef5b..a5640b7bcb7d60 100644 --- a/Include/internal/pycore_object.h +++ b/Include/internal/pycore_object.h @@ -14,10 +14,19 @@ extern "C" { #include "pycore_interp.h" // PyInterpreterState.gc #include "pycore_pyatomic_ft_wrappers.h" // FT_ATOMIC_STORE_PTR_RELAXED #include "pycore_pystate.h" // _PyInterpreterState_GET() +#include "pycore_typeid.h" // _PyType_IncrefSlow #define _Py_IMMORTAL_REFCNT_LOOSE ((_Py_IMMORTAL_REFCNT >> 1) + 1) +// This value is added to `ob_ref_shared` for objects that use deferred +// reference counting so that they are not immediately deallocated when the +// non-deferred reference count drops to zero. +// +// The value is half the maximum shared refcount because the low two bits of +// `ob_ref_shared` are used for flags. +#define _Py_REF_DEFERRED (PY_SSIZE_T_MAX / 8) + // gh-121528, gh-118997: Similar to _Py_IsImmortal() but be more loose when // comparing the reference count to stay compatible with C extensions built // with the stable ABI 3.11 or older. Such extensions implement INCREF/DECREF @@ -280,6 +289,67 @@ extern PyStatus _PyObject_InitState(PyInterpreterState *interp); extern void _PyObject_FiniState(PyInterpreterState *interp); extern bool _PyRefchain_IsTraced(PyInterpreterState *interp, PyObject *obj); +#ifndef Py_GIL_DISABLED +# define _Py_INCREF_TYPE Py_INCREF +# define _Py_DECREF_TYPE Py_DECREF +#else +static inline void +_Py_INCREF_TYPE(PyTypeObject *type) +{ + if (!_PyType_HasFeature(type, Py_TPFLAGS_HEAPTYPE)) { + assert(_Py_IsImmortal(type)); + return; + } + + _PyThreadStateImpl *tstate = (_PyThreadStateImpl *)_PyThreadState_GET(); + PyHeapTypeObject *ht = (PyHeapTypeObject *)type; + + // Unsigned comparison so that `unique_id=-1`, which indicates that + // per-thread refcounting has been disabled on this type, is handled by + // the "else". + if ((size_t)ht->unique_id < (size_t)tstate->types.size) { +# ifdef Py_REF_DEBUG + _Py_INCREF_IncRefTotal(); +# endif + _Py_INCREF_STAT_INC(); + tstate->types.refcounts[ht->unique_id]++; + } + else { + // The slow path resizes the thread-local refcount array if necessary. + // It handles the unique_id=-1 case to keep the inlinable function smaller. + _PyType_IncrefSlow(ht); + } +} + +static inline void +_Py_DECREF_TYPE(PyTypeObject *type) +{ + if (!_PyType_HasFeature(type, Py_TPFLAGS_HEAPTYPE)) { + assert(_Py_IsImmortal(type)); + return; + } + + _PyThreadStateImpl *tstate = (_PyThreadStateImpl *)_PyThreadState_GET(); + PyHeapTypeObject *ht = (PyHeapTypeObject *)type; + + // Unsigned comparison so that `unique_id=-1`, which indicates that + // per-thread refcounting has been disabled on this type, is handled by + // the "else". + if ((size_t)ht->unique_id < (size_t)tstate->types.size) { +# ifdef Py_REF_DEBUG + _Py_DECREF_DecRefTotal(); +# endif + _Py_DECREF_STAT_INC(); + tstate->types.refcounts[ht->unique_id]--; + } + else { + // Directly decref the type if the type id is not assigned or if + // per-thread refcounting has been disabled on this type. + Py_DECREF(type); + } +} +#endif + /* Inline functions trading binary compatibility for speed: _PyObject_Init() is the fast version of PyObject_Init(), and _PyObject_InitVar() is the fast version of PyObject_InitVar(). @@ -291,7 +361,7 @@ _PyObject_Init(PyObject *op, PyTypeObject *typeobj) assert(op != NULL); Py_SET_TYPE(op, typeobj); assert(_PyType_HasFeature(typeobj, Py_TPFLAGS_HEAPTYPE) || _Py_IsImmortalLoose(typeobj)); - Py_INCREF(typeobj); + _Py_INCREF_TYPE(typeobj); _Py_NewReference(op); } diff --git a/Include/internal/pycore_tstate.h b/Include/internal/pycore_tstate.h index 18c972bd367599..f681b644c9ad5d 100644 --- a/Include/internal/pycore_tstate.h +++ b/Include/internal/pycore_tstate.h @@ -31,6 +31,16 @@ typedef struct _PyThreadStateImpl { struct _mimalloc_thread_state mimalloc; struct _Py_freelists freelists; struct _brc_thread_state brc; + struct { + // The thread-local refcounts for heap type objects + Py_ssize_t *refcounts; + + // Size of the refcounts array. + Py_ssize_t size; + + // If set, don't use thread-local refcounts + int is_finalized; + } types; #endif #if defined(Py_REF_DEBUG) && defined(Py_GIL_DISABLED) diff --git a/Include/internal/pycore_typeid.h b/Include/internal/pycore_typeid.h new file mode 100644 index 00000000000000..e64d1447f6b51d --- /dev/null +++ b/Include/internal/pycore_typeid.h @@ -0,0 +1,75 @@ +#ifndef Py_INTERNAL_TYPEID_H +#define Py_INTERNAL_TYPEID_H +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef Py_BUILD_CORE +# error "this header requires Py_BUILD_CORE define" +#endif + +#ifdef Py_GIL_DISABLED + +// This contains code for allocating unique ids to heap type objects +// and re-using those ids when the type is deallocated. +// +// The type ids are used to implement per-thread reference counts of +// heap type objects to avoid contention on the reference count fields +// of heap type objects. Static type objects are immortal, so contention +// is not an issue for those types. +// +// Type id of -1 is used to indicate a type doesn't use thread-local +// refcounting. This value is used when a type object is finalized by the GC +// and during interpreter shutdown to allow the type object to be +// deallocated promptly when the object's refcount reaches zero. +// +// Each entry implicitly represents a type id based on it's offset in the +// table. Non-allocated entries form a free-list via the 'next' pointer. +// Allocated entries store the corresponding PyTypeObject. +typedef union _Py_type_id_entry { + // Points to the next free type id, when part of the freelist + union _Py_type_id_entry *next; + + // Stores the type object when the id is assigned + PyHeapTypeObject *type; +} _Py_type_id_entry; + +struct _Py_type_id_pool { + PyMutex mutex; + + // combined table of types with allocated type ids and unallocated + // type ids. + _Py_type_id_entry *table; + + // Next entry to allocate inside 'table' or NULL + _Py_type_id_entry *freelist; + + // size of 'table' + Py_ssize_t size; +}; + +// Assigns the next id from the pool of type ids. +extern void _PyType_AssignId(PyHeapTypeObject *type); + +// Releases the allocated type id back to the pool. +extern void _PyType_ReleaseId(PyHeapTypeObject *type); + +// Merges the thread-local reference counts into the corresponding types. +extern void _PyType_MergeThreadLocalRefcounts(_PyThreadStateImpl *tstate); + +// Like _PyType_MergeThreadLocalRefcounts, but also frees the thread-local +// array of refcounts. +extern void _PyType_FinalizeThreadLocalRefcounts(_PyThreadStateImpl *tstate); + +// Frees the interpreter's pool of type ids. +extern void _PyType_FinalizeIdPool(PyInterpreterState *interp); + +// Increfs the type, resizing the thread-local refcount array if necessary. +PyAPI_FUNC(void) _PyType_IncrefSlow(PyHeapTypeObject *type); + +#endif /* Py_GIL_DISABLED */ + +#ifdef __cplusplus +} +#endif +#endif /* !Py_INTERNAL_TYPEID_H */ diff --git a/Lib/ensurepip/__init__.py b/Lib/ensurepip/__init__.py index 23fe7a82eb029d..c5350df270487a 100644 --- a/Lib/ensurepip/__init__.py +++ b/Lib/ensurepip/__init__.py @@ -10,7 +10,7 @@ __all__ = ["version", "bootstrap"] -_PIP_VERSION = "24.1.1" +_PIP_VERSION = "24.2" # Directory of system wheel packages. Some Linux distribution packaging # policies recommend against bundling dependencies. For example, Fedora diff --git a/Lib/ensurepip/_bundled/pip-24.1.1-py3-none-any.whl b/Lib/ensurepip/_bundled/pip-24.2-py3-none-any.whl similarity index 76% rename from Lib/ensurepip/_bundled/pip-24.1.1-py3-none-any.whl rename to Lib/ensurepip/_bundled/pip-24.2-py3-none-any.whl index e27568eb8b39c9..542cdd1e7284ae 100644 Binary files a/Lib/ensurepip/_bundled/pip-24.1.1-py3-none-any.whl and b/Lib/ensurepip/_bundled/pip-24.2-py3-none-any.whl differ diff --git a/Lib/pathlib/_abc.py b/Lib/pathlib/_abc.py index a2152b3e93480b..8ea6aab84a8baf 100644 --- a/Lib/pathlib/_abc.py +++ b/Lib/pathlib/_abc.py @@ -923,15 +923,15 @@ def rmdir(self): """ raise UnsupportedOperation(self._unsupported_msg('rmdir()')) - def rmtree(self, ignore_errors=False, on_error=None): + def delete(self, ignore_errors=False, on_error=None): """ - Recursively delete this directory tree. + Delete this file or directory (including all sub-directories). - If *ignore_errors* is true, exceptions raised from scanning the tree - and removing files and directories are ignored. Otherwise, if - *on_error* is set, it will be called to handle the error. If neither - *ignore_errors* nor *on_error* are set, exceptions are propagated to - the caller. + If *ignore_errors* is true, exceptions raised from scanning the + filesystem and removing files and directories are ignored. Otherwise, + if *on_error* is set, it will be called to handle the error. If + neither *ignore_errors* nor *on_error* are set, exceptions are + propagated to the caller. """ if ignore_errors: def on_error(err): @@ -939,14 +939,10 @@ def on_error(err): elif on_error is None: def on_error(err): raise err - try: - if self.is_symlink(): - raise OSError("Cannot call rmtree on a symbolic link") - elif self.is_junction(): - raise OSError("Cannot call rmtree on a junction") + if self.is_dir(follow_symlinks=False): results = self.walk( on_error=on_error, - top_down=False, # Bottom-up so we rmdir() empty directories. + top_down=False, # So we rmdir() empty directories. follow_symlinks=False) for dirpath, dirnames, filenames in results: for name in filenames: @@ -959,10 +955,15 @@ def on_error(err): dirpath.joinpath(name).rmdir() except OSError as err: on_error(err) - self.rmdir() + delete_self = self.rmdir + else: + delete_self = self.unlink + try: + delete_self() except OSError as err: err.filename = str(self) on_error(err) + delete.avoids_symlink_attacks = False def owner(self, *, follow_symlinks=True): """ diff --git a/Lib/pathlib/_local.py b/Lib/pathlib/_local.py index e658930cf663e5..e459527c92cc7c 100644 --- a/Lib/pathlib/_local.py +++ b/Lib/pathlib/_local.py @@ -3,6 +3,7 @@ import operator import os import posixpath +import shutil import sys from glob import _StringGlobber from itertools import chain @@ -823,24 +824,34 @@ def rmdir(self): """ os.rmdir(self) - def rmtree(self, ignore_errors=False, on_error=None): + def delete(self, ignore_errors=False, on_error=None): """ - Recursively delete this directory tree. + Delete this file or directory (including all sub-directories). - If *ignore_errors* is true, exceptions raised from scanning the tree - and removing files and directories are ignored. Otherwise, if - *on_error* is set, it will be called to handle the error. If neither - *ignore_errors* nor *on_error* are set, exceptions are propagated to - the caller. + If *ignore_errors* is true, exceptions raised from scanning the + filesystem and removing files and directories are ignored. Otherwise, + if *on_error* is set, it will be called to handle the error. If + neither *ignore_errors* nor *on_error* are set, exceptions are + propagated to the caller. """ - if on_error: - def onexc(func, filename, err): - err.filename = filename - on_error(err) - else: + if self.is_dir(follow_symlinks=False): onexc = None - import shutil - shutil.rmtree(str(self), ignore_errors, onexc=onexc) + if on_error: + def onexc(func, filename, err): + err.filename = filename + on_error(err) + shutil.rmtree(str(self), ignore_errors, onexc=onexc) + else: + try: + self.unlink() + except OSError as err: + if not ignore_errors: + if on_error: + on_error(err) + else: + raise + + delete.avoids_symlink_attacks = shutil.rmtree.avoids_symlink_attacks def rename(self, target): """ diff --git a/Lib/test/test_capi/test_misc.py b/Lib/test/test_capi/test_misc.py index 5c4547da1bdc53..be6fe0c1fb43de 100644 --- a/Lib/test/test_capi/test_misc.py +++ b/Lib/test/test_capi/test_misc.py @@ -1157,6 +1157,19 @@ def genf(): yield gen = genf() self.assertEqual(_testcapi.gen_get_code(gen), gen.gi_code) + def test_pyeval_getlocals(self): + # Test PyEval_GetLocals() + x = 1 + self.assertEqual(_testcapi.pyeval_getlocals(), + {'self': self, + 'x': 1}) + + y = 2 + self.assertEqual(_testcapi.pyeval_getlocals(), + {'self': self, + 'x': 1, + 'y': 2}) + @requires_limited_api class TestHeapTypeRelative(unittest.TestCase): diff --git a/Lib/test/test_pathlib/test_pathlib.py b/Lib/test/test_pathlib/test_pathlib.py index abf7016f71528d..fa151b590d7c52 100644 --- a/Lib/test/test_pathlib/test_pathlib.py +++ b/Lib/test/test_pathlib/test_pathlib.py @@ -32,7 +32,7 @@ if hasattr(os, 'geteuid'): root_in_posix = (os.geteuid() == 0) -rmtree_use_fd_functions = ( +delete_use_fd_functions = ( {os.open, os.stat, os.unlink, os.rmdir} <= os.supports_dir_fd and os.listdir in os.supports_fd and os.stat in os.supports_follow_symlinks) @@ -862,8 +862,9 @@ def test_group_no_follow_symlinks(self): self.assertEqual(expected_gid, gid_2) self.assertEqual(expected_name, link.group(follow_symlinks=False)) - def test_rmtree_uses_safe_fd_version_if_available(self): - if rmtree_use_fd_functions: + def test_delete_uses_safe_fd_version_if_available(self): + if delete_use_fd_functions: + self.assertTrue(self.cls.delete.avoids_symlink_attacks) d = self.cls(self.base, 'a') d.mkdir() try: @@ -876,16 +877,18 @@ def _raiser(*args, **kwargs): raise Called os.open = _raiser - self.assertRaises(Called, d.rmtree) + self.assertRaises(Called, d.delete) finally: os.open = real_open + else: + self.assertFalse(self.cls.delete.avoids_symlink_attacks) @unittest.skipIf(sys.platform[:6] == 'cygwin', "This test can't be run on Cygwin (issue #1071513).") @os_helper.skip_if_dac_override @os_helper.skip_unless_working_chmod - def test_rmtree_unwritable(self): - tmp = self.cls(self.base, 'rmtree') + def test_delete_unwritable(self): + tmp = self.cls(self.base, 'delete') tmp.mkdir() child_file_path = tmp / 'a' child_dir_path = tmp / 'b' @@ -902,7 +905,7 @@ def test_rmtree_unwritable(self): tmp.chmod(new_mode) errors = [] - tmp.rmtree(on_error=errors.append) + tmp.delete(on_error=errors.append) # Test whether onerror has actually been called. self.assertEqual(len(errors), 3) finally: @@ -911,9 +914,9 @@ def test_rmtree_unwritable(self): child_dir_path.chmod(old_child_dir_mode) @needs_windows - def test_rmtree_inner_junction(self): + def test_delete_inner_junction(self): import _winapi - tmp = self.cls(self.base, 'rmtree') + tmp = self.cls(self.base, 'delete') tmp.mkdir() dir1 = tmp / 'dir1' dir2 = dir1 / 'dir2' @@ -929,15 +932,15 @@ def test_rmtree_inner_junction(self): link3 = dir1 / 'link3' _winapi.CreateJunction(str(file1), str(link3)) # make sure junctions are removed but not followed - dir1.rmtree() + dir1.delete() self.assertFalse(dir1.exists()) self.assertTrue(dir3.exists()) self.assertTrue(file1.exists()) @needs_windows - def test_rmtree_outer_junction(self): + def test_delete_outer_junction(self): import _winapi - tmp = self.cls(self.base, 'rmtree') + tmp = self.cls(self.base, 'delete') tmp.mkdir() try: src = tmp / 'cheese' @@ -946,22 +949,22 @@ def test_rmtree_outer_junction(self): spam = src / 'spam' spam.write_text('') _winapi.CreateJunction(str(src), str(dst)) - self.assertRaises(OSError, dst.rmtree) - dst.rmtree(ignore_errors=True) + self.assertRaises(OSError, dst.delete) + dst.delete(ignore_errors=True) finally: - tmp.rmtree(ignore_errors=True) + tmp.delete(ignore_errors=True) @needs_windows - def test_rmtree_outer_junction_on_error(self): + def test_delete_outer_junction_on_error(self): import _winapi - tmp = self.cls(self.base, 'rmtree') + tmp = self.cls(self.base, 'delete') tmp.mkdir() dir_ = tmp / 'dir' dir_.mkdir() link = tmp / 'link' _winapi.CreateJunction(str(dir_), str(link)) try: - self.assertRaises(OSError, link.rmtree) + self.assertRaises(OSError, link.delete) self.assertTrue(dir_.exists()) self.assertTrue(link.exists(follow_symlinks=False)) errors = [] @@ -969,18 +972,18 @@ def test_rmtree_outer_junction_on_error(self): def on_error(error): errors.append(error) - link.rmtree(on_error=on_error) + link.delete(on_error=on_error) self.assertEqual(len(errors), 1) self.assertIsInstance(errors[0], OSError) self.assertEqual(errors[0].filename, str(link)) finally: os.unlink(str(link)) - @unittest.skipUnless(rmtree_use_fd_functions, "requires safe rmtree") - def test_rmtree_fails_on_close(self): + @unittest.skipUnless(delete_use_fd_functions, "requires safe delete") + def test_delete_fails_on_close(self): # Test that the error handler is called for failed os.close() and that # os.close() is only called once for a file descriptor. - tmp = self.cls(self.base, 'rmtree') + tmp = self.cls(self.base, 'delete') tmp.mkdir() dir1 = tmp / 'dir1' dir1.mkdir() @@ -996,7 +999,7 @@ def close(fd): close_count = 0 with swap_attr(os, 'close', close) as orig_close: with self.assertRaises(OSError): - dir1.rmtree() + dir1.delete() self.assertTrue(dir2.is_dir()) self.assertEqual(close_count, 2) @@ -1004,7 +1007,7 @@ def close(fd): errors = [] with swap_attr(os, 'close', close) as orig_close: - dir1.rmtree(on_error=errors.append) + dir1.delete(on_error=errors.append) self.assertEqual(len(errors), 2) self.assertEqual(errors[0].filename, str(dir2)) self.assertEqual(errors[1].filename, str(dir1)) @@ -1013,27 +1016,23 @@ def close(fd): @unittest.skipUnless(hasattr(os, "mkfifo"), 'requires os.mkfifo()') @unittest.skipIf(sys.platform == "vxworks", "fifo requires special path on VxWorks") - def test_rmtree_on_named_pipe(self): + def test_delete_on_named_pipe(self): p = self.cls(self.base, 'pipe') os.mkfifo(p) - try: - with self.assertRaises(NotADirectoryError): - p.rmtree() - self.assertTrue(p.exists()) - finally: - p.unlink() + p.delete() + self.assertFalse(p.exists()) p = self.cls(self.base, 'dir') p.mkdir() os.mkfifo(p / 'mypipe') - p.rmtree() + p.delete() self.assertFalse(p.exists()) @unittest.skipIf(sys.platform[:6] == 'cygwin', "This test can't be run on Cygwin (issue #1071513).") @os_helper.skip_if_dac_override @os_helper.skip_unless_working_chmod - def test_rmtree_deleted_race_condition(self): + def test_delete_deleted_race_condition(self): # bpo-37260 # # Test that a file or a directory deleted after it is enumerated @@ -1057,7 +1056,7 @@ def on_error(exc): if p != keep: p.unlink() - tmp = self.cls(self.base, 'rmtree') + tmp = self.cls(self.base, 'delete') tmp.mkdir() paths = [tmp] + [tmp / f'child{i}' for i in range(6)] dirs = paths[1::2] @@ -1075,7 +1074,7 @@ def on_error(exc): path.chmod(new_mode) try: - tmp.rmtree(on_error=on_error) + tmp.delete(on_error=on_error) except: # Test failed, so cleanup artifacts. for path, mode in zip(paths, old_modes): @@ -1083,13 +1082,13 @@ def on_error(exc): path.chmod(mode) except OSError: pass - tmp.rmtree() + tmp.delete() raise - def test_rmtree_does_not_choke_on_failing_lstat(self): + def test_delete_does_not_choke_on_failing_lstat(self): try: orig_lstat = os.lstat - tmp = self.cls(self.base, 'rmtree') + tmp = self.cls(self.base, 'delete') def raiser(fn, *args, **kwargs): if fn != str(tmp): @@ -1102,7 +1101,7 @@ def raiser(fn, *args, **kwargs): tmp.mkdir() foo = tmp / 'foo' foo.write_text('') - tmp.rmtree() + tmp.delete() finally: os.lstat = orig_lstat diff --git a/Lib/test/test_pathlib/test_pathlib_abc.py b/Lib/test/test_pathlib/test_pathlib_abc.py index 0026377b1cc7ed..4cad9864c22b79 100644 --- a/Lib/test/test_pathlib/test_pathlib_abc.py +++ b/Lib/test/test_pathlib/test_pathlib_abc.py @@ -2627,85 +2627,43 @@ def test_rmdir(self): self.assertFileNotFound(p.stat) self.assertFileNotFound(p.unlink) - def test_rmtree(self): + def test_delete_file(self): + p = self.cls(self.base) / 'fileA' + p.delete() + self.assertFileNotFound(p.stat) + self.assertFileNotFound(p.unlink) + + def test_delete_dir(self): base = self.cls(self.base) - base.joinpath('dirA').rmtree() + base.joinpath('dirA').delete() self.assertRaises(FileNotFoundError, base.joinpath('dirA').stat) self.assertRaises(FileNotFoundError, base.joinpath('dirA', 'linkC').lstat) - base.joinpath('dirB').rmtree() + base.joinpath('dirB').delete() self.assertRaises(FileNotFoundError, base.joinpath('dirB').stat) self.assertRaises(FileNotFoundError, base.joinpath('dirB', 'fileB').stat) self.assertRaises(FileNotFoundError, base.joinpath('dirB', 'linkD').lstat) - base.joinpath('dirC').rmtree() + base.joinpath('dirC').delete() self.assertRaises(FileNotFoundError, base.joinpath('dirC').stat) self.assertRaises(FileNotFoundError, base.joinpath('dirC', 'dirD').stat) self.assertRaises(FileNotFoundError, base.joinpath('dirC', 'dirD', 'fileD').stat) self.assertRaises(FileNotFoundError, base.joinpath('dirC', 'fileC').stat) self.assertRaises(FileNotFoundError, base.joinpath('dirC', 'novel.txt').stat) - def test_rmtree_errors(self): - tmp = self.cls(self.base, 'rmtree') - tmp.mkdir() - # filename is guaranteed not to exist - filename = tmp / 'foo' - self.assertRaises(FileNotFoundError, filename.rmtree) - # test that ignore_errors option is honored - filename.rmtree(ignore_errors=True) - - # existing file - filename = tmp / "tstfile" - filename.write_text("") - with self.assertRaises(NotADirectoryError) as cm: - filename.rmtree() - self.assertEqual(cm.exception.filename, str(filename)) - self.assertTrue(filename.exists()) - # test that ignore_errors option is honored - filename.rmtree(ignore_errors=True) - self.assertTrue(filename.exists()) - - def test_rmtree_on_error(self): - tmp = self.cls(self.base, 'rmtree') - tmp.mkdir() - filename = tmp / "tstfile" - filename.write_text("") - errors = [] - - def on_error(error): - errors.append(error) - - filename.rmtree(on_error=on_error) - self.assertEqual(len(errors), 2) - # First from scandir() - self.assertIsInstance(errors[0], NotADirectoryError) - self.assertEqual(errors[0].filename, str(filename)) - # Then from munlink() - self.assertIsInstance(errors[1], NotADirectoryError) - self.assertEqual(errors[1].filename, str(filename)) - @needs_symlinks - def test_rmtree_outer_symlink(self): - tmp = self.cls(self.base, 'rmtree') + def test_delete_symlink(self): + tmp = self.cls(self.base, 'delete') tmp.mkdir() dir_ = tmp / 'dir' dir_.mkdir() link = tmp / 'link' link.symlink_to(dir_) - self.assertRaises(OSError, link.rmtree) + link.delete() self.assertTrue(dir_.exists()) - self.assertTrue(link.exists(follow_symlinks=False)) - errors = [] - - def on_error(error): - errors.append(error) - - link.rmtree(on_error=on_error) - self.assertEqual(len(errors), 1) - self.assertIsInstance(errors[0], OSError) - self.assertEqual(errors[0].filename, str(link)) + self.assertFalse(link.exists(follow_symlinks=False)) @needs_symlinks - def test_rmtree_inner_symlink(self): - tmp = self.cls(self.base, 'rmtree') + def test_delete_inner_symlink(self): + tmp = self.cls(self.base, 'delete') tmp.mkdir() dir1 = tmp / 'dir1' dir2 = dir1 / 'dir2' @@ -2721,11 +2679,26 @@ def test_rmtree_inner_symlink(self): link3 = dir1 / 'link3' link3.symlink_to(file1) # make sure symlinks are removed but not followed - dir1.rmtree() + dir1.delete() self.assertFalse(dir1.exists()) self.assertTrue(dir3.exists()) self.assertTrue(file1.exists()) + def test_delete_missing(self): + tmp = self.cls(self.base, 'delete') + tmp.mkdir() + # filename is guaranteed not to exist + filename = tmp / 'foo' + self.assertRaises(FileNotFoundError, filename.delete) + # test that ignore_errors option is honored + filename.delete(ignore_errors=True) + # test on_error + errors = [] + filename.delete(on_error=errors.append) + self.assertEqual(len(errors), 1) + self.assertIsInstance(errors[0], FileNotFoundError) + self.assertEqual(errors[0].filename, str(filename)) + def setUpWalk(self): # Build: # TESTFN/ diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py index 709355e293f2fc..42b5a7c94e7700 100644 --- a/Lib/test/test_sys.py +++ b/Lib/test/test_sys.py @@ -1710,6 +1710,7 @@ def delx(self): del self.__x fmt = 'P2nPI13Pl4Pn9Pn12PIPc' s = vsize(fmt) check(int, s) + typeid = 'n' if support.Py_GIL_DISABLED else '' # class s = vsize(fmt + # PyTypeObject '4P' # PyAsyncMethods @@ -1718,7 +1719,8 @@ def delx(self): del self.__x '10P' # PySequenceMethods '2P' # PyBufferProcs '6P' - '1PIP' # Specializer cache + '1PIP' # Specializer cache + + typeid # heap type id (free-threaded only) ) class newstyleclass(object): pass # Separate block for PyDictKeysObject with 8 keys and 5 entries diff --git a/Makefile.pre.in b/Makefile.pre.in index 5608e593ac9aca..66b3665e9c29ae 100644 --- a/Makefile.pre.in +++ b/Makefile.pre.in @@ -483,6 +483,7 @@ PYTHON_OBJS= \ Python/thread.o \ Python/traceback.o \ Python/tracemalloc.o \ + Python/typeid.o \ Python/getopt.o \ Python/pystrcmp.o \ Python/pystrtod.o \ @@ -1257,6 +1258,7 @@ PYTHON_HEADERS= \ $(srcdir)/Include/internal/pycore_tracemalloc.h \ $(srcdir)/Include/internal/pycore_tstate.h \ $(srcdir)/Include/internal/pycore_tuple.h \ + $(srcdir)/Include/internal/pycore_typeid.h \ $(srcdir)/Include/internal/pycore_typeobject.h \ $(srcdir)/Include/internal/pycore_typevarobject.h \ $(srcdir)/Include/internal/pycore_ucnhash.h \ diff --git a/Misc/NEWS.d/next/C_API/2024-08-06-14-23-11.gh-issue-122728.l-fQ-v.rst b/Misc/NEWS.d/next/C_API/2024-08-06-14-23-11.gh-issue-122728.l-fQ-v.rst new file mode 100644 index 00000000000000..a128d6aef34dfc --- /dev/null +++ b/Misc/NEWS.d/next/C_API/2024-08-06-14-23-11.gh-issue-122728.l-fQ-v.rst @@ -0,0 +1,2 @@ +Fix :c:func:`PyEval_GetLocals` to avoid :exc:`SystemError` ("bad argument to +internal function"). Patch by Victor Stinner. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2024-07-29-19-20-25.gh-issue-122417.NVgs0a.rst b/Misc/NEWS.d/next/Core_and_Builtins/2024-07-29-19-20-25.gh-issue-122417.NVgs0a.rst new file mode 100644 index 00000000000000..b050c9ce39c054 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2024-07-29-19-20-25.gh-issue-122417.NVgs0a.rst @@ -0,0 +1,4 @@ +In the free-threaded build, the reference counts for heap type objects are now +partially stored in a distributed manner in per-thread arrays. This reduces +contention on the heap type's reference count fields when creating or +destroying instances of the same type from multiple threads concurrently. diff --git a/Misc/NEWS.d/next/Library/2024-05-15-01-21-44.gh-issue-73991.bNDqQN.rst b/Misc/NEWS.d/next/Library/2024-05-15-01-21-44.gh-issue-73991.bNDqQN.rst index 9aa7a7dba666af..5806fed91c7880 100644 --- a/Misc/NEWS.d/next/Library/2024-05-15-01-21-44.gh-issue-73991.bNDqQN.rst +++ b/Misc/NEWS.d/next/Library/2024-05-15-01-21-44.gh-issue-73991.bNDqQN.rst @@ -1 +1 @@ -Add :meth:`pathlib.Path.rmtree`, which recursively removes a directory. +Add :meth:`pathlib.Path.delete`, which recursively removes a file or directory. diff --git a/Misc/NEWS.d/next/Library/2024-08-06-18-07-19.gh-issue-122744.kCzNDI.rst b/Misc/NEWS.d/next/Library/2024-08-06-18-07-19.gh-issue-122744.kCzNDI.rst new file mode 100644 index 00000000000000..18ac3dd10d6553 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-08-06-18-07-19.gh-issue-122744.kCzNDI.rst @@ -0,0 +1 @@ +Bump the version of pip bundled in ensurepip to version 24.2. diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c index 4a371a5ce33ebe..05deb0549fa637 100644 --- a/Modules/_testcapimodule.c +++ b/Modules/_testcapimodule.c @@ -3341,6 +3341,12 @@ test_critical_sections(PyObject *module, PyObject *Py_UNUSED(args)) Py_RETURN_NONE; } +static PyObject * +pyeval_getlocals(PyObject *module, PyObject *Py_UNUSED(args)) +{ + return Py_XNewRef(PyEval_GetLocals()); +} + static PyMethodDef TestMethods[] = { {"set_errno", set_errno, METH_VARARGS}, {"test_config", test_config, METH_NOARGS}, @@ -3483,6 +3489,7 @@ static PyMethodDef TestMethods[] = { {"test_weakref_capi", test_weakref_capi, METH_NOARGS}, {"function_set_warning", function_set_warning, METH_NOARGS}, {"test_critical_sections", test_critical_sections, METH_NOARGS}, + {"pyeval_getlocals", pyeval_getlocals, METH_NOARGS}, {NULL, NULL} /* sentinel */ }; diff --git a/Objects/object.c b/Objects/object.c index db9d3e46795668..c6d46caa0bb62b 100644 --- a/Objects/object.c +++ b/Objects/object.c @@ -2477,15 +2477,7 @@ _PyObject_SetDeferredRefcount(PyObject *op) assert(_Py_IsOwnedByCurrentThread(op)); assert(op->ob_ref_shared == 0); _PyObject_SET_GC_BITS(op, _PyGC_BITS_DEFERRED); - PyInterpreterState *interp = _PyInterpreterState_GET(); - if (_Py_atomic_load_int_relaxed(&interp->gc.immortalize) == 1) { - // gh-117696: immortalize objects instead of using deferred reference - // counting for now. - _Py_SetImmortal(op); - return; - } - op->ob_ref_local += 1; - op->ob_ref_shared = _Py_REF_QUEUED; + op->ob_ref_shared = _Py_REF_SHARED(_Py_REF_DEFERRED, 0); #endif } diff --git a/Objects/typeobject.c b/Objects/typeobject.c index a2d82e65b6ad9f..00f0dc9849b5c8 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -2452,7 +2452,7 @@ subtype_dealloc(PyObject *self) reference counting. Only decref if the base type is not already a heap allocated type. Otherwise, basedealloc should have decref'd it already */ if (type_needs_decref) { - Py_DECREF(type); + _Py_DECREF_TYPE(type); } /* Done */ @@ -2562,7 +2562,7 @@ subtype_dealloc(PyObject *self) reference counting. Only decref if the base type is not already a heap allocated type. Otherwise, basedealloc should have decref'd it already */ if (type_needs_decref) { - Py_DECREF(type); + _Py_DECREF_TYPE(type); } endlabel: @@ -3913,7 +3913,9 @@ type_new_alloc(type_new_ctx *ctx) et->ht_module = NULL; et->_ht_tpname = NULL; - _PyObject_SetDeferredRefcount((PyObject *)et); +#ifdef Py_GIL_DISABLED + _PyType_AssignId(et); +#endif return type; } @@ -4965,6 +4967,11 @@ _PyType_FromMetaclass_impl( type->tp_weaklistoffset = weaklistoffset; type->tp_dictoffset = dictoffset; +#ifdef Py_GIL_DISABLED + // Assign a type id to enable thread-local refcounting + _PyType_AssignId(res); +#endif + /* Ready the type (which includes inheritance). * * After this call we should generally only touch up what's @@ -5914,6 +5921,9 @@ type_dealloc(PyObject *self) } Py_XDECREF(et->ht_module); PyMem_Free(et->_ht_tpname); +#ifdef Py_GIL_DISABLED + _PyType_ReleaseId(et); +#endif Py_TYPE(type)->tp_free((PyObject *)type); } diff --git a/PCbuild/_freeze_module.vcxproj b/PCbuild/_freeze_module.vcxproj index e5e18de60ec349..962d754e4a121d 100644 --- a/PCbuild/_freeze_module.vcxproj +++ b/PCbuild/_freeze_module.vcxproj @@ -267,6 +267,7 @@ + diff --git a/PCbuild/_freeze_module.vcxproj.filters b/PCbuild/_freeze_module.vcxproj.filters index 9630f54ae4ea29..86146f73857bd4 100644 --- a/PCbuild/_freeze_module.vcxproj.filters +++ b/PCbuild/_freeze_module.vcxproj.filters @@ -464,6 +464,9 @@ Source Files + + Source Files + Source Files diff --git a/PCbuild/pythoncore.vcxproj b/PCbuild/pythoncore.vcxproj index 9e3af689f4a288..7991eb93aa2c8a 100644 --- a/PCbuild/pythoncore.vcxproj +++ b/PCbuild/pythoncore.vcxproj @@ -304,6 +304,7 @@ + @@ -643,6 +644,7 @@ + diff --git a/PCbuild/pythoncore.vcxproj.filters b/PCbuild/pythoncore.vcxproj.filters index 31f7971bda845d..075910915fb912 100644 --- a/PCbuild/pythoncore.vcxproj.filters +++ b/PCbuild/pythoncore.vcxproj.filters @@ -831,6 +831,9 @@ Include\internal + + Include\internal + Include\internal @@ -1493,6 +1496,9 @@ Python + + Python + Python diff --git a/Python/ceval.c b/Python/ceval.c index f1663ee539aeac..c685a95b2ef088 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -2499,7 +2499,7 @@ PyEval_GetLocals(void) PyFrameObject *f = _PyFrame_GetFrameObject(current_frame); PyObject *ret = f->f_locals_cache; if (ret == NULL) { - PyObject *ret = PyDict_New(); + ret = PyDict_New(); if (ret == NULL) { Py_DECREF(locals); return NULL; diff --git a/Python/gc_free_threading.c b/Python/gc_free_threading.c index 53f04160c38841..1e02db00649c75 100644 --- a/Python/gc_free_threading.c +++ b/Python/gc_free_threading.c @@ -15,6 +15,7 @@ #include "pycore_tstate.h" // _PyThreadStateImpl #include "pycore_weakref.h" // _PyWeakref_ClearRef() #include "pydtrace.h" +#include "pycore_typeid.h" // _PyType_MergeThreadLocalRefcounts #ifdef Py_GIL_DISABLED @@ -164,7 +165,15 @@ disable_deferred_refcounting(PyObject *op) { if (_PyObject_HasDeferredRefcount(op)) { op->ob_gc_bits &= ~_PyGC_BITS_DEFERRED; - op->ob_ref_shared -= (1 << _Py_REF_SHARED_SHIFT); + op->ob_ref_shared -= _Py_REF_SHARED(_Py_REF_DEFERRED, 0); + + if (PyType_Check(op)) { + // Disable thread-local refcounting for heap types + PyTypeObject *type = (PyTypeObject *)op; + if (PyType_HasFeature(type, Py_TPFLAGS_HEAPTYPE)) { + _PyType_ReleaseId((PyHeapTypeObject *)op); + } + } } } @@ -328,16 +337,6 @@ merge_queued_objects(_PyThreadStateImpl *tstate, struct collection_state *state) } } -static void -merge_all_queued_objects(PyInterpreterState *interp, struct collection_state *state) -{ - HEAD_LOCK(&_PyRuntime); - for (PyThreadState *p = interp->threads.head; p != NULL; p = p->next) { - merge_queued_objects((_PyThreadStateImpl *)p, state); - } - HEAD_UNLOCK(&_PyRuntime); -} - static void process_delayed_frees(PyInterpreterState *interp) { @@ -389,7 +388,9 @@ update_refs(const mi_heap_t *heap, const mi_heap_area_t *area, } Py_ssize_t refcount = Py_REFCNT(op); - refcount -= _PyObject_HasDeferredRefcount(op); + if (_PyObject_HasDeferredRefcount(op)) { + refcount -= _Py_REF_DEFERRED; + } _PyObject_ASSERT(op, refcount >= 0); if (refcount > 0 && !_PyObject_HasDeferredRefcount(op)) { @@ -754,10 +755,6 @@ _PyGC_Init(PyInterpreterState *interp) { GCState *gcstate = &interp->gc; - // gh-117783: immortalize objects that would use deferred refcounting - // once the first non-main thread is created (but not in subinterpreters). - gcstate->immortalize = _Py_IsMainInterpreter(interp) ? 0 : -1; - gcstate->garbage = PyList_New(0); if (gcstate->garbage == NULL) { return _PyStatus_NO_MEMORY(); @@ -1105,8 +1102,18 @@ gc_collect_internal(PyInterpreterState *interp, struct collection_state *state, state->gcstate->old[i-1].count = 0; } - // merge refcounts for all queued objects - merge_all_queued_objects(interp, state); + HEAD_LOCK(&_PyRuntime); + for (PyThreadState *p = interp->threads.head; p != NULL; p = p->next) { + _PyThreadStateImpl *tstate = (_PyThreadStateImpl *)p; + + // merge per-thread refcount for types into the type's actual refcount + _PyType_MergeThreadLocalRefcounts(tstate); + + // merge refcounts for all queued objects + merge_queued_objects(tstate, state); + } + HEAD_UNLOCK(&_PyRuntime); + process_delayed_frees(interp); // Find unreachable objects @@ -1835,32 +1842,6 @@ custom_visitor_wrapper(const mi_heap_t *heap, const mi_heap_area_t *area, return true; } -// gh-117783: Immortalize objects that use deferred reference counting to -// temporarily work around scaling bottlenecks. -static bool -immortalize_visitor(const mi_heap_t *heap, const mi_heap_area_t *area, - void *block, size_t block_size, void *args) -{ - PyObject *op = op_from_block(block, args, false); - if (op != NULL && _PyObject_HasDeferredRefcount(op)) { - _Py_SetImmortal(op); - op->ob_gc_bits &= ~_PyGC_BITS_DEFERRED; - } - return true; -} - -void -_PyGC_ImmortalizeDeferredObjects(PyInterpreterState *interp) -{ - struct visitor_args args; - _PyEval_StopTheWorld(interp); - if (interp->gc.immortalize == 0) { - gc_visit_heaps(interp, &immortalize_visitor, &args); - interp->gc.immortalize = 1; - } - _PyEval_StartTheWorld(interp); -} - void PyUnstable_GC_VisitObjects(gcvisitobjects_t callback, void *arg) { diff --git a/Python/pystate.c b/Python/pystate.c index 6fbd17f7eaeaa9..8f4818cee00d9d 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -20,6 +20,7 @@ #include "pycore_runtime_init.h" // _PyRuntimeState_INIT #include "pycore_sysmodule.h" // _PySys_Audit() #include "pycore_obmalloc.h" // _PyMem_obmalloc_state_on_heap() +#include "pycore_typeid.h" // _PyType_FinalizeIdPool /* -------------------------------------------------------------------------- CAUTION @@ -1584,13 +1585,6 @@ new_threadstate(PyInterpreterState *interp, int whence) PyMem_RawFree(new_tstate); } else { -#ifdef Py_GIL_DISABLED - if (_Py_atomic_load_int(&interp->gc.immortalize) == 0) { - // Immortalize objects marked as using deferred reference counting - // the first time a non-main thread is created. - _PyGC_ImmortalizeDeferredObjects(interp); - } -#endif } #ifdef Py_GIL_DISABLED @@ -1741,6 +1735,10 @@ PyThreadState_Clear(PyThreadState *tstate) struct _Py_freelists *freelists = _Py_freelists_GET(); _PyObject_ClearFreeLists(freelists, 1); + // Merge our thread-local refcounts into the type's own refcount and + // free our local refcount array. + _PyType_FinalizeThreadLocalRefcounts((_PyThreadStateImpl *)tstate); + // Remove ourself from the biased reference counting table of threads. _Py_brc_remove_thread(tstate); #endif @@ -1799,6 +1797,7 @@ tstate_delete_common(PyThreadState *tstate, int release_gil) _PyThreadStateImpl *tstate_impl = (_PyThreadStateImpl *)tstate; tstate->interp->object_state.reftotal += tstate_impl->reftotal; tstate_impl->reftotal = 0; + assert(tstate_impl->types.refcounts == NULL); #endif HEAD_UNLOCK(runtime); diff --git a/Python/typeid.c b/Python/typeid.c new file mode 100644 index 00000000000000..83a68723ded61b --- /dev/null +++ b/Python/typeid.c @@ -0,0 +1,200 @@ +#include "Python.h" + +#include "pycore_lock.h" // PyMutex_LockFlags() +#include "pycore_pystate.h" // _PyThreadState_GET() +#include "pycore_object.h" // _Py_IncRefTotal +#include "pycore_typeid.h" + +// This contains code for allocating unique ids to heap type objects +// and re-using those ids when the type is deallocated. +// +// See Include/internal/pycore_typeid.h for more details. + +#ifdef Py_GIL_DISABLED + +#define POOL_MIN_SIZE 8 + +#define LOCK_POOL(pool) PyMutex_LockFlags(&pool->mutex, _Py_LOCK_DONT_DETACH) +#define UNLOCK_POOL(pool) PyMutex_Unlock(&pool->mutex) + +static int +resize_interp_type_id_pool(struct _Py_type_id_pool *pool) +{ + if ((size_t)pool->size > PY_SSIZE_T_MAX / (2 * sizeof(*pool->table))) { + return -1; + } + + Py_ssize_t new_size = pool->size * 2; + if (new_size < POOL_MIN_SIZE) { + new_size = POOL_MIN_SIZE; + } + + _Py_type_id_entry *table = PyMem_Realloc(pool->table, + new_size * sizeof(*pool->table)); + if (table == NULL) { + return -1; + } + + Py_ssize_t start = pool->size; + for (Py_ssize_t i = start; i < new_size - 1; i++) { + table[i].next = &table[i + 1]; + } + table[new_size - 1].next = NULL; + + pool->table = table; + pool->freelist = &table[start]; + _Py_atomic_store_ssize(&pool->size, new_size); + return 0; +} + +static int +resize_local_refcounts(_PyThreadStateImpl *tstate) +{ + if (tstate->types.is_finalized) { + return -1; + } + + struct _Py_type_id_pool *pool = &tstate->base.interp->type_ids; + Py_ssize_t size = _Py_atomic_load_ssize(&pool->size); + + Py_ssize_t *refcnts = PyMem_Realloc(tstate->types.refcounts, + size * sizeof(Py_ssize_t)); + if (refcnts == NULL) { + return -1; + } + + Py_ssize_t old_size = tstate->types.size; + if (old_size < size) { + memset(refcnts + old_size, 0, (size - old_size) * sizeof(Py_ssize_t)); + } + + tstate->types.refcounts = refcnts; + tstate->types.size = size; + return 0; +} + +void +_PyType_AssignId(PyHeapTypeObject *type) +{ + PyInterpreterState *interp = _PyInterpreterState_GET(); + struct _Py_type_id_pool *pool = &interp->type_ids; + + LOCK_POOL(pool); + if (pool->freelist == NULL) { + if (resize_interp_type_id_pool(pool) < 0) { + type->unique_id = -1; + UNLOCK_POOL(pool); + return; + } + } + + _Py_type_id_entry *entry = pool->freelist; + pool->freelist = entry->next; + entry->type = type; + _PyObject_SetDeferredRefcount((PyObject *)type); + type->unique_id = (entry - pool->table); + UNLOCK_POOL(pool); +} + +void +_PyType_ReleaseId(PyHeapTypeObject *type) +{ + PyInterpreterState *interp = _PyInterpreterState_GET(); + struct _Py_type_id_pool *pool = &interp->type_ids; + + if (type->unique_id < 0) { + // The type doesn't have an id assigned. + return; + } + + LOCK_POOL(pool); + _Py_type_id_entry *entry = &pool->table[type->unique_id]; + assert(entry->type == type); + entry->next = pool->freelist; + pool->freelist = entry; + + type->unique_id = -1; + UNLOCK_POOL(pool); +} + +void +_PyType_IncrefSlow(PyHeapTypeObject *type) +{ + _PyThreadStateImpl *tstate = (_PyThreadStateImpl *)_PyThreadState_GET(); + if (type->unique_id < 0 || resize_local_refcounts(tstate) < 0) { + // just incref the type directly. + Py_INCREF(type); + return; + } + + assert(type->unique_id < tstate->types.size); + tstate->types.refcounts[type->unique_id]++; +#ifdef Py_REF_DEBUG + _Py_IncRefTotal((PyThreadState *)tstate); +#endif + _Py_INCREF_STAT_INC(); +} + +void +_PyType_MergeThreadLocalRefcounts(_PyThreadStateImpl *tstate) +{ + if (tstate->types.refcounts == NULL) { + return; + } + + struct _Py_type_id_pool *pool = &tstate->base.interp->type_ids; + + LOCK_POOL(pool); + for (Py_ssize_t i = 0, n = tstate->types.size; i < n; i++) { + Py_ssize_t refcnt = tstate->types.refcounts[i]; + if (refcnt != 0) { + PyObject *type = (PyObject *)pool->table[i].type; + assert(PyType_Check(type)); + + _Py_atomic_add_ssize(&type->ob_ref_shared, + refcnt << _Py_REF_SHARED_SHIFT); + tstate->types.refcounts[i] = 0; + } + } + UNLOCK_POOL(pool); +} + +void +_PyType_FinalizeThreadLocalRefcounts(_PyThreadStateImpl *tstate) +{ + _PyType_MergeThreadLocalRefcounts(tstate); + + PyMem_Free(tstate->types.refcounts); + tstate->types.refcounts = NULL; + tstate->types.size = 0; + tstate->types.is_finalized = 1; +} + +void +_PyType_FinalizeIdPool(PyInterpreterState *interp) +{ + struct _Py_type_id_pool *pool = &interp->type_ids; + + // First, set the free-list to NULL values + while (pool->freelist) { + _Py_type_id_entry *next = pool->freelist->next; + pool->freelist->type = NULL; + pool->freelist = next; + } + + // Now everything non-NULL is a type. Set the type's id to -1 in case it + // outlives the interpreter. + for (Py_ssize_t i = 0; i < pool->size; i++) { + PyHeapTypeObject *ht = pool->table[i].type; + if (ht) { + ht->unique_id = -1; + pool->table[i].type = NULL; + } + } + PyMem_Free(pool->table); + pool->table = NULL; + pool->freelist = NULL; + pool->size = 0; +} + +#endif /* Py_GIL_DISABLED */ diff --git a/configure b/configure index 39ab48fa4e2526..329373560f8578 100755 --- a/configure +++ b/configure @@ -28697,9 +28697,6 @@ fi # builtin hash modules default_hashlib_hashes="md5,sha1,sha2,sha3,blake2" - -printf "%s\n" "#define PY_BUILTIN_HASHLIB_HASHES /**/" >>confdefs.h - { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for --with-builtin-hashlib-hashes" >&5 printf %s "checking for --with-builtin-hashlib-hashes... " >&6; } @@ -28724,6 +28721,7 @@ fi { printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $with_builtin_hashlib_hashes" >&5 printf "%s\n" "$with_builtin_hashlib_hashes" >&6; } + printf "%s\n" "#define PY_BUILTIN_HASHLIB_HASHES \"$with_builtin_hashlib_hashes\"" >>confdefs.h diff --git a/configure.ac b/configure.ac index 62ed812991fc4e..fb40607abfa73c 100644 --- a/configure.ac +++ b/configure.ac @@ -7403,8 +7403,6 @@ AC_DEFINE([PY_SSL_DEFAULT_CIPHERS], [1]) # builtin hash modules default_hashlib_hashes="md5,sha1,sha2,sha3,blake2" -AC_DEFINE([PY_BUILTIN_HASHLIB_HASHES], [], [enabled builtin hash modules] -) AC_MSG_CHECKING([for --with-builtin-hashlib-hashes]) AC_ARG_WITH( [builtin-hashlib-hashes], @@ -7421,7 +7419,8 @@ AC_ARG_WITH( AC_MSG_RESULT([$with_builtin_hashlib_hashes]) AC_DEFINE_UNQUOTED([PY_BUILTIN_HASHLIB_HASHES], - ["$with_builtin_hashlib_hashes"]) + ["$with_builtin_hashlib_hashes"], + [enabled builtin hash modules]) as_save_IFS=$IFS IFS=,