summaryrefslogtreecommitdiffstats
path: root/Objects/dictobject.c
Commit message (Collapse)AuthorAgeFilesLines
* [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)
* gh-112075: Remove compiler warning from apple clang (gh-115855)Donghee Na2024-02-281-2/+2
|
* gh-112075: Iterating a dict shouldn't require locks (#115108)Dino Viehland2024-02-221-60/+264
| | | Makes iteration of a dict be lock free for the forward iteration case.
* gh-112075: Accessing a single element should optimistically avoid locking ↵Dino Viehland2024-02-211-158/+492
| | | | | (#115109) Makes accessing a single element thread safe and typically lock free
* gh-112075: Make PyDictKeysObject thread-safe (#114741)Dino Viehland2024-02-211-92/+198
| | | Adds locking for shared PyDictKeysObject's for dictionaries
* gh-111968: Split _Py_dictkeys_freelist out of _Py_dict_freelist (gh-115505)Donghee Na2024-02-161-20/+31
|
* gh-111968: Rename freelist related struct names to Eric's suggestion (gh-115329)Donghee Na2024-02-141-31/+20
|
* gh-110481: Implement inter-thread queue for biased reference counting (#114824)Sam Gross2024-02-091-1/+15
| | | | | | | | | Biased reference counting maintains two refcount fields in each object: `ob_ref_local` and `ob_ref_shared`. The true refcount is the sum of these two fields. In some cases, when refcounting operations are split across threads, the ob_ref_shared field can be negative (although the total refcount must be at least zero). In this case, the thread that decremented the refcount requests that the owning thread give up ownership and merge the refcount fields.
* gh-112075: Add critical sections for most dict APIs (#114508)Dino Viehland2024-02-061-236/+630
| | | | | | | | | Starts adding thread safety to dict objects. Use @critical_section for APIs which are exposed via argument clinic and don't directly correlate with a public C API which needs to acquire the lock Use a _lock_held suffix for keeping changes to complicated functions simple and just wrapping them with a critical section Acquire and release the lock in an existing function where it won't be overly disruptive to the existing logic
* gh-112066: Add `PyDict_SetDefaultRef` function. (#112123)Sam Gross2024-02-061-20/+71
| | | | | | | The `PyDict_SetDefaultRef` function is similar to `PyDict_SetDefault`, but returns a strong reference through the optional `**result` pointer instead of a borrowed reference. Co-authored-by: Petr Viktorin <encukou@gmail.com>
* GH-113710: Add a "globals to constants" pass (GH-114592)Mark Shannon2024-02-021-1/+2
| | | Converts specializations of `LOAD_GLOBAL` into constants during tier 2 optimization.
* gh-111968: Use per-thread freelists for dict in free-threading (gh-114323)Donghee Na2024-02-011-49/+39
|
* gh-112075: refactor dictionary lookup functions for better re-usability ↵Dino Viehland2024-01-301-97/+95
| | | | | (#114629) Refactor dict lookup functions to use force inline helpers
* gh-112075: Use PyMem_* for allocating dict keys objects (#114543)Dino Viehland2024-01-291-43/+23
| | | Use PyMem_* for keys allocation
* gh-112075: Adapt more dict methods to Argument Clinic (#114256)Dino Viehland2024-01-231-80/+82
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * Move more dict objects to argument clinic * Improve doc strings * More doc string improvements * Update Objects/dictobject.c Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com> * Update Objects/dictobject.c Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com> * Update Objects/dictobject.c Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com> * Update Objects/dictobject.c Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com> * Update Objects/dictobject.c Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com> * Update Objects/dictobject.c Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com> * Update Objects/dictobject.c Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com> --------- Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
* gh-111178: Avoid calling functions from incompatible pointer types in ↵Christopher Chavez2024-01-021-100/+126
| | | | | dictobject.c (#112892) Fix undefined behavior warnings (UBSan -fsanitize=function).
* gh-112026: Restore removed _PyDict_GetItemStringWithError() (#112119)Victor Stinner2023-11-151-0/+14
| | | | Restore the removed _PyDict_GetItemStringWithError() function. It is used by numpy.
* gh-112026: Restore removed private C API (#112115)Victor Stinner2023-11-151-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Restore removed private C API functions, macros and structures which have no simple replacement for now: * _PyDict_GetItem_KnownHash() * _PyDict_NewPresized() * _PyHASH_BITS * _PyHASH_IMAG * _PyHASH_INF * _PyHASH_MODULUS * _PyHASH_MULTIPLIER * _PyLong_Copy() * _PyLong_FromDigits() * _PyLong_New() * _PyLong_Sign() * _PyObject_CallMethodId() * _PyObject_CallMethodNoArgs() * _PyObject_CallMethodOneArg() * _PyObject_CallOneArg() * _PyObject_EXTRA_INIT * _PyObject_FastCallDict() * _PyObject_GetAttrId() * _PyObject_Vectorcall() * _PyObject_VectorcallMethod() * _PyStack_AsDict() * _PyThread_CurrentFrames() * _PyUnicodeWriter structure * _PyUnicodeWriter_Dealloc() * _PyUnicodeWriter_Finish() * _PyUnicodeWriter_Init() * _PyUnicodeWriter_Prepare() * _PyUnicodeWriter_PrepareKind() * _PyUnicodeWriter_WriteASCIIString() * _PyUnicodeWriter_WriteChar() * _PyUnicodeWriter_WriteLatin1String() * _PyUnicodeWriter_WriteStr() * _PyUnicodeWriter_WriteSubstring() * _PyUnicode_AsString() * _PyUnicode_FromId() * _PyVectorcall_Function() * _Py_HashDouble() * _Py_HashPointer() * _Py_IDENTIFIER() * _Py_c_abs() * _Py_c_diff() * _Py_c_neg() * _Py_c_pow() * _Py_c_prod() * _Py_c_quot() * _Py_c_sum() * _Py_static_string() * _Py_static_string_init()
* gh-111262: Add PyDict_Pop() function (#112028)Victor Stinner2023-11-141-32/+87
| | | | | | | _PyDict_Pop_KnownHash(): remove the default value and the return type becomes an int. Co-authored-by: Stefan Behnel <stefan_ml@behnel.de> Co-authored-by: Antoine Pitrou <pitrou@free.fr>
* gh-106672: C API: Report indiscriminately ignored errors (GH-106674)Serhiy Storchaka2023-11-071-5/+21
| | | | | Functions which indiscriminately ignore all errors now report them as unraisable errors.
* gh-108082: Use PyErr_FormatUnraisable() (GH-111580)Serhiy Storchaka2023-11-021-7/+2
| | | | | | Replace most of calls of _PyErr_WriteUnraisableMsg() and some calls of PyErr_WriteUnraisable(NULL) with PyErr_FormatUnraisable(). Co-authored-by: Victor Stinner <vstinner@python.org>
* gh-107073: Make PyObject_VisitManagedDict() public (#108763)Victor Stinner2023-10-021-2/+2
| | | | | | | | Make PyObject_VisitManagedDict() and PyObject_ClearManagedDict() functions public in Python 3.13 C API. * Rename _PyObject_VisitManagedDict() to PyObject_VisitManagedDict(). * Rename _PyObject_ClearManagedDict() to PyObject_ClearManagedDict(). * Document these functions.
* gh-108511: Add C API functions which do not silently ignore errors (GH-109025)Serhiy Storchaka2023-09-171-4/+3
| | | | | | | | | Add the following functions: * PyObject_HasAttrWithError() * PyObject_HasAttrStringWithError() * PyMapping_HasKeyWithError() * PyMapping_HasKeyStringWithError()
* Add missing `PyDoc_STR` calls (#109393)Nikita Sobolev2023-09-151-1/+1
| | | | | | | | | In files: * Modules/_ctypes/cfield.c * Modules/_struct.c * Objects/dictobject.c * Objects/typevarobject.c * Objects/unionobject.c
* gh-106320: Remove private _PyEval function (#108433)Victor Stinner2023-08-241-0/+1
| | | | | | | | | | | | | | Move private _PyEval functions to the internal C API (pycore_ceval.h): * _PyEval_GetBuiltin() * _PyEval_GetBuiltinId() * _PyEval_GetSwitchInterval() * _PyEval_MakePendingCalls() * _PyEval_SetProfile() * _PyEval_SetSwitchInterval() * _PyEval_SetTrace() No longer export most of these functions.
* gh-108308: Remove _PyDict_GetItemStringWithError() function (#108426)Victor Stinner2023-08-241-13/+0
| | | | | | | | Remove the internal _PyDict_GetItemStringWithError() function. It can now be replaced with the new public PyDict_ContainsString() and PyDict_GetItemStringRef() functions. getargs.c now now uses a strong reference for current_arg. find_keyword() returns a strong reference.