Skip to content
Open
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
4 changes: 4 additions & 0 deletions Doc/c-api/dict.rst
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,10 @@ Frozen dictionary objects

Create an empty dictionary if *iterable* is ``NULL``.

.. c:function:: PyObject* PyAnyDict_AsNewDict(PyObject *p)

Create a new dictionary from a :class:`dict` or a :class:`frozendict`.


Ordered dictionaries
^^^^^^^^^^^^^^^^^^^^
Expand Down
3 changes: 3 additions & 0 deletions Include/cpython/dictobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,6 @@ PyAPI_FUNC(int) PyDict_Unwatch(int watcher_id, PyObject* dict);

// Create a frozendict. Create an empty dictionary if iterable is NULL.
PyAPI_FUNC(PyObject*) PyFrozenDict_New(PyObject *iterable);

// Create a new dictionary from a dict or a frozendict.
PyAPI_FUNC(PyObject*) PyAnyDict_AsNewDict(PyObject *o);
3 changes: 0 additions & 3 deletions Include/internal/pycore_dict.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,6 @@ extern void _PyDict_Clear_LockHeld(PyObject *op);
PyAPI_FUNC(void) _PyDict_EnsureSharedOnRead(PyDictObject *mp);
#endif

// Export for '_elementtree' shared extension
PyAPI_FUNC(PyObject*) _PyDict_CopyAsDict(PyObject *op);

#define DKIX_EMPTY (-1)
#define DKIX_DUMMY (-2) /* Used internally */
#define DKIX_ERROR (-3)
Expand Down
13 changes: 13 additions & 0 deletions Lib/test/test_capi/test_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,19 @@ def test_frozendict_new(self):
self.assertEqual(dct, frozendict())
self.assertIs(type(dct), frozendict)

def test_anydict_asnewdict(self):
# Test PyAnyDict_AsNewDict()
anydict_asnewdict = _testcapi.anydict_asnewdict
for dict_type in ANYDICT_TYPES:
dct = dict_type({1: 2})
dct_copy = anydict_asnewdict(dct)
self.assertIs(type(dct_copy), dict)
self.assertEqual(dct_copy, dct)

for test_type in NOT_ANYDICT_TYPES + OTHER_TYPES:
self.assertRaises(SystemError, anydict_asnewdict, test_type())
self.assertRaises(SystemError, anydict_asnewdict, NULL)


if __name__ == "__main__":
unittest.main()
5 changes: 2 additions & 3 deletions Modules/_elementtree.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#endif

#include "Python.h"
#include "pycore_dict.h" // _PyDict_CopyAsDict()
#include "pycore_pyhash.h" // _Py_HashSecret
#include "pycore_weakref.h" // FT_CLEAR_WEAKREFS()

Expand Down Expand Up @@ -390,7 +389,7 @@ get_attrib_from_keywords(PyObject *kwds)
Py_DECREF(attrib);
return NULL;
}
Py_SETREF(attrib, _PyDict_CopyAsDict(attrib));
Py_SETREF(attrib, PyAnyDict_AsNewDict(attrib));
}
else {
attrib = PyDict_New();
Expand Down Expand Up @@ -429,7 +428,7 @@ element_init(PyObject *self, PyObject *args, PyObject *kwds)

if (attrib) {
/* attrib passed as positional arg */
attrib = _PyDict_CopyAsDict(attrib);
attrib = PyAnyDict_AsNewDict(attrib);
if (!attrib)
return -1;
if (kwds) {
Expand Down
9 changes: 9 additions & 0 deletions Modules/_testcapi/dict.c
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,14 @@ frozendict_new(PyObject *self, PyObject *obj)
}


static PyObject*
anydict_asnewdict(PyObject *self, PyObject *obj)
{
NULLABLE(obj);
return PyAnyDict_AsNewDict(obj);
}


static PyMethodDef test_methods[] = {
{"dict_containsstring", dict_containsstring, METH_VARARGS},
{"dict_getitemref", dict_getitemref, METH_VARARGS},
Expand All @@ -311,6 +319,7 @@ static PyMethodDef test_methods[] = {
{"anydict_check", anydict_check, METH_O},
{"anydict_checkexact", anydict_checkexact, METH_O},
{"frozendict_new", frozendict_new, METH_O},
{"anydict_asnewdict", anydict_asnewdict, METH_O},
{NULL},
};

Expand Down
9 changes: 5 additions & 4 deletions Objects/dictobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -4414,12 +4414,13 @@ anydict_copy(PyObject *o)
return res;
}

// Similar to PyDict_Copy(), but accept also frozendict:
// convert frozendict to a new dict.
PyObject*
_PyDict_CopyAsDict(PyObject *o)
PyAnyDict_AsNewDict(PyObject *o)
{
assert(PyAnyDict_Check(o));
if (o == NULL || !PyAnyDict_Check(o)) {
PyErr_BadInternalCall();
return NULL;
}

PyObject *res;
Py_BEGIN_CRITICAL_SECTION(o);
Expand Down
2 changes: 1 addition & 1 deletion Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -4850,7 +4850,7 @@ type_new_get_slots(type_new_ctx *ctx, PyObject *dict)
static PyTypeObject*
type_new_init(type_new_ctx *ctx)
{
PyObject *dict = _PyDict_CopyAsDict(ctx->orig_dict);
PyObject *dict = PyAnyDict_AsNewDict(ctx->orig_dict);
if (dict == NULL) {
goto error;
}
Expand Down
Loading