[Python-checkins] python/dist/src/Objects setobject.c,1.2,1.3

rhettinger at users.sourceforge.net rhettinger at users.sourceforge.net
Mon Nov 17 11:42:35 EST 2003


Update of /cvsroot/python/python/dist/src/Objects
In directory sc8-pr-cvs1:/tmp/cvs-serv10398/Objects
Modified Files:
	setobject.c 
Log Message:
Various fixups (most suggested by Armin Rigo).
Index: setobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/setobject.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** setobject.c	16 Nov 2003 16:36:58 -0000	1.2
--- setobject.c	17 Nov 2003 16:42:32 -0000	1.3
***************
*** 13,17 ****
 
 #define DICT_CONTAINS(d, k) (d->ob_type->tp_as_sequence->sq_contains(d, k))
- #define IS_SET(so)	(so->ob_type == &PySet_Type || so->ob_type == &PyFrozenSet_Type)
 
 /* set object **********************************************************/
--- 13,16 ----
***************
*** 43,48 ****
 			Py_DECREF(data);
 			Py_DECREF(item);
- 			PyErr_SetString(PyExc_TypeError,
- 					"all set entries must be immutable");
 			return NULL;
 } 
--- 42,45 ----
***************
*** 68,72 ****
 
 static PyObject *
! set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
 {
 	PyObject *iterable = NULL;
--- 65,69 ----
 
 static PyObject *
! frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
 {
 	PyObject *iterable = NULL;
***************
*** 77,80 ****
--- 74,85 ----
 }
 
+ static PyObject *
+ set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
+ {
+ 	PyObject *iterable = NULL;
+ 
+ 	return make_new_set(type, NULL);
+ }
+ 
 static void
 set_dealloc(PySetObject *so)
***************
*** 140,143 ****
--- 145,150 ----
 
 	result = (PySetObject *)set_copy(so);
+ 	if (result == NULL)
+ 		return NULL;
 	it = PyObject_GetIter(other);
 	if (it == NULL) {
***************
*** 151,156 ****
 			Py_DECREF(result);
 			Py_DECREF(item);
- 			PyErr_SetString(PyExc_TypeError,
- 					"all set entries must be immutable");
 			return NULL;
 } 
--- 158,161 ----
***************
*** 184,189 ****
 			Py_DECREF(it);
 			Py_DECREF(item);
- 			PyErr_SetString(PyExc_TypeError,
- 					"all set entries must be immutable");
 			return NULL;
 } 
