[Python-checkins] CVS: python/dist/src/Objects intobject.c,2.53,2.54

Neil Schemenauer python-dev@python.org
Wed, 03 Jan 2001 17:45:35 -0800


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

Modified Files:
	intobject.c 
Log Message:
Make int a new style number type.  Sequence repeat is now done here
now as well.


Index: intobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/intobject.c,v
retrieving revision 2.53
retrieving revision 2.54
diff -C2 -r2.53 -r2.54
*** intobject.c	2000/10/24 19:57:45	2.53
--- intobject.c	2001/01/04 01:45:33	2.54
***************
*** 220,223 ****
--- 220,236 ----
  /* Methods */
  
+ /* Integers are seen as the "smallest" of all numeric types and thus
+    don't have any knowledge about conversion of other types to
+    integers. */
+ 
+ #define CONVERT_TO_LONG(obj, lng)		\
+ 	if (PyInt_Check(obj)) {			\
+ 		lng = PyInt_AS_LONG(obj);	\
+ 	}					\
+ 	else {					\
+ 		Py_INCREF(Py_NotImplemented);	\
+ 		return Py_NotImplemented;	\
+ 	}
+ 
  /* ARGSUSED */
  static int
***************
*** 245,248 ****
--- 258,271 ----
  }
  
+ /* Needed for the new style number compare slots */
+ static PyObject *
+ int_cmp(PyObject *v, PyObject *w)
+ {
+ 	register long a, b;
+ 	CONVERT_TO_LONG(v, a);
+ 	CONVERT_TO_LONG(w, b);
+ 	return PyInt_FromLong((a < b) ? -1 : (a > b) ? 1 : 0);
+ }
+ 
  static long
  int_hash(PyIntObject *v)
