Coverage Report

Created: 2025-11-30 06:38

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython-install/include/python3.15/cpython/dictobject.h
Line
Count
Source
1
#ifndef Py_CPYTHON_DICTOBJECT_H
2
#  error "this header file must not be included directly"
3
#endif
4
5
typedef struct _dictkeysobject PyDictKeysObject;
6
typedef struct _dictvalues PyDictValues;
7
8
/* The ma_values pointer is NULL for a combined table
9
 * or points to an array of PyObject* for a split table
10
 */
11
typedef struct {
12
    PyObject_HEAD
13
14
    /* Number of items in the dictionary */
15
    Py_ssize_t ma_used;
16
17
    /* This is a private field for CPython's internal use.
18
     * Bits 0-7 are for dict watchers.
19
     * Bits 8-11 are for the watched mutation counter (used by tier2 optimization)
20
     * Bits 12-31 are currently unused
21
     * Bits 32-63 are a unique id in the free threading build (used for per-thread refcounting)
22
     */
23
    uint64_t _ma_watcher_tag;
24
25
    PyDictKeysObject *ma_keys;
26
27
    /* If ma_values is NULL, the table is "combined": keys and values
28
       are stored in ma_keys.
29
30
       If ma_values is not NULL, the table is split:
31
       keys are stored in ma_keys and values are stored in ma_values */
32
    PyDictValues *ma_values;
33
} PyDictObject;
34
35
PyAPI_FUNC(PyObject *) _PyDict_GetItem_KnownHash(PyObject *mp, PyObject *key,
36
                                                 Py_hash_t hash);
37
// PyDict_GetItemStringRef() can be used instead
38
Py_DEPRECATED(3.14) PyAPI_FUNC(PyObject *) _PyDict_GetItemStringWithError(PyObject *, const char *);
39
PyAPI_FUNC(PyObject *) PyDict_SetDefault(
40
    PyObject *mp, PyObject *key, PyObject *defaultobj);
41
42
/* Get the number of items of a dictionary. */
43
0
static inline Py_ssize_t PyDict_GET_SIZE(PyObject *op) {
44
0
    PyDictObject *mp;
45
0
    assert(PyDict_Check(op));
46
0
    mp = _Py_CAST(PyDictObject*, op);
47
0
#ifdef Py_GIL_DISABLED
48
0
    return _Py_atomic_load_ssize_relaxed(&mp->ma_used);
49
0
#else
50
0
    return mp->ma_used;
51
0
#endif
52
0
}
53
#define PyDict_GET_SIZE(op) PyDict_GET_SIZE(_PyObject_CAST(op))
54
55
PyAPI_FUNC(int) PyDict_ContainsString(PyObject *mp, const char *key);
56
57
PyAPI_FUNC(PyObject *) _PyDict_NewPresized(Py_ssize_t minused);
58
59
PyAPI_FUNC(int) PyDict_Pop(PyObject *dict, PyObject *key, PyObject **result);
60
PyAPI_FUNC(int) PyDict_PopString(PyObject *dict, const char *key, PyObject **result);
61
62
// Use PyDict_Pop() instead
63
Py_DEPRECATED(3.14) PyAPI_FUNC(PyObject *) _PyDict_Pop(
64
    PyObject *dict,
65
    PyObject *key,
66
    PyObject *default_value);
67
68
/* Dictionary watchers */
69
70
#define PY_FOREACH_DICT_EVENT(V) \
71
    V(ADDED)                     \
72
    V(MODIFIED)                  \
73
    V(DELETED)                   \
74
    V(CLONED)                    \
75
    V(CLEARED)                   \
76
    V(DEALLOCATED)
77
78
typedef enum {
79
    #define PY_DEF_EVENT(EVENT) PyDict_EVENT_##EVENT,
80
    PY_FOREACH_DICT_EVENT(PY_DEF_EVENT)
81
    #undef PY_DEF_EVENT
82
} PyDict_WatchEvent;
83
84
// Callback to be invoked when a watched dict is cleared, dealloced, or modified.
85
// In clear/dealloc case, key and new_value will be NULL. Otherwise, new_value will be the
86
// new value for key, NULL if key is being deleted.
87
typedef int(*PyDict_WatchCallback)(PyDict_WatchEvent event, PyObject* dict, PyObject* key, PyObject* new_value);
88
89
// Register/unregister a dict-watcher callback
90
PyAPI_FUNC(int) PyDict_AddWatcher(PyDict_WatchCallback callback);
91
PyAPI_FUNC(int) PyDict_ClearWatcher(int watcher_id);
92
93
// Mark given dictionary as "watched" (callback will be called if it is modified)
94
PyAPI_FUNC(int) PyDict_Watch(int watcher_id, PyObject* dict);
95
PyAPI_FUNC(int) PyDict_Unwatch(int watcher_id, PyObject* dict);