summaryrefslogtreecommitdiffstats
path: root/Objects/dictobject.c
Commit message (Collapse)AuthorAgeFilesLines
* gh-132869: Fix crash in `_PyObject_TryGetInstanceAttribute` (#133700)Sam Gross2025-05-141-8/+50
| | | | | | | | | This fixes a crash in `_PyObject_TryGetInstanceAttribute` due to the use of `_PyDictKeys_StringLookup` on an unlocked dictionary that may be concurrently modified. The underlying bug was already fixed in 3.14 and the main branch. (partially cherry picked from commit 1b15c89a17ca3de6b05de5379b8717e9738c51ef)
* [3.13] GH-133543: Maintain tracking for materialized instance dictionaries ↵Brandt Bucher2025-05-121-0/+3
| | | | (GH-133617)
* gh-133703: dict: fix calculate_log2_keysize() (GH-133809)Inada Naoki2025-05-111-5/+5
| | | (cherry picked from commit 92337f666e8a076a68305a8d6dc8bc9c095000e9)
* [3.13] gh-132762: Fix underallocation bug in `dict.fromkeys()`(gh-133627) ↵Sam Gross2025-05-081-3/+4
| | | | | | | | | | | (gh-133686) The function `dict_set_fromkeys()` adds elements of a set to an existing dictionary. The size of the expanded dictionary was estimated with `PySet_GET_SIZE(iterable)`, which did not take into account the size of the existing dictionary. (cherry picked from commit 421ba589d02b53131f793889d221ef3b1f1410a4) Co-authored-by: Angela Liss <59097311+angela-tarantula@users.noreply.github.com>
* [3.13] gh-131113: Fix data race in dict.popitem() (gh-131115) (#131119)Miss Islington (bot)2025-03-111-8/+8
| | | | | | | The clearing of the key, hash, and value need to use atomic operations to avoid a data race with concurrent read operations. (cherry picked from commit c00ac578241b3213ceb79c1f32bc83ea471f02da) Co-authored-by: Sam Gross <colesbury@gmail.com>
* [3.13] gh-130547: Fix race between dict_dealloc and split_keys_entry_added ↵Miss Islington (bot)2025-03-051-1/+1
| | | | | | | | (gh-130778) (gh-130833) gh-130547: Fix race between dict_dealloc and split_keys_entry_added (gh-130778) (cherry picked from commit 80e6d3ec4972220587c8b883161311a49ea8d0ff) Co-authored-by: Donghee Na <donghee.na@python.org>
* [3.13] gh-117657: Fix data race in `dict_dict_merge` (gh-129755) (gh-129808)Miss Islington (bot)2025-02-071-1/+1
| | | | | | Found while running `test_load_attr_module` from `test_opcache` under TSan. (cherry picked from commit 34379d0a593e5a76e2f754cdd7fccb79f25a4613) Co-authored-by: Sam Gross <colesbury@gmail.com>
* [3.13] gh-127563: use `dk_log2_index_bytes=3` in empty dicts (GH-127568) ↵Miss Islington (bot)2024-12-111-1/+4
| | | | | | | | (GH-127798) This fixes a UBSan failure (unaligned zero-size memcpy) in `dictobject.c`. (cherry picked from commit 9af96f440618304e7cc609c246e1f8c8b2d7a119) Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
* [3.13] gh-127582: Make object resurrection thread-safe for free threading. ↵Sam Gross2024-12-051-5/+2
| | | | | | | | | | | | | | | (GH-127612) (GH-127659) Objects may be temporarily "resurrected" in destructors when calling finalizers or watcher callbacks. We previously undid the resurrection by decrementing the reference count using `Py_SET_REFCNT`. This was not thread-safe because other threads might be accessing the object (modifying its reference count) if it was exposed by the finalizer, watcher callback, or temporarily accessed by a racy dictionary or list access. This adds internal-only thread-safe functions for temporary object resurrection during destructors. (cherry picked from commit f4f530804b9d8f089eba0f157ec2144c03b13651)
* [3.13] gh-127316: fix incorrect assertion in setting `__class__` in ↵Miss Islington (bot)2024-11-291-1/+1
| | | | | | | | free-threading (GH-127399) (#127422) gh-127316: fix incorrect assertion in setting `__class__` in free-threading (GH-127399) (cherry picked from commit 45c5cba318a19dda3ee6f9fc84781cc7a2fbde80) Co-authored-by: Kumar Aditya <kumaraditya@python.org>
* [3.13] gh-116938: Fix `dict.update` docstring and remove erraneous full stop ↵Miss Islington (bot)2024-10-291-2/+2
| | | | | | | | | from `dict` documentation (GH-125421) (#126150) gh-116938: Fix `dict.update` docstring and remove erraneous full stop from `dict` documentation (GH-125421) (cherry picked from commit 5527c4051c0b58218ce69044f92b45f1d66ed43f) Co-authored-by: Prometheus3375 <35541026+Prometheus3375@users.noreply.github.com> Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
* [3.13] gh-125608: Trigger dictionary watchers when inline values change ↵Miss Islington (bot)2024-10-251-6/+15
| | | | | | | | | | (GH-125611) (GH-125982) Dictionary watchers on an object's attributes dictionary (`object.__dict__`) were not triggered when the managed dictionary used the object's inline values. (cherry picked from commit 5989eb74463c26780632f17f221d6bf4c9372a01) Co-authored-by: Sam Gross <colesbury@gmail.com>
* [3.13] gh-124642: Dictionaries aren't marking objects as weakref'd ↵Miss Islington (bot)2024-09-301-3/+3
| | | | | | | | | | (GH-124643) (#124798) gh-124642: Dictionaries aren't marking objects as weakref'd (GH-124643) Dictionaries aren't marking objects as weakref'd (cherry picked from commit 077e7ef6a0abbf9e04b9aa11b4f621031004c31f) Co-authored-by: Dino Viehland <dinoviehland@meta.com>
* [3.13] GH-124547: Clear instance dictionary if memory error occurs during ↵Miss Islington (bot)2024-09-271-2/+9
| | | | | | | | object dealloc (GH-124627) (#124714) GH-124547: Clear instance dictionary if memory error occurs during object dealloc (GH-124627) (cherry picked from commit 0e21cc6cf820679439d72e3ebd06227ee2a085f9) Co-authored-by: Mark Shannon <mark@hotpy.org>
* [3.13] gh-123083: Fix a potential use-after-free in ``STORE_ATTR_WITH… ↵Donghee Na2024-08-221-0/+2
| | | | | | (#123235) [3.13] gh-123083: Fix a potential use-after-free in ``STORE_ATTR_WITH_HINT`` (gh-123092) (cherry picked from commit 297f2e093ec95800ae2184330b8408c875523467)
* [3.13] gh-120974: Make asyncio `swap_current_task` safe in free-threaded ↵Miss Islington (bot)2024-08-021-16/+38
| | | | | | | | | | build (GH-122317) (#122612) gh-120974: Make asyncio `swap_current_task` safe in free-threaded build (GH-122317) * gh-120974: Make asyncio `swap_current_task` safe in free-threaded build (cherry picked from commit b5e6fb39a246bf7ee470d58632cdf588bb9d0298) Co-authored-by: Sam Gross <colesbury@gmail.com>
* [3.13] gh-122208: Don't delivery PyDict_EVENT_ADDED until it can't fail ↵Dino Viehland2024-07-301-11/+8
| | | | | (#122326) Don't delivery PyDict_EVENT_ADDED until it can't fail
* [3.13] gh-120974: Make _asyncio._leave_task atomic in the free-threaded ↵Miss Islington (bot)2024-07-231-15/+15
| | | | | | | | | | | | | build (GH-122139) (#122186) gh-120974: Make _asyncio._leave_task atomic in the free-threaded build (GH-122139) * gh-120974: Make _asyncio._leave_task atomic in the free-threaded build Update `_PyDict_DelItemIf` to allow for an argument to be passed to the predicate. (cherry picked from commit a15feded71dd47202db169613effdafc468a8cf3) Co-authored-by: Sam Gross <colesbury@gmail.com>
* [3.13] gh-121266: Remove Py_ALWAYS_INLINE in dictobject.c (GH-121493) (#122095)Miss Islington (bot)2024-07-211-3/+3
| | | | | | | | | | gh-121266: Remove Py_ALWAYS_INLINE in dictobject.c (GH-121493) compare_unicode_generic(), compare_unicode_unicode() and compare_generic() are callbacks used by do_lookup(). When enabling assertions, it's not possible to inline these functions. (cherry picked from commit c5a6b9afd82cad3f6abd9dc71cd5fdd5781a53f5) Co-authored-by: Victor Stinner <vstinner@python.org>
* [3.13] gh-121266: Change dict check_lookup() return type to int (GH-121581) ↵Miss Islington (bot)2024-07-201-11/+11
| | | | | | | | (#121949) gh-121266: Change dict check_lookup() return type to int (GH-121581) (cherry picked from commit 51da3dfbf3782ad678624e720ec3fc8dfff27d16) Co-authored-by: Victor Stinner <vstinner@python.org>
* [3.13] gh-121860: Fix crash when materializing managed dict (GH-121866) ↵Miss Islington (bot)2024-07-161-5/+12
| | | | | | | | | (#121867) The object's inline values may be marked invalid if the materialized dict was already initialized and then deleted. (cherry picked from commit 162b41f57757c1df40eb377985e2e877fb0f0ea3) Co-authored-by: Sam Gross <colesbury@gmail.com>
* [3.13] gh-120198: Stop the world when setting __class__ on free-threaded ↵Ken Jin2024-07-121-5/+10
| | | | | build (#121591) (cherry-picked from commit 3bfc9c8)
* [3.13] gh-117657: Fix TSan race in _PyDict_CheckConsistency (GH-121551) ↵Miss Islington (bot)2024-07-101-9/+15
| | | | | | | | | (#121590) The only remaining race in dictobject.c was in _PyDict_CheckConsistency when the dictionary has shared keys. (cherry picked from commit 3ec719fabf936ea7a012a76445b860759155de86) Co-authored-by: Sam Gross <colesbury@gmail.com>
* [3.13] gh-112075: Fix dict thread safety issues (GH-119288) (#121545)Miss Islington (bot)2024-07-091-25/+41
| | | | | (cherry picked from commit f0ed1863bd7a0b9d021fb59e156663a7ec553f0e) Co-authored-by: Germán Méndez Bravo <kronuz@fb.com>
* [3.13] gh-117657: Fix data races reported by TSAN in some set methods ↵Miss Islington (bot)2024-07-011-84/+51
| | | | | | | | | | | (GH-120914) (#121240) Refactor the fast Unicode hash check into `_PyObject_HashFast` and use relaxed atomic loads in the free-threaded build. After this change, the TSAN doesn't report data races for this method. (cherry picked from commit 294e72496439da984cb8dba9100d3613c8cc8a6d) Co-authored-by: AN Long <aisk@users.noreply.github.com>
* [3.13] gh-120858: PyDict_Next should not lock the dict (GH-120859) (#120964)Miss Islington (bot)2024-06-241-7/+1
| | | | | | | | | | | | PyDict_Next no longer locks the dictionary in the free-threaded build. Locking around individual PyDict_Next calls is not sufficient because the function returns borrowed references and because it allows concurrent modifications during the iteraiton loop. The internal locking also interferes with correct external synchronization because it may suspend outer critical sections created by the caller. (cherry picked from commit 375b723d5873f948696c7e85a97f4778d9e00ff0) Co-authored-by: Sam Gross <colesbury@gmail.com>
* [3.13] gh-113993: Allow interned strings to be mortal, and fix related ↵Petr Viktorin2024-06-241-1/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | issues (GH-120520) (GH-120945) * Add an InternalDocs file describing how interning should work and how to use it. * Add internal functions to *explicitly* request what kind of interning is done: - `_PyUnicode_InternMortal` - `_PyUnicode_InternImmortal` - `_PyUnicode_InternStatic` * Switch uses of `PyUnicode_InternInPlace` to those. * Disallow using `_Py_SetImmortal` on strings directly. You should use `_PyUnicode_InternImmortal` instead: - Strings should be interned before immortalization, otherwise you're possibly interning a immortalizing copy. - `_Py_SetImmortal` doesn't handle the `SSTATE_INTERNED_MORTAL` to `SSTATE_INTERNED_IMMORTAL` update, and those flags can't be changed in backports, as they are now part of public API and version-specific ABI. * Add private `_only_immortal` argument for `sys.getunicodeinternedsize`, used in refleak test machinery. * Make sure the statically allocated string singletons are unique. This means these sets are now disjoint: - `_Py_ID` - `_Py_STR` (including the empty string) - one-character latin-1 singletons Now, when you intern a singleton, that exact singleton will be interned. * Add a `_Py_LATIN1_CHR` macro, use it instead of `_Py_ID`/`_Py_STR` for one-character latin-1 singletons everywhere (including Clinic). * Intern `_Py_STR` singletons at startup. * For free-threaded builds, intern `_Py_LATIN1_CHR` singletons at startup. * Beef up the tests. Cover internal details (marked with `@cpython_only`). * Add lots of assertions Co-authored-by: Eric Snow <ericsnowcurrently@gmail.com>
* [3.13] gh-119344: Make critical section API public (GH-119353) (#120856)Sam Gross2024-06-211-1/+1
| | | | | | | | | | | This makes the following macros public as part of the non-limited C-API for locking a single object or two objects at once. * `Py_BEGIN_CRITICAL_SECTION(op)` / `Py_END_CRITICAL_SECTION()` * `Py_BEGIN_CRITICAL_SECTION2(a, b)` / `Py_END_CRITICAL_SECTION2()` The supporting functions and structs used by the macros are also exposed for cases where C macros are not available. (cherry picked from commit 8f17d69b7bc906e8407095317842cc0fd52cd84a)
* [3.13] gh-117657: Fix missing atomic in dict_resize (GH-119312) (#119417)Miss Islington (bot)2024-05-221-1/+1
| | | | | | | | gh-117657: Fix missing atomic in dict_resize (GH-119312) Fix missing atomic in dict_resize (cherry picked from commit 2b3fb767bea1f96c9e0523f6cc341b40f0fa1ca1) Co-authored-by: Dino Viehland <dinoviehland@meta.com>
* [3.13] gh-118849: Fix "code will never be executed" warning in ↵Miss Islington (bot)2024-05-091-1/+1
| | | | | | | | `dictobject.c` (GH-118850) (#118859) gh-118849: Fix "code will never be executed" warning in `dictobject.c` (GH-118850) (cherry picked from commit 82abe75e77129bebb3c13d807e8040f6924194f6) Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
* gh-112075: Fix race in constructing dict for instance (#118499)Dino Viehland2024-05-061-69/+71
|
* gh-118362: Fix thread safety around lookups from the type cache in the face ↵Dino Viehland2024-05-061-11/+39
| | | | | | | of concurrent mutators (#118454) Add _PyType_LookupRef and use incref before setting attribute on type Makes setting an attribute on a class and signaling type modified atomic Avoid adding re-entrancy exposing the type cache in an inconsistent state by decrefing after type is updated
* gh-118331: Handle errors in _PyObject_SetManagedDict (#118334)Sam Gross2024-04-291-11/+18
| | | | | When detaching a dict, the `copy_values` call may fail due to out-of-memory errors. This can be triggered by test_no_memory in test_repl.
* gh-112075: _Py_dict_lookup needs to lock shared keys (#117528)Dino Viehland2024-04-251-127/+158
| | | | | Lock shared keys in `Py_dict_lookup` and use thread-safe lookup in `insertdict` Co-authored-by: Sam Gross <colesbury@gmail.com>
* gh-117657: Fixes a few small TSAN issues in dictobject (#118200)Dino Viehland2024-04-251-11/+14
| | | | Fixup TSAN errors for dict
* gh-112075: Make instance attributes stored in inline "dict" thread safe ↵Dino Viehland2024-04-221-70/+315
| | | | | (#114742) Make instance attributes stored in inline "dict" thread safe on free-threaded builds
* Fix a typo in dictobject.c documentation (#117515)Noah Kim2024-04-191-1/+1
|
* gh-117657: use relaxed loads for checking dict keys immortality (#118067)Dino Viehland2024-04-191-2/+2
| | | Use relaxed load to check if dictkeys are immortal
* gh-112069: Add _PySet_NextEntryRef to be thread-safe. (gh-117990)Donghee Na2024-04-181-2/+3
|
* GH-117750: When clearing object's dict, clear inline values but leave dict ↵Mark Shannon2024-04-151-15/+18
| | | | attached (GH-117808)
* gh-117826: Remove lookdict_index from delitemif_lock_held (gh-117869)Donghee Na2024-04-151-5/+2
|
* gh-117439: Make refleak checking thread-safe without the GIL (#117469)Sam Gross2024-04-081-5/+5
| | | | | This keeps track of the per-thread total reference count operations in PyThreadState in the free-threaded builds. The count is merged into the interpreter's total when the thread exits.
* gh-111926: Make weakrefs thread-safe in free-threaded builds (#117168)mpage2024-04-081-4/+4
| | | | | | | | | Most mutable data is protected by a striped lock that is keyed on the referenced object's address. The weakref's hash is protected using the weakref's per-object lock. Note that this only affects free-threaded builds. Apart from some minor refactoring, the added code is all either gated by `ifdef`s or is a no-op (e.g. `Py_BEGIN_CRITICAL_SECTION`).
* gh-112075: Make _PyDict_LoadGlobal thread safe (#117529)Dino Viehland2024-04-041-25/+17
| | | Make _PyDict_LoadGlobal threadsafe
* GH-115776: Embed the values array into the object, for "normal" Python ↵Mark Shannon2024-04-021-227/+237
| | | | objects. (GH-116115)
* gh-112075: Enable freeing with qsbr and fallback to lock on key changed ↵Dino Viehland2024-03-051-6/+3
| | | | (GH-116336)
* gh-116029: Fix unused function warning on macOS (#116340)Sam Gross2024-03-051-2/+2
|
* gh-115941: fixes in dictobject.c doc block(#116196)Humbulani2024-03-011-1/+2
|
* gh-112075: Avoid locking shared keys on every assignment (#116087)Dino Viehland2024-02-291-12/+20
|
* gh-112075: Use relaxed stores for places where we may race with when reading ↵Dino Viehland2024-02-281-18/+25
| | | | lock-free (#115786)