[Python-checkins] python/nondist/sandbox/setobj setobject.c, 1.13, 1.14

rhettinger at users.sourceforge.net rhettinger at users.sourceforge.net
Fri Nov 14 18:20:21 EST 2003


Update of /cvsroot/python/python/nondist/sandbox/setobj
In directory sc8-pr-cvs1:/tmp/cvs-serv8958

Modified Files:
	setobject.c 
Log Message:
Make sure frozen sets only compute the hash once.  Add error checking to __reduce__().

Index: setobject.c
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/setobj/setobject.c,v
retrieving revision 1.13
retrieving revision 1.14
diff -C2 -d -r1.13 -r1.14
*** setobject.c	14 Nov 2003 22:59:27 -0000	1.13
--- setobject.c	14 Nov 2003 23:20:19 -0000	1.14
***************
*** 24,27 ****
--- 24,28 ----
  	PyObject_HEAD
  	PyObject *data;
+ 	long hash;	/* only used by frozenset objects */
  } setobject;
  
***************
*** 72,75 ****
--- 73,77 ----
  	}
  	so->data = data;
+ 	so->hash = -1;
  
  	return (PyObject *)so;
***************
*** 604,607 ****
--- 606,612 ----
  	PyObject *it, *item;
  	long hash = 0;
+ 
+ 	if (so->hash != -1)
+ 		return so->hash;
  		
  	it = PyObject_GetIter(((setobject *)so)->data);
***************
*** 613,618 ****
  		Py_DECREF(item);
  	}
  	Py_DECREF(it);
! 	return hash;	/* XXX Consider caching the hash value to save recomputations */
  }
  
--- 618,624 ----
  		Py_DECREF(item);
  	}
+ 	so->hash = hash;
  	Py_DECREF(it);
! 	return hash;
  }
  
***************
*** 741,751 ****
  set_reduce(setobject *so)
  {
! 	PyObject *keys, *args, *result;
  
  	keys = PyDict_Keys(so->data);
  	args = PyTuple_Pack(1, keys);
  	result = PyTuple_Pack(2, so->ob_type, args);
! 	Py_DECREF(args);
! 	Py_DECREF(keys);
  	return result;
  }
--- 747,762 ----
  set_reduce(setobject *so)
  {
! 	PyObject *keys=NULL, *args=NULL, *result=NULL;
  
  	keys = PyDict_Keys(so->data);
+ 	if (keys == NULL)
+ 		goto done;
  	args = PyTuple_Pack(1, keys);
+ 	if (args == NULL)
+ 		goto done;
  	result = PyTuple_Pack(2, so->ob_type, args);
! done:
! 	Py_XDECREF(args);
! 	Py_XDECREF(keys);
  	return result;
  }





More information about the Python-checkins mailing list