Skip to content

Commit 2ff7055

Browse files
committed
Add PyFrozenDict_New(iterable)
1 parent 06927a0 commit 2ff7055

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

Include/cpython/dictobject.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,6 @@ PyAPI_FUNC(int) PyDict_ClearWatcher(int watcher_id);
113113
// Mark given dictionary as "watched" (callback will be called if it is modified)
114114
PyAPI_FUNC(int) PyDict_Watch(int watcher_id, PyObject* dict);
115115
PyAPI_FUNC(int) PyDict_Unwatch(int watcher_id, PyObject* dict);
116+
117+
// Create a frozendict. Create an empty dictionary if iterable is NULL.
118+
PyAPI_FUNC(PyObject*) PyFrozenDict_New(PyObject *iterable);

Objects/dictobject.c

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ As a consequence of this, split keys have a maximum size of 16.
138138
// Forward declarations
139139
static PyObject* frozendict_new(PyTypeObject *type, PyObject *args,
140140
PyObject *kwds);
141-
static PyObject* _PyFrozenDict_New(void);
142141

143142

144143
/*[clinic input]
@@ -4135,7 +4134,7 @@ copy_lock_held(PyObject *o)
41354134
if (mp->ma_used == 0) {
41364135
/* The dict is empty; just return a new dict. */
41374136
if (frozendict) {
4138-
return _PyFrozenDict_New();
4137+
return PyFrozenDict_New(NULL);
41394138
}
41404139
else {
41414140
return PyDict_New();
@@ -4211,7 +4210,7 @@ copy_lock_held(PyObject *o)
42114210
}
42124211

42134212
if (frozendict) {
4214-
copy = _PyFrozenDict_New();
4213+
copy = PyFrozenDict_New(NULL);
42154214
}
42164215
else {
42174216
copy = PyDict_New();
@@ -7911,15 +7910,37 @@ frozendict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
79117910
}
79127911
PyFrozenDictObject *self = _PyFrozenDictObject_CAST(d);
79137912
self->ma_hash = -1;
7913+
7914+
if (args != NULL) {
7915+
if (dict_update_common(d, args, kwds, "frozendict") < 0) {
7916+
Py_DECREF(d);
7917+
return NULL;
7918+
}
7919+
}
7920+
else {
7921+
assert(kwds == NULL);
7922+
}
7923+
79147924
return d;
79157925
}
79167926

79177927

7918-
static PyObject*
7919-
_PyFrozenDict_New(void)
7928+
PyObject*
7929+
PyFrozenDict_New(PyObject *iterable)
79207930
{
7921-
PyObject *args = Py_GetConstant(Py_CONSTANT_EMPTY_TUPLE);
7922-
return frozendict_new(&PyFrozenDict_Type, args, NULL);
7931+
if (iterable != NULL) {
7932+
PyObject *args = PyTuple_Pack(1, iterable);
7933+
if (args == NULL) {
7934+
return NULL;
7935+
}
7936+
PyObject *frozendict = frozendict_new(&PyFrozenDict_Type, args, NULL);
7937+
Py_DECREF(args);
7938+
return frozendict;
7939+
}
7940+
else {
7941+
PyObject *args = Py_GetConstantBorrowed(Py_CONSTANT_EMPTY_TUPLE);
7942+
return frozendict_new(&PyFrozenDict_Type, args, NULL);
7943+
}
79237944
}
79247945

79257946

0 commit comments

Comments
 (0)