[Python-checkins] CVS: python/dist/src/Objects typeobject.c,2.81,2.82

Guido van Rossum gvanrossum@users.sourceforge.net
Mon, 01 Oct 2001 10:18:24 -0700


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

Modified Files:
	typeobject.c 
Log Message:
Miscellaneous code fiddling:

- SLOT1BINFULL() macro: changed this to check for __rop__ overriding
  __op__, like binary_op1() in abstract.c -- the latter only calls the
  slot function once if both types use the same slot function, so the
  slot function must make both calls -- which it already did for the
  __op__, __rop__ order, but not yet for the __rop__, __op__ order
  when B.__class__ is a subclass of A.__class__.

- slot_sq_contains(), slot_nb_nonzero(): use lookup_maybe() rather
  than lookup_method() which sets an exception which we then clear.

- slot_nb_coerce(): don't give up when left argument's __coerce__
returns NotImplemented, but give the right argument a chance.


Index: typeobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/typeobject.c,v
retrieving revision 2.81
retrieving revision 2.82
diff -C2 -d -r2.81 -r2.82
*** typeobject.c	2001/10/01 16:42:49	2.81
--- typeobject.c	2001/10/01 17:18:22	2.82
***************
*** 2694,2700 ****
--- 2694,2712 ----
  { \
  	static PyObject *cache_str, *rcache_str; \
+ 	int do_other = self->ob_type != other->ob_type && \
+ 	    other->ob_type->tp_as_number != NULL && \
+ 	    other->ob_type->tp_as_number->SLOTNAME == TESTFUNC; \
  	if (self->ob_type->tp_as_number != NULL && \
  	    self->ob_type->tp_as_number->SLOTNAME == TESTFUNC) { \
  		PyObject *r; \
+ 		if (do_other && \
+ 		    PyType_IsSubtype(other->ob_type, self->ob_type)) { \
+ 			r = call_maybe( \
+ 				other, ROPSTR, &rcache_str, "(O)", self); \
+ 			if (r != Py_NotImplemented) \
+ 				return r; \
+ 			Py_DECREF(r); \
+ 			do_other = 0; \
+ 		} \
  		r = call_maybe( \
  			self, OPSTR, &cache_str, "(O)", other); \
***************
*** 2704,2709 ****
  		Py_DECREF(r); \
  	} \
! 	if (other->ob_type->tp_as_number != NULL && \
! 	    other->ob_type->tp_as_number->SLOTNAME == TESTFUNC) { \
  		return call_maybe( \
  			other, ROPSTR, &rcache_str, "(O)", self); \
--- 2716,2720 ----
  		Py_DECREF(r); \
  	} \
! 	if (do_other) { \
  		return call_maybe( \
  			other, ROPSTR, &rcache_str, "(O)", self); \
***************
*** 2786,2790 ****
  	static PyObject *contains_str;
  
! 	func = lookup_method(self, "__contains__", &contains_str);
  
  	if (func != NULL) {
--- 2797,2801 ----
  	static PyObject *contains_str;
  
! 	func = lookup_maybe(self, "__contains__", &contains_str);
  
  	if (func != NULL) {
***************
*** 2801,2806 ****
  		return PyObject_IsTrue(res);
  	}
  	else {
- 		PyErr_Clear();
  		return _PySequence_IterSearch(self, value,
  					      PY_ITERSEARCH_CONTAINS);
--- 2812,2818 ----
  		return PyObject_IsTrue(res);
  	}
+ 	else if (PyErr_Occurred())
+ 		return -1;
  	else {
  		return _PySequence_IterSearch(self, value,
  					      PY_ITERSEARCH_CONTAINS);
***************
*** 2867,2887 ****
  	static PyObject *nonzero_str, *len_str;
  
! 	func = lookup_method(self, "__nonzero__", &nonzero_str);
  	if (func == NULL) {
! 		PyErr_Clear();
! 		func = lookup_method(self, "__len__", &len_str);
! 	}
! 
! 	if (func != NULL) {
! 		res = PyObject_CallObject(func, NULL);
! 		Py_DECREF(func);
! 		if (res == NULL)
  			return -1;
! 		return PyObject_IsTrue(res);
! 	}
! 	else {
! 		PyErr_Clear();
! 		return 1;
  	}
  }
  
--- 2879,2899 ----
  	static PyObject *nonzero_str, *len_str;
  
! 	func = lookup_maybe(self, "__nonzero__", &nonzero_str);
  	if (func == NULL) {
! 		if (PyErr_Occurred())
  			return -1;
! 		func = lookup_maybe(self, "__len__", &len_str);
! 		if (func == NULL) {
! 			if (PyErr_Occurred())
! 				return -1;
! 			else
! 				return 1;
! 		}
  	}
+ 	res = PyObject_CallObject(func, NULL);
+ 	Py_DECREF(func);
+ 	if (res == NULL)
+ 		return -1;
+ 	return PyObject_IsTrue(res);
  }
  
***************
*** 2908,2925 ****
  		if (r == Py_NotImplemented) {
  			Py_DECREF(r);
- 			return 1;
  		}
! 		if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {
! 			PyErr_SetString(PyExc_TypeError,
  					"__coerce__ didn't return a 2-tuple");
  			Py_DECREF(r);
! 			return -1;
  		}
- 		*a = PyTuple_GET_ITEM(r, 0);
- 		Py_INCREF(*a);
- 		*b = PyTuple_GET_ITEM(r, 1);
- 		Py_INCREF(*b);
- 		Py_DECREF(r);
- 		return 0;
  	}
  	if (other->ob_type->tp_as_number != NULL &&
--- 2920,2938 ----
  		if (r == Py_NotImplemented) {
  			Py_DECREF(r);
  		}
! 		else {
! 			if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {
! 				PyErr_SetString(PyExc_TypeError,
  					"__coerce__ didn't return a 2-tuple");
+ 				Py_DECREF(r);
+ 				return -1;
+ 			}
+ 			*a = PyTuple_GET_ITEM(r, 0);
+ 			Py_INCREF(*a);
+ 			*b = PyTuple_GET_ITEM(r, 1);
+ 			Py_INCREF(*b);
  			Py_DECREF(r);
! 			return 0;
  		}
  	}
  	if (other->ob_type->tp_as_number != NULL &&