[Python-checkins] CVS: python/dist/src/Objects complexobject.c,2.48,2.49 object.c,2.151,2.152 stringobject.c,2.134,2.135 typeobject.c,2.75,2.76

Guido van Rossum gvanrossum@users.sourceforge.net
Thu, 27 Sep 2001 13:30:10 -0700


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

Modified Files:
	complexobject.c object.c stringobject.c typeobject.c 
Log Message:
Merge branch changes (coercion, rich comparisons) into trunk.

Index: complexobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/complexobject.c,v
retrieving revision 2.48
retrieving revision 2.49
diff -C2 -d -r2.48 -r2.49
*** complexobject.c	2001/09/24 17:52:04	2.48
--- complexobject.c	2001/09/27 20:30:07	2.49
***************
*** 561,568 ****
  		return Py_NotImplemented;
  	}
! 	/* May sure both arguments use complex comparison.
! 	   This implies PyComplex_Check(a) && PyComplex_Check(b). */
! 	if (v->ob_type->tp_richcompare != complex_richcompare ||
! 	    w->ob_type->tp_richcompare != complex_richcompare) {
  		Py_DECREF(v);
  		Py_DECREF(w);
--- 561,566 ----
  		return Py_NotImplemented;
  	}
! 	/* Make sure both arguments are complex. */
! 	if (!(PyComplex_Check(v) && PyComplex_Check(w))) {
  		Py_DECREF(v);
  		Py_DECREF(w);

Index: object.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/object.c,v
retrieving revision 2.151
retrieving revision 2.152
diff -C2 -d -r2.151 -r2.152
*** object.c	2001/09/20 13:38:22	2.151
--- object.c	2001/09/27 20:30:07	2.152
***************
*** 366,369 ****
--- 366,377 ----
  	PyObject *res;
  
+ 	if (v->ob_type != w->ob_type &&
+ 	    PyType_IsSubtype(w->ob_type, v->ob_type) &&
+ 	    (f = RICHCOMPARE(w->ob_type)) != NULL) {
+ 		res = (*f)(w, v, swapped_op[op]);
+ 		if (res != Py_NotImplemented)
+ 			return res;
+ 		Py_DECREF(res);
+ 	}
  	if ((f = RICHCOMPARE(v->ob_type)) != NULL) {
  		res = (*f)(v, w, op);

Index: stringobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/stringobject.c,v
retrieving revision 2.134
retrieving revision 2.135
diff -C2 -d -r2.134 -r2.135
*** stringobject.c	2001/09/24 16:51:54	2.134
--- stringobject.c	2001/09/27 20:30:07	2.135
***************
*** 825,832 ****
  	PyObject *result;
  
! 	/* May sure both arguments use string comparison.
! 	   This implies PyString_Check(a) && PyString_Check(b). */
! 	if (a->ob_type->tp_richcompare != (richcmpfunc)string_richcompare ||
! 	    b->ob_type->tp_richcompare != (richcmpfunc)string_richcompare) {
  		result = Py_NotImplemented;
  		goto out;
--- 825,830 ----
  	PyObject *result;
  
! 	/* Make sure both arguments are strings. */
! 	if (!(PyString_Check(a) && PyString_Check(b))) {
  		result = Py_NotImplemented;
  		goto out;

Index: typeobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/typeobject.c,v
retrieving revision 2.75
retrieving revision 2.76
diff -C2 -d -r2.75 -r2.76
*** typeobject.c	2001/09/25 21:16:33	2.75
--- typeobject.c	2001/09/27 20:30:07	2.76
***************
*** 1838,1841 ****
--- 1838,1875 ----
  BINARY(xor, "x^y");
  BINARY(or, "x|y");
+ 
+ static PyObject *
+ wrap_coercefunc(PyObject *self, PyObject *args, void *wrapped)
+ {
+ 	coercion func = (coercion)wrapped;
+ 	PyObject *other, *res;
+ 	int ok;
+ 
+ 	if (!PyArg_ParseTuple(args, "O", &other))
+ 		return NULL;
+ 	ok = func(&self, &other);
+ 	if (ok < 0)
+ 		return NULL;
+ 	if (ok > 0) {
+ 		Py_INCREF(Py_NotImplemented);
+ 		return Py_NotImplemented;
+ 	}
+ 	res = PyTuple_New(2);
+ 	if (res == NULL) {
+ 		Py_DECREF(self);
+ 		Py_DECREF(other);
+ 		return NULL;
+ 	}
+ 	PyTuple_SET_ITEM(res, 0, self);
+ 	PyTuple_SET_ITEM(res, 1, other);
+ 	return res;
+ }
+ 
+ static struct wrapperbase tab_coerce[] = {
+ 	{"__coerce__", (wrapperfunc)wrap_coercefunc,
+ 	 "x.__coerce__(y) <==> coerce(x, y)"},
+ 	{0}
+ };
+ 
  BINARY(floordiv, "x//y");
  BINARY(truediv, "x/y # true division");
***************
*** 2574,2578 ****
  		ADD(nb->nb_xor, tab_xor);
  		ADD(nb->nb_or, tab_or);
! 		/* We don't support coerce() -- see above comment */
  		ADD(nb->nb_int, tab_int);
  		ADD(nb->nb_long, tab_long);
--- 2608,2612 ----
  		ADD(nb->nb_xor, tab_xor);
  		ADD(nb->nb_or, tab_or);
! 		ADD(nb->nb_coerce, tab_coerce);
  		ADD(nb->nb_int, tab_int);
  		ADD(nb->nb_long, tab_long);
***************
*** 2841,2845 ****
  SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__")
  SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__")
! /* Not coerce() */
  SLOT0(slot_nb_int, "__int__")
  SLOT0(slot_nb_long, "__long__")
--- 2875,2936 ----
  SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__")
  SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__")
! 
! static int
! slot_nb_coerce(PyObject **a, PyObject **b)
! {
! 	static PyObject *coerce_str;
! 	PyObject *self = *a, *other = *b;
! 
! 	if (self->ob_type->tp_as_number != NULL &&
! 	    self->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {
! 		PyObject *r;
! 		r = call_maybe(
! 			self, "__coerce__", &coerce_str, "(O)", other);
! 		if (r == NULL)
! 			return -1;
! 		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 &&
! 	    other->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {
! 		PyObject *r;
! 		r = call_maybe(
! 			other, "__coerce__", &coerce_str, "(O)", self);
! 		if (r == NULL)
! 			return -1;
! 		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, 1);
! 		Py_INCREF(*a);
! 		*b = PyTuple_GET_ITEM(r, 0);
! 		Py_INCREF(*b);
! 		Py_DECREF(r);
! 		return 0;
! 	}
! 	return 1;
! }
! 
  SLOT0(slot_nb_int, "__int__")
  SLOT0(slot_nb_long, "__long__")
***************
*** 3337,3341 ****
  	NBSLOT("__xor__", nb_xor, slot_nb_xor);
  	NBSLOT("__or__", nb_or, slot_nb_or);
! 	/* Not coerce() */
  	NBSLOT("__int__", nb_int, slot_nb_int);
  	NBSLOT("__long__", nb_long, slot_nb_long);
--- 3428,3432 ----
  	NBSLOT("__xor__", nb_xor, slot_nb_xor);
  	NBSLOT("__or__", nb_or, slot_nb_or);
! 	NBSLOT("__coerce__", nb_coerce, slot_nb_coerce);
  	NBSLOT("__int__", nb_int, slot_nb_int);
  	NBSLOT("__long__", nb_long, slot_nb_long);