Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 4 additions & 1 deletion Lib/test/picklecommon.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,9 @@ class MyList(list):
class MyDict(dict):
sample = {"a": 1, "b": 2}

class MyFrozenDict(dict):
sample = frozendict({"a": 1, "b": 2})
Comment thread
vstinner marked this conversation as resolved.
Outdated

class MySet(set):
sample = {"a", "b"}

Expand All @@ -261,7 +264,7 @@ class MyFrozenSet(frozenset):
myclasses = [MyInt, MyLong, MyFloat,
MyComplex,
MyStr, MyUnicode,
MyTuple, MyList, MyDict, MySet, MyFrozenSet]
MyTuple, MyList, MyDict, MyFrozenDict, MySet, MyFrozenSet]

# For test_newobj_overridden_new
class MyIntWithNew(int):
Expand Down
16 changes: 15 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,9 @@ 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.
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 Expand Up @@ -3095,6 +3100,15 @@ def test_float_format(self):
# make sure that floats are formatted locale independent with proto 0
self.assertEqual(self.dumps(1.2, 0)[0:3], b'F1.')

def test_frozendict(self):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would not be better to add this test in test_frozendict.py? Together with tests for copy() and deepcopy()?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I moved this test to test_pickle.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Together with tests for copy() and deepcopy()?

Commit dd64e42, which adds frozendict support to the copy module, added frozendict tests to test_copy.

for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
for fd in (
frozendict(),
frozendict(x=1, y=2),
):
p = self.dumps(fd, proto)
self.assert_is_copy(fd, self.loads(p))

def test_reduce(self):
for proto in protocols:
with self.subTest(proto=proto):
Expand Down
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