***************
*** 260,265 ****
  {
  	register long a, b, x;
! 	a = v->ob_ival;
! 	b = w->ob_ival;
  	x = a + b;
  	if ((x^a) < 0 && (x^b) < 0)
--- 283,288 ----
  {
  	register long a, b, x;
! 	CONVERT_TO_LONG(v, a);
! 	CONVERT_TO_LONG(w, b);
  	x = a + b;
  	if ((x^a) < 0 && (x^b) < 0)
***************
*** 272,277 ****
  {
  	register long a, b, x;
! 	a = v->ob_ival;
! 	b = w->ob_ival;
  	x = a - b;
  	if ((x^a) < 0 && (x^~b) < 0)
--- 295,300 ----
  {
  	register long a, b, x;
! 	CONVERT_TO_LONG(v, a);
! 	CONVERT_TO_LONG(w, b);
  	x = a - b;
  	if ((x^a) < 0 && (x^~b) < 0)
***************
*** 310,320 ****
  
  static PyObject *
! int_mul(PyIntObject *v, PyIntObject *w)
  {
  	long a, b, ah, bh, x, y;
  	int s = 1;
  
! 	a = v->ob_ival;
! 	b = w->ob_ival;
  	ah = a >> (LONG_BIT/2);
  	bh = b >> (LONG_BIT/2);
--- 333,356 ----
  
  static PyObject *
! int_mul(PyObject *v, PyObject *w)
  {
  	long a, b, ah, bh, x, y;
  	int s = 1;
  
! 	if (v->ob_type->tp_as_sequence &&
! 			v->ob_type->tp_as_sequence->sq_repeat) {
! 		/* sequence * int */
! 		a = PyInt_AsLong(w);
! 		return (*v->ob_type->tp_as_sequence->sq_repeat)(v, a);
! 	}
! 	else if (w->ob_type->tp_as_sequence &&
! 			w->ob_type->tp_as_sequence->sq_repeat) {
! 		/* int * sequence */
! 		a = PyInt_AsLong(v);
! 		return (*w->ob_type->tp_as_sequence->sq_repeat)(w, a);
! 	}
! 
! 	CONVERT_TO_LONG(v, a);
! 	CONVERT_TO_LONG(w, b);
  	ah = a >> (LONG_BIT/2);
  	bh = b >> (LONG_BIT/2);
***************
*** 409,417 ****
  
  static int
! i_divmod(register PyIntObject *x, register PyIntObject *y,
           long *p_xdivy, long *p_xmody)
  {
- 	long xi = x->ob_ival;
- 	long yi = y->ob_ival;
  	long xdivy, xmody;
  	
--- 445,451 ----
  
  static int
! i_divmod(register long xi, register long yi,
           long *p_xdivy, long *p_xmody)
  {
  	long xdivy, xmody;
  	
***************
*** 452,457 ****
  int_div(PyIntObject *x, PyIntObject *y)
  {
  	long d, m;
! 	if (i_divmod(x, y, &d, &m) < 0)
  		return NULL;
  	return PyInt_FromLong(d);
--- 486,494 ----
  int_div(PyIntObject *x, PyIntObject *y)
  {
+ 	long xi, yi;
  	long d, m;
! 	CONVERT_TO_LONG(x, xi);
! 	CONVERT_TO_LONG(y, yi);
! 	if (i_divmod(xi, yi, &d, &m) < 0)
  		return NULL;
  	return PyInt_FromLong(d);
***************
*** 461,466 ****
  int_mod(PyIntObject *x, PyIntObject *y)
  {
  	long d, m;
! 	if (i_divmod(x, y, &d, &m) < 0)
  		return NULL;
  	return PyInt_FromLong(m);
--- 498,506 ----
  int_mod(PyIntObject *x, PyIntObject *y)
  {
+ 	long xi, yi;
  	long d, m;
! 	CONVERT_TO_LONG(x, xi);
! 	CONVERT_TO_LONG(y, yi);
! 	if (i_divmod(xi, yi, &d, &m) < 0)
  		return NULL;
  	return PyInt_FromLong(m);
***************
*** 470,475 ****
  int_divmod(PyIntObject *x, PyIntObject *y)
  {
  	long d, m;
! 	if (i_divmod(x, y, &d, &m) < 0)
  		return NULL;
  	return Py_BuildValue("(ll)", d, m);
--- 510,518 ----
  int_divmod(PyIntObject *x, PyIntObject *y)
  {
+ 	long xi, yi;
  	long d, m;
! 	CONVERT_TO_LONG(x, xi);
! 	CONVERT_TO_LONG(y, yi);
! 	if (i_divmod(xi, yi, &d, &m) < 0)
  		return NULL;
  	return Py_BuildValue("(ll)", d, m);
***************
*** 481,486 ****
  #if 1
  	register long iv, iw, iz=0, ix, temp, prev;
! 	iv = v->ob_ival;
! 	iw = w->ob_ival;
  	if (iw < 0) {
  		if (iv)
--- 524,529 ----
  #if 1
  	register long iv, iw, iz=0, ix, temp, prev;
! 	CONVERT_TO_LONG(v, iv);
! 	CONVERT_TO_LONG(w, iw);
  	if (iw < 0) {
  		if (iv)
***************
*** 493,497 ****
  	}
   	if ((PyObject *)z != Py_None) {
! 		iz = z->ob_ival;
  		if (iz == 0) {
  			PyErr_SetString(PyExc_ValueError,
--- 536,540 ----
  	}
   	if ((PyObject *)z != Py_None) {
! 		CONVERT_TO_LONG(z, iz);
  		if (iz == 0) {
  			PyErr_SetString(PyExc_ValueError,
***************
*** 532,549 ****
  	}
  	if (iz) {
! 	 	PyObject *t1, *t2;
! 	 	long int div, mod;
! 	 	t1=PyInt_FromLong(ix); 
! 		t2=PyInt_FromLong(iz);
! 	 	if (t1==NULL || t2==NULL ||
! 	 		i_divmod((PyIntObject *)t1,
! 				 (PyIntObject *)t2, &div, &mod)<0)
! 		{
! 		 	Py_XDECREF(t1);
! 		 	Py_XDECREF(t2);
  			return(NULL);
- 		}
- 		Py_DECREF(t1);
- 		Py_DECREF(t2);
  	 	ix=mod;
  	}
--- 575,581 ----
  	}
  	if (iz) {
! 	 	long div, mod;
! 	 	if (i_divmod(ix, iz, &div, &mod) < 0)
  			return(NULL);
  	 	ix=mod;
  	}
***************
*** 551,556 ****
  #else
  	register long iv, iw, ix;
! 	iv = v->ob_ival;
! 	iw = w->ob_ival;
  	if (iw < 0) {
  		PyErr_SetString(PyExc_ValueError,
--- 583,588 ----
  #else
  	register long iv, iw, ix;
! 	CONVERT_TO_LONG(v, iv);
! 	CONVERT_TO_LONG(w, iw);
  	if (iw < 0) {
  		PyErr_SetString(PyExc_ValueError,
***************
*** 619,624 ****
  {
  	register long a, b;
! 	a = v->ob_ival;
! 	b = w->ob_ival;
  	if (b < 0) {
  		PyErr_SetString(PyExc_ValueError, "negative shift count");
--- 651,656 ----
  {
  	register long a, b;
! 	CONVERT_TO_LONG(v, a);
! 	CONVERT_TO_LONG(w, b);
  	if (b < 0) {
  		PyErr_SetString(PyExc_ValueError, "negative shift count");
***************
*** 640,645 ****
  {
  	register long a, b;
! 	a = v->ob_ival;
! 	b = w->ob_ival;
  	if (b < 0) {
  		PyErr_SetString(PyExc_ValueError, "negative shift count");
--- 672,677 ----
  {
  	register long a, b;
! 	CONVERT_TO_LONG(v, a);
! 	CONVERT_TO_LONG(w, b);
  	if (b < 0) {
  		PyErr_SetString(PyExc_ValueError, "negative shift count");
***************
*** 666,671 ****
  {
  	register long a, b;
! 	a = v->ob_ival;
! 	b = w->ob_ival;
  	return PyInt_FromLong(a & b);
  }
--- 698,703 ----
  {
  	register long a, b;
! 	CONVERT_TO_LONG(v, a);
! 	CONVERT_TO_LONG(w, b);
  	return PyInt_FromLong(a & b);
  }
***************
*** 675,680 ****
  {
  	register long a, b;
! 	a = v->ob_ival;
! 	b = w->ob_ival;
  	return PyInt_FromLong(a ^ b);
  }
--- 707,712 ----
  {
  	register long a, b;
! 	CONVERT_TO_LONG(v, a);
! 	CONVERT_TO_LONG(w, b);
  	return PyInt_FromLong(a ^ b);
  }
***************
*** 684,689 ****
  {
  	register long a, b;
! 	a = v->ob_ival;
! 	b = w->ob_ival;
  	return PyInt_FromLong(a | b);
  }
--- 716,721 ----
  {
  	register long a, b;
! 	CONVERT_TO_LONG(v, a);
! 	CONVERT_TO_LONG(w, b);
  	return PyInt_FromLong(a | b);
  }
***************
*** 730,756 ****
  
  static PyNumberMethods int_as_number = {
! 	(binaryfunc)int_add, /*nb_add*/
! 	(binaryfunc)int_sub, /*nb_subtract*/
! 	(binaryfunc)int_mul, /*nb_multiply*/
! 	(binaryfunc)int_div, /*nb_divide*/
! 	(binaryfunc)int_mod, /*nb_remainder*/
! 	(binaryfunc)int_divmod, /*nb_divmod*/
! 	(ternaryfunc)int_pow, /*nb_power*/
! 	(unaryfunc)int_neg, /*nb_negative*/
! 	(unaryfunc)int_pos, /*nb_positive*/
! 	(unaryfunc)int_abs, /*nb_absolute*/
! 	(inquiry)int_nonzero, /*nb_nonzero*/
! 	(unaryfunc)int_invert, /*nb_invert*/
! 	(binaryfunc)int_lshift, /*nb_lshift*/
! 	(binaryfunc)int_rshift, /*nb_rshift*/
! 	(binaryfunc)int_and, /*nb_and*/
! 	(binaryfunc)int_xor, /*nb_xor*/
! 	(binaryfunc)int_or, /*nb_or*/
! 	0,		/*nb_coerce*/
! 	(unaryfunc)int_int, /*nb_int*/
! 	(unaryfunc)int_long, /*nb_long*/
! 	(unaryfunc)int_float, /*nb_float*/
! 	(unaryfunc)int_oct, /*nb_oct*/
! 	(unaryfunc)int_hex, /*nb_hex*/
  };
  
--- 762,802 ----
  
  static PyNumberMethods int_as_number = {
! 	(binaryfunc)int_add,	/*nb_add*/
! 	(binaryfunc)int_sub,	/*nb_subtract*/
! 	(binaryfunc)int_mul,	/*nb_multiply*/
! 	(binaryfunc)int_div,	/*nb_divide*/
! 	(binaryfunc)int_mod,	/*nb_remainder*/
! 	(binaryfunc)int_divmod,	/*nb_divmod*/
! 	(ternaryfunc)int_pow,	/*nb_power*/
! 	(unaryfunc)int_neg,	/*nb_negative*/
! 	(unaryfunc)int_pos,	/*nb_positive*/
! 	(unaryfunc)int_abs,	/*nb_absolute*/
! 	(inquiry)int_nonzero,	/*nb_nonzero*/
! 	(unaryfunc)int_invert,	/*nb_invert*/
! 	(binaryfunc)int_lshift,	/*nb_lshift*/
! 	(binaryfunc)int_rshift,	/*nb_rshift*/
! 	(binaryfunc)int_and,	/*nb_and*/
! 	(binaryfunc)int_xor,	/*nb_xor*/
! 	(binaryfunc)int_or,	/*nb_or*/
! 	0,			/*nb_coerce*/
! 	(unaryfunc)int_int,	/*nb_int*/
! 	(unaryfunc)int_long,	/*nb_long*/
! 	(unaryfunc)int_float,	/*nb_float*/
! 	(unaryfunc)int_oct,	/*nb_oct*/
! 	(unaryfunc)int_hex, 	/*nb_hex*/
! 	0,			/*nb_inplace_add*/
! 	0,			/*nb_inplace_subtract*/
! 	0,			/*nb_inplace_multiply*/
! 	0,			/*nb_inplace_divide*/
! 	0,			/*nb_inplace_remainder*/
! 	0,			/*nb_inplace_power*/
! 	0,			/*nb_inplace_lshift*/
! 	0,			/*nb_inplace_rshift*/
! 	0,			/*nb_inplace_and*/
! 	0,			/*nb_inplace_xor*/
! 	0,			/*nb_inplace_or*/
! 	
! 	/* New style slots: */
! 	(binaryfunc)int_cmp,	/*nb_cmp*/
  };
  
***************
*** 771,774 ****
--- 817,826 ----
  	0,		/*tp_as_mapping*/
  	(hashfunc)int_hash, /*tp_hash*/
+         0,			/*tp_call*/
+         0,			/*tp_str*/
+ 	0,			/*tp_getattro*/
+ 	0,			/*tp_setattro*/
+ 	0,			/*tp_as_buffer*/
+ 	Py_TPFLAGS_NEWSTYLENUMBER /*tp_flags*/
  };