--- 189,192 ----
***************
*** 202,206 ****
 set_or(PySetObject *so, PyObject *other)
 {
! 	if (!IS_SET(so) || !IS_SET(other)) {
 		Py_INCREF(Py_NotImplemented);
 		return Py_NotImplemented;
--- 205,209 ----
 set_or(PySetObject *so, PyObject *other)
 {
! 	if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
 		Py_INCREF(Py_NotImplemented);
 		return Py_NotImplemented;
***************
*** 214,218 ****
 	PyObject *result;
 
! 	if (!IS_SET(other)) {
 		Py_INCREF(Py_NotImplemented);
 		return Py_NotImplemented;
--- 217,221 ----
 	PyObject *result;
 
! 	if (!PyAnySet_Check(other)) {
 		Py_INCREF(Py_NotImplemented);
 		return Py_NotImplemented;
***************
*** 250,255 ****
 				Py_DECREF(result);
 				Py_DECREF(item);
- 				PyErr_SetString(PyExc_TypeError,
- 						"all set entries must be immutable");
 				return NULL;
 			}
--- 253,256 ----
***************
*** 292,297 ****
 				Py_DECREF(it);
 				Py_DECREF(item);
- 				PyErr_SetString(PyExc_TypeError,
- 						"all set entries must be immutable");
 				return NULL;
 			}
--- 293,296 ----
***************
*** 316,320 ****
 set_and(PySetObject *so, PyObject *other)
 {
! 	if (!IS_SET(so) || !IS_SET(other)) {
 		Py_INCREF(Py_NotImplemented);
 		return Py_NotImplemented;
--- 315,319 ----
 set_and(PySetObject *so, PyObject *other)
 {
! 	if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
 		Py_INCREF(Py_NotImplemented);
 		return Py_NotImplemented;
***************
*** 328,332 ****
 	PyObject *result;
 
! 	if (!IS_SET(other)) {
 		Py_INCREF(Py_NotImplemented);
 		return Py_NotImplemented;
--- 327,331 ----
 	PyObject *result;
 
! 	if (!PyAnySet_Check(other)) {
 		Py_INCREF(Py_NotImplemented);
 		return Py_NotImplemented;
***************
*** 417,421 ****
 set_sub(PySetObject *so, PyObject *other)
 {
! 	if (!IS_SET(so) || !IS_SET(other)) {
 		Py_INCREF(Py_NotImplemented);
 		return Py_NotImplemented;
--- 416,420 ----
 set_sub(PySetObject *so, PyObject *other)
 {
! 	if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
 		Py_INCREF(Py_NotImplemented);
 		return Py_NotImplemented;
***************
*** 429,433 ****
 	PyObject *result;
 
! 	if (!IS_SET(other)) {
 		Py_INCREF(Py_NotImplemented);
 		return Py_NotImplemented;
--- 428,432 ----
 	PyObject *result;
 
! 	if (!PyAnySet_Check(other)) {
 		Py_INCREF(Py_NotImplemented);
 		return Py_NotImplemented;
***************
*** 476,481 ****
 				Py_DECREF(result);
 				Py_DECREF(item);
- 				PyErr_SetString(PyExc_TypeError,
- 						"all set entries must be immutable");
 				return NULL;
 			} 
--- 475,478 ----
***************
*** 507,511 ****
 	if (PyDict_Check(other))
 		otherdata = other;
! 	else if (IS_SET(other))
 		otherdata = ((PySetObject *)other)->data;
 	else {
--- 504,508 ----
 	if (PyDict_Check(other))
 		otherdata = other;
! 	else if (PyAnySet_Check(other))
 		otherdata = ((PySetObject *)other)->data;
 	else {
***************
*** 526,531 ****
 				Py_DECREF(it);
 				Py_DECREF(item);
- 				PyErr_SetString(PyExc_TypeError,
- 						"all set entries must be immutable");
 				return NULL;
 			}
--- 523,526 ----
***************
*** 535,540 ****
 				Py_DECREF(it);
 				Py_DECREF(item);
- 				PyErr_SetString(PyExc_TypeError,
- 						"all set entries must be immutable");
 				return NULL;
 			}
--- 530,533 ----
***************
*** 555,559 ****
 set_xor(PySetObject *so, PyObject *other)
 {
! 	if (!IS_SET(so) || !IS_SET(other)) {
 		Py_INCREF(Py_NotImplemented);
 		return Py_NotImplemented;
--- 548,552 ----
 set_xor(PySetObject *so, PyObject *other)
 {
! 	if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
 		Py_INCREF(Py_NotImplemented);
 		return Py_NotImplemented;
***************
*** 567,571 ****
 	PyObject *result;
 
! 	if (!IS_SET(other)) {
 		Py_INCREF(Py_NotImplemented);
 		return Py_NotImplemented;
--- 560,564 ----
 	PyObject *result;
 
! 	if (!PyAnySet_Check(other)) {
 		Py_INCREF(Py_NotImplemented);
 		return Py_NotImplemented;
***************
*** 584,588 ****
 	PyObject *otherdata, *it, *item;
 
! 	if (!IS_SET(other)) {
 		PyErr_SetString(PyExc_TypeError, "can only compare to a set");
 		return NULL;
--- 577,581 ----
 	PyObject *otherdata, *it, *item;
 
! 	if (!PyAnySet_Check(other)) {
 		PyErr_SetString(PyExc_TypeError, "can only compare to a set");
 		return NULL;
***************
*** 605,608 ****
--- 598,603 ----
 	}
 	Py_DECREF(it);
+ 	if (PyErr_Occurred()) 
+ 		return NULL;
 	Py_RETURN_TRUE;
 }
***************
*** 613,617 ****
 set_issuperset(PySetObject *so, PyObject *other)
 {
! 	if (!IS_SET(other)) {
 		PyErr_SetString(PyExc_TypeError, "can only compare to a set");
 		return NULL;
--- 608,612 ----
 set_issuperset(PySetObject *so, PyObject *other)
 {
! 	if (!PyAnySet_Check(other)) {
 		PyErr_SetString(PyExc_TypeError, "can only compare to a set");
 		return NULL;
***************
*** 654,659 ****
 		Py_DECREF(item);
 	}
- 	so->hash = hash;
 	Py_DECREF(it);
 	return hash;
 }
--- 649,656 ----
 		Py_DECREF(item);
 	}
 	Py_DECREF(it);
+ 	if (PyErr_Occurred()) 
+ 		return -1;
+ 	so->hash = hash;
 	return hash;
 }
***************
*** 662,671 ****
 set_richcompare(PySetObject *v, PyObject *w, int op)
 {
! 	/* XXX factor out is_set test */
! 	if (op == Py_EQ && !IS_SET(w))
! 		Py_RETURN_FALSE;
! 	else if (op == Py_NE && !IS_SET(w))
! 		Py_RETURN_TRUE;
! 	if (!IS_SET(w)) {
 		PyErr_SetString(PyExc_TypeError, "can only compare to a set");
 		return NULL;
--- 659,667 ----
 set_richcompare(PySetObject *v, PyObject *w, int op)
 {
! 	if(!PyAnySet_Check(w)) {
! 		if (op == Py_EQ)
! 			Py_RETURN_FALSE;
! 		if (op == Py_NE)
! 			Py_RETURN_TRUE;
 		PyErr_SetString(PyExc_TypeError, "can only compare to a set");
 		return NULL;
***************
*** 699,704 ****
--- 695,704 ----
 
 	keys = PyDict_Keys(so->data);
+ 	if (keys == NULL)
+ 		return NULL;
 	listrepr = PyObject_Repr(keys);
 	Py_DECREF(keys);
+ 	if (listrepr == NULL)
+ 		return NULL;
 
 	result = PyString_FromFormat("%s(%s)", so->ob_type->tp_name,
***************
*** 733,736 ****
--- 733,738 ----
 	Py_DECREF(it);
 	fprintf(fp, "])");
+ 	if (PyErr_Occurred()) 
+ 		return -1;
 	return 0;
 }
***************
*** 811,816 ****
 	}
 	Py_INCREF(key);
! 	if (PyDict_DelItem(so->data, key) == -1)
! 		PyErr_Clear();
 	return key;
 }
--- 813,820 ----
 	}
 	Py_INCREF(key);
! 	if (PyDict_DelItem(so->data, key) == -1) {
! 		Py_DECREF(key);
! 		return NULL;
! 	}
 	return key;
 }
***************
*** 838,841 ****
--- 842,867 ----
 PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
 
+ static int
+ set_init(PySetObject *self, PyObject *args, PyObject *kwds)
+ {
+ 	PyObject *iterable = NULL;
+ 	PyObject *result;
+ 
+ 	if (!PyAnySet_Check(self))
+ 		return -1;
+ 	if (!PyArg_UnpackTuple(args, self->ob_type->tp_name, 0, 1, &iterable))
+ 		return -1;
+ 	PyDict_Clear(self->data);
+ 	self->hash = -1;
+ 	if (iterable == NULL)
+ 		return 0;
+ 	result = set_union_update(self, iterable);
+ 	if (result != NULL) {
+ 		Py_DECREF(result);
+ 		return 0;
+ 	}
+ 	return -1;
+ }
+ 
 static PySequenceMethods set_as_sequence = {
 	(inquiry)set_len,		/* sq_length */
***************
*** 972,976 ****
 	0,				/* tp_descr_set */
 	0,				/* tp_dictoffset */
! 	0,				/* tp_init */
 	PyType_GenericAlloc,		/* tp_alloc */
 	set_new,			/* tp_new */
--- 998,1002 ----
 	0,				/* tp_descr_set */
 	0,				/* tp_dictoffset */
! 	(initproc)set_init,		/* tp_init */
 	PyType_GenericAlloc,		/* tp_alloc */
 	set_new,			/* tp_new */
***************
*** 1069,1073 ****
 	0,				/* tp_init */
 	PyType_GenericAlloc,		/* tp_alloc */
! 	set_new,			/* tp_new */
 	PyObject_GC_Del,		/* tp_free */
 };
--- 1095,1099 ----
 	0,				/* tp_init */
 	PyType_GenericAlloc,		/* tp_alloc */
! 	frozenset_new,			/* tp_new */
 	PyObject_GC_Del,		/* tp_free */
 };


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /