Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
1637c04
[WIP] PEP 814: Add built-in frozendict type
vstinner Nov 2, 2025
49aca48
Update Tools/build/generate_token.py for token.py
vstinner Nov 13, 2025
3299299
Try to fix build on Ubuntu and macOS
vstinner Nov 13, 2025
18ce520
Fix make check-c-globals
vstinner Nov 13, 2025
1a72eec
Add PyFrozenDict_Type to static_types
vstinner Nov 13, 2025
9d6ca58
Update Doc/library/stdtypes.rst
vstinner Nov 13, 2025
439c98c
Replace PyDict_Check() with _PyAnyDict_Check()
vstinner Nov 13, 2025
4a1a504
copy: support frozendict
vstinner Nov 13, 2025
302767b
Fix frozendict.__reduce_ex__()
vstinner Nov 13, 2025
07b3510
exec(): accept frozendict for globals
vstinner Nov 13, 2025
04f66ad
Make PyAnyDict_Check() public
vstinner Nov 13, 2025
6fca6d1
Optimize frozendict.copy()
vstinner Nov 14, 2025
6727967
Fix frozendict merge ("|=" operator); add tests
vstinner Nov 14, 2025
d231cdb
copy: use _copy_atomic_types instead; add tests
vstinner Nov 14, 2025
3a07c46
frozendict
vstinner Nov 14, 2025
bc25bb2
pprint supports frozendict
vstinner Nov 14, 2025
0acff5e
change json.tool._group_to_theme_color formatting
vstinner Nov 14, 2025
e90156e
json: use PyAnyDict_Check()
vstinner Nov 14, 2025
fe7e7f5
_pickle: fix refleak
vstinner Nov 14, 2025
4c5c8d2
frozendict_hash: use atomic load/store
vstinner Nov 14, 2025
0a57448
Update Lib/pydoc_data/topics.py
vstinner Nov 14, 2025
fc66056
Fix test_copy: remove duplicated test
vstinner Nov 17, 2025
c1f5055
frozendict | {} returns the same frozendict
vstinner Dec 12, 2025
6ff9c67
Fix frozendict_or()
vstinner Dec 12, 2025
9a64439
Moar frozendict|frozendict optimizations
vstinner Dec 12, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Lib/test/test_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -1692,6 +1692,9 @@ def test_merge(self):
frozendict({'x': 1, 'y': 2}))
self.assertEqual(frozendict(x=1, y=2) | frozendict(y=5),
frozendict({'x': 1, 'y': 5}))
fd = frozendict(x=1, y=2)
self.assertIs(fd | frozendict(), fd)
self.assertIs(fd | {}, fd)

def test_update(self):
# test "a |= b" operator
Expand Down
31 changes: 24 additions & 7 deletions Objects/dictobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,11 @@ load_keys_nentries(PyDictObject *mp)

#endif

#define _PyAnyDict_CAST(op) \
(assert(PyAnyDict_Check(op)), _Py_CAST(PyDictObject*, op))

#define GET_USED(ep) FT_ATOMIC_LOAD_SSIZE_RELAXED((ep)->ma_used)

#define STORE_KEY(ep, key) FT_ATOMIC_STORE_PTR_RELEASE((ep)->me_key, key)
#define STORE_VALUE(ep, value) FT_ATOMIC_STORE_PTR_RELEASE((ep)->me_value, value)
#define STORE_SPLIT_VALUE(mp, idx, value) FT_ATOMIC_STORE_PTR_RELEASE(mp->ma_values->values[idx], value)
Expand Down Expand Up @@ -3476,7 +3481,7 @@ dict_repr(PyObject *self)
static Py_ssize_t
dict_length(PyObject *self)
{
return FT_ATOMIC_LOAD_SSIZE_RELAXED(((PyDictObject *)self)->ma_used);
return GET_USED(_PyAnyDict_CAST(self));
}

static PyObject *
Expand Down Expand Up @@ -4250,7 +4255,7 @@ PyDict_Size(PyObject *mp)
PyErr_BadInternalCall();
return -1;
}
return FT_ATOMIC_LOAD_SSIZE_RELAXED(((PyDictObject *)mp)->ma_used);
return GET_USED((PyDictObject *)mp);
}

/* Return 1 if dicts equal, 0 if not, -1 if error.
Expand Down Expand Up @@ -4794,6 +4799,18 @@ dict_or(PyObject *self, PyObject *other)
return new;
}

static PyObject *
frozendict_or(PyObject *self, PyObject *other)
{
if (PyAnyDict_CheckExact(self) || PyAnyDict_CheckExact(other)) {
if (GET_USED((PyDictObject *)other) == 0) {
return Py_NewRef(self);
}
}
Comment thread
vstinner marked this conversation as resolved.
return dict_or(self, other);
}


static PyObject *
dict_ior(PyObject *self, PyObject *other)
{
Expand Down Expand Up @@ -5153,7 +5170,7 @@ dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
return NULL;
}
di->di_dict = (PyDictObject*)Py_NewRef(dict);
used = FT_ATOMIC_LOAD_SSIZE_RELAXED(dict->ma_used);
used = GET_USED(dict);
di->di_used = used;
di->len = used;
if (itertype == &PyDictRevIterKey_Type ||
Expand Down Expand Up @@ -5209,7 +5226,7 @@ dictiter_len(PyObject *self, PyObject *Py_UNUSED(ignored))
{
dictiterobject *di = (dictiterobject *)self;
Py_ssize_t len = 0;
if (di->di_dict != NULL && di->di_used == FT_ATOMIC_LOAD_SSIZE_RELAXED(di->di_dict->ma_used))
if (di->di_dict != NULL && di->di_used == GET_USED(di->di_dict))
len = FT_ATOMIC_LOAD_SSIZE_RELAXED(di->len);
return PyLong_FromSize_t(len);
}
Expand Down Expand Up @@ -5995,7 +6012,7 @@ dictview_len(PyObject *self)
_PyDictViewObject *dv = (_PyDictViewObject *)self;
Py_ssize_t len = 0;
if (dv->dv_dict != NULL)
len = FT_ATOMIC_LOAD_SSIZE_RELAXED(dv->dv_dict->ma_used);
len = GET_USED(dv->dv_dict);
return len;
}

Expand Down Expand Up @@ -7255,7 +7272,7 @@ _PyObject_IsInstanceDictEmpty(PyObject *obj)
if (dict == NULL) {
return 1;
}
return FT_ATOMIC_LOAD_SSIZE_RELAXED(((PyDictObject *)dict)->ma_used) == 0;
return GET_USED((PyDictObject *)dict) == 0;
}

int
Expand Down Expand Up @@ -7833,7 +7850,7 @@ _PyObject_InlineValuesConsistencyCheck(PyObject *obj)
// --- frozendict implementation ---------------------------------------------

static PyNumberMethods frozendict_as_number = {
.nb_or = dict_or,
.nb_or = frozendict_or,
};

static PyMappingMethods frozendict_as_mapping = {
Expand Down