[Python-checkins] CVS: python/dist/src/Objects dictobject.c,2.107,2.108

Guido van Rossum gvanrossum@users.sourceforge.net
Fri, 10 Aug 2001 13:28:30 -0700


Update of /cvsroot/python/python/dist/src/Objects
In directory usw-pr-cvs1:/tmp/cvs-serv27392/Objects

Modified Files:
	dictobject.c 
Log Message:
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.


Index: dictobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/dictobject.c,v
retrieving revision 2.107
retrieving revision 2.108
diff -C2 -d -r2.107 -r2.108
*** dictobject.c	2001/08/02 04:15:00	2.107
--- dictobject.c	2001/08/10 20:28:28	2.108
***************
*** 1000,1006 ****
--- 1000,1016 ----
  }
  
+ /* Update unconditionally replaces existing items.
+    Merge has a 3rd argument 'override'; if set, it acts like Update,
+    otherwise it leaves existing items unchanged. */
+ 
  int
  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 int i;
***************
*** 1032,1036 ****
  		for (i = 0; i <= other->ma_mask; i++) {
  			entry = &other->ma_table[i];
! 			if (entry->me_value != NULL) {
  				Py_INCREF(entry->me_key);
  				Py_INCREF(entry->me_value);
--- 1042,1048 ----
  		for (i = 0; i <= other->ma_mask; i++) {
  			entry = &other->ma_table[i];
! 			if (entry->me_value != NULL &&
! 			    (override ||
! 			     PyDict_GetItem(a, entry->me_key) == NULL)) {
  				Py_INCREF(entry->me_key);
  				Py_INCREF(entry->me_value);
***************
*** 1061,1064 ****
--- 1073,1080 ----
  
  		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);
  			if (value == NULL) {
***************
*** 1067,1071 ****
  				return -1;
  			}
! 			status = PyDict_SetItem((PyObject*)mp, key, value);
  			Py_DECREF(key);
  			Py_DECREF(value);
--- 1083,1087 ----
  				return -1;
  			}
! 			status = PyDict_SetItem(a, key, value);
  			Py_DECREF(key);
  			Py_DECREF(value);