[Python-checkins] python/dist/src/Objects dictobject.c,2.140,2.141

rhettinger@users.sourceforge.net rhettinger@users.sourceforge.net
Thu, 06 Mar 2003 15:54:31 -0800


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

Modified Files:
	dictobject.c 
Log Message:
SF patch #693753:  fix for bug 639806: default for dict.pop
(contributed by Michael Stone.)


Index: dictobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/dictobject.c,v
retrieving revision 2.140
retrieving revision 2.141
diff -C2 -d -r2.140 -r2.141
*** dictobject.c	15 Feb 2003 14:45:12 -0000	2.140
--- dictobject.c	6 Mar 2003 23:54:28 -0000	2.141
***************
*** 1545,1555 ****
  
  static PyObject *
! dict_pop(dictobject *mp, PyObject *key)
  {
  	long hash;
  	dictentry *ep;
  	PyObject *old_value, *old_key;
  
  	if (mp->ma_used == 0) {
  		PyErr_SetString(PyExc_KeyError,
  				"pop(): dictionary is empty");
--- 1545,1562 ----
  
  static PyObject *
! dict_pop(dictobject *mp, PyObject *args)
  {
  	long hash;
  	dictentry *ep;
  	PyObject *old_value, *old_key;
+ 	PyObject *key, *deflt = NULL;
  
+ 	if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
+ 		return NULL;
  	if (mp->ma_used == 0) {
+ 		if (deflt) {
+ 			Py_INCREF(deflt);
+ 			return deflt;
+ 		}
  		PyErr_SetString(PyExc_KeyError,
  				"pop(): dictionary is empty");
***************
*** 1564,1567 ****
--- 1571,1578 ----
  	ep = (mp->ma_lookup)(mp, key, hash);
  	if (ep->me_value == NULL) {
+ 		if (deflt) {
+ 			Py_INCREF(deflt);
+ 			return deflt;
+ 		}
  		PyErr_SetObject(PyExc_KeyError, key);
  		return NULL;
***************
*** 1720,1724 ****
  
  PyDoc_STRVAR(pop__doc__,
! "D.pop(k) -> v, remove specified key and return the corresponding value");
  
  PyDoc_STRVAR(popitem__doc__,
--- 1731,1736 ----
  
  PyDoc_STRVAR(pop__doc__,
! "D.pop(k[,d]) -> v, remove specified key and return the corresponding value\n\
! If key is not found, d is returned if given, otherwise KeyError is raised");
  
  PyDoc_STRVAR(popitem__doc__,
***************
*** 1764,1768 ****
  	{"setdefault",  (PyCFunction)dict_setdefault,   METH_VARARGS,
  	 setdefault_doc__},
! 	{"pop",         (PyCFunction)dict_pop,          METH_O,
  	 pop__doc__},
  	{"popitem",	(PyCFunction)dict_popitem,	METH_NOARGS,
--- 1776,1780 ----
  	{"setdefault",  (PyCFunction)dict_setdefault,   METH_VARARGS,
  	 setdefault_doc__},
! 	{"pop",         (PyCFunction)dict_pop,          METH_VARARGS,
  	 pop__doc__},
  	{"popitem",	(PyCFunction)dict_popitem,	METH_NOARGS,