Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 10 additions & 0 deletions 10 Lib/test/test_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -1815,6 +1815,16 @@ class FrozenDictSubclass2(frozendict):
self.assertEqual(fd, frozendict(a=None, b=None, c=None))
self.assertEqual(type(fd), FrozenDictSubclass2)

# Dict subclass which overrides the constructor
class DictSubclass(dict):
def __new__(self):
return created

fd = DictSubclass.fromkeys("abc")
self.assertEqual(fd, frozendict(x=1, a=None, b=None, c=None))
self.assertEqual(type(fd), DictSubclass)
self.assertEqual(created, frozendict(x=1))


if __name__ == "__main__":
unittest.main()
16 changes: 10 additions & 6 deletions 16 Objects/dictobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ As a consequence of this, split keys have a maximum size of 16.
// Forward declarations
static PyObject* frozendict_new(PyTypeObject *type, PyObject *args,
PyObject *kwds);
static PyObject* dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
static int dict_merge(PyObject *a, PyObject *b, int override);


Expand Down Expand Up @@ -3305,15 +3306,18 @@ _PyDict_FromKeys(PyObject *cls, PyObject *iterable, PyObject *value)
return NULL;
}

// If cls is a frozendict subclass with overridden constructor,
// If cls is a dict or frozendict subclass with overridden constructor,
// copy the frozendict.
PyTypeObject *cls_type = _PyType_CAST(cls);
if (PyFrozenDict_Check(d)
&& PyObject_IsSubclass(cls, (PyObject*)&PyFrozenDict_Type)
&& cls_type->tp_new != frozendict_new)
{
if (PyFrozenDict_Check(d) && cls_type->tp_new != frozendict_new) {
// Subclass-friendly copy
PyObject *copy = frozendict_new(cls_type, NULL, NULL);
PyObject *copy;
if (PyObject_IsSubclass(cls, (PyObject*)&PyFrozenDict_Type)) {
copy = frozendict_new(cls_type, NULL, NULL);
}
else {
copy = dict_new(cls_type, NULL, NULL);
}
if (copy == NULL) {
Py_DECREF(d);
return NULL;
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.