Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
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
11 changes: 11 additions & 0 deletions Lib/test/picklecommon.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,17 @@ class MyFrozenSet(frozenset):
MyStr, MyUnicode,
MyTuple, MyList, MyDict, MySet, MyFrozenSet]

try:
frozendict
except NameError:
# Python 3.14 and older
pass
else:
class MyFrozenDict(dict):
sample = frozendict({"a": 1, "b": 2})
myclasses.append(MyFrozenDict)


# For test_newobj_overridden_new
class MyIntWithNew(int):
def __new__(cls, value):
Expand Down
9 changes: 8 additions & 1 deletion Lib/test/pickletester.py
Original file line number Diff line number Diff line change
Expand Up @@ -2839,11 +2839,13 @@ def test_recursive_multi(self):
self.assertEqual(list(x[0].attr.keys()), [1])
self.assertIs(x[0].attr[1], x)

def _test_recursive_collection_and_inst(self, factory, oldminproto=None):
def _test_recursive_collection_and_inst(self, factory, oldminproto=None,
minprotocol=0):
if self.py_version < (3, 0):
self.skipTest('"classic" classes are not interoperable with Python 2')
# Mutable object containing a collection containing the original
# object.
protocols = range(minprotocol, pickle.HIGHEST_PROTOCOL + 1)
o = Object()
o.attr = factory([o])
t = type(o.attr)
Expand Down Expand Up @@ -2883,6 +2885,11 @@ def test_recursive_tuple_and_inst(self):
def test_recursive_dict_and_inst(self):
self._test_recursive_collection_and_inst(dict.fromkeys, oldminproto=0)

def test_recursive_frozendict_and_inst(self):
Comment thread
vstinner marked this conversation as resolved.
if self.py_version < (3, 15):
self.skipTest('need frozendict')
self._test_recursive_collection_and_inst(frozendict.fromkeys, minprotocol=2)

def test_recursive_set_and_inst(self):
self._test_recursive_collection_and_inst(set)

Expand Down
9 changes: 9 additions & 0 deletions Lib/test/test_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -1825,6 +1825,15 @@ def __new__(self):
self.assertEqual(type(fd), DictSubclass)
self.assertEqual(created, frozendict(x=1))

def test_pickle(self):
for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
Comment thread
vstinner marked this conversation as resolved.
Outdated
for fd in (
frozendict(),
frozendict(x=1, y=2),
Comment thread
vstinner marked this conversation as resolved.
):
p = pickle.dumps(fd, proto)
self.assertEqual(fd, pickle.loads(p))


if __name__ == "__main__":
unittest.main()
13 changes: 13 additions & 0 deletions Objects/dictobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -7930,6 +7930,18 @@ _PyObject_InlineValuesConsistencyCheck(PyObject *obj)

// --- frozendict implementation ---------------------------------------------

static PyObject *
frozendict_getnewargs(PyObject *op, PyObject *Py_UNUSED(dummy))
{
// Call dict(op): convert 'op' frozendict to a dict
PyObject *arg = PyObject_CallOneArg((PyObject*)&PyDict_Type, op);
if (arg == NULL) {
return NULL;
}
return Py_BuildValue("(N)", arg);
}


static PyNumberMethods frozendict_as_number = {
.nb_or = frozendict_or,
};
Expand All @@ -7951,6 +7963,7 @@ static PyMethodDef frozendict_methods[] = {
DICT_COPY_METHODDEF
DICT___REVERSED___METHODDEF
{"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
{"__getnewargs__", frozendict_getnewargs, METH_NOARGS},
{NULL, NULL} /* sentinel */
};

Expand Down
Loading