mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Add PyDict_Merge(a, b, override):
PyDict_Merge(a, b, 1) is the same as PyDict_Update(a, b). PyDict_Merge(a, b, 0) does something similar but leaves existing items unchanged.
This commit is contained in:
parent
f4aa684132
commit
05ac6de2d5
2 changed files with 19 additions and 2 deletions
|
@ -98,6 +98,7 @@ extern DL_IMPORT(PyObject *) PyDict_Items(PyObject *mp);
|
||||||
extern DL_IMPORT(int) PyDict_Size(PyObject *mp);
|
extern DL_IMPORT(int) PyDict_Size(PyObject *mp);
|
||||||
extern DL_IMPORT(PyObject *) PyDict_Copy(PyObject *mp);
|
extern DL_IMPORT(PyObject *) PyDict_Copy(PyObject *mp);
|
||||||
extern DL_IMPORT(int) PyDict_Update(PyObject *mp, PyObject *other);
|
extern DL_IMPORT(int) PyDict_Update(PyObject *mp, PyObject *other);
|
||||||
|
extern DL_IMPORT(int) PyDict_Merge(PyObject *mp, PyObject *other, int override);
|
||||||
|
|
||||||
|
|
||||||
extern DL_IMPORT(PyObject *) PyDict_GetItemString(PyObject *dp, char *key);
|
extern DL_IMPORT(PyObject *) PyDict_GetItemString(PyObject *dp, char *key);
|
||||||
|
|
|
@ -999,8 +999,18 @@ dict_update(PyObject *mp, PyObject *args)
|
||||||
return Py_None;
|
return Py_None;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Update unconditionally replaces existing items.
|
||||||
|
Merge has a 3rd argument 'override'; if set, it acts like Update,
|
||||||
|
otherwise it leaves existing items unchanged. */
|
||||||
|
|
||||||
int
|
int
|
||||||
PyDict_Update(PyObject *a, PyObject *b)
|
PyDict_Update(PyObject *a, PyObject *b)
|
||||||
|
{
|
||||||
|
return PyDict_Merge(a, b, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
PyDict_Merge(PyObject *a, PyObject *b, int override)
|
||||||
{
|
{
|
||||||
register PyDictObject *mp, *other;
|
register PyDictObject *mp, *other;
|
||||||
register int i;
|
register int i;
|
||||||
|
@ -1031,7 +1041,9 @@ PyDict_Update(PyObject *a, PyObject *b)
|
||||||
}
|
}
|
||||||
for (i = 0; i <= other->ma_mask; i++) {
|
for (i = 0; i <= other->ma_mask; i++) {
|
||||||
entry = &other->ma_table[i];
|
entry = &other->ma_table[i];
|
||||||
if (entry->me_value != NULL) {
|
if (entry->me_value != NULL &&
|
||||||
|
(override ||
|
||||||
|
PyDict_GetItem(a, entry->me_key) == NULL)) {
|
||||||
Py_INCREF(entry->me_key);
|
Py_INCREF(entry->me_key);
|
||||||
Py_INCREF(entry->me_value);
|
Py_INCREF(entry->me_value);
|
||||||
insertdict(mp, entry->me_key, entry->me_hash,
|
insertdict(mp, entry->me_key, entry->me_hash,
|
||||||
|
@ -1060,13 +1072,17 @@ PyDict_Update(PyObject *a, PyObject *b)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
|
for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
|
||||||
|
if (!override && PyDict_GetItem(a, key) != NULL) {
|
||||||
|
Py_DECREF(key);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
value = PyObject_GetItem(b, key);
|
value = PyObject_GetItem(b, key);
|
||||||
if (value == NULL) {
|
if (value == NULL) {
|
||||||
Py_DECREF(iter);
|
Py_DECREF(iter);
|
||||||
Py_DECREF(key);
|
Py_DECREF(key);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
status = PyDict_SetItem((PyObject*)mp, key, value);
|
status = PyDict_SetItem(a, key, value);
|
||||||
Py_DECREF(key);
|
Py_DECREF(key);
|
||||||
Py_DECREF(value);
|
Py_DECREF(value);
|
||||||
if (status < 0) {
|
if (status < 0) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue