[Python-checkins] python/dist/src/Objects intobject.c,2.95,2.96 longobject.c,1.145,1.146

nascheme@users.sourceforge.net nascheme@users.sourceforge.net
Mon, 30 Dec 2002 12:19:04 -0800


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

Modified Files:
	intobject.c longobject.c 
Log Message:
Consolidate the int and long sequence repeat code.  Before the change,
integers checked for integer overflow but longs did not. 


Index: intobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/intobject.c,v
retrieving revision 2.95
retrieving revision 2.96
diff -C2 -d -r2.95 -r2.96
*** intobject.c	19 Nov 2002 20:49:15 -0000	2.95
--- intobject.c	30 Dec 2002 20:19:02 -0000	2.96
***************
*** 355,366 ****
  */
  
- /* Return true if the sq_repeat method should be used */
- #define USE_SQ_REPEAT(o) (!PyInt_Check(o) && \
- 			  o->ob_type->tp_as_sequence && \
- 			  o->ob_type->tp_as_sequence->sq_repeat && \
- 			  !(o->ob_type->tp_as_number && \
-                             o->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES && \
- 			    o->ob_type->tp_as_number->nb_multiply))
- 
  static PyObject *
  int_mul(PyObject *v, PyObject *w)
--- 355,358 ----
***************
*** 370,411 ****
  	double doubled_longprod;	/* (double)longprod */
  	double doubleprod;		/* (double)a * (double)b */
- 
- 	if (USE_SQ_REPEAT(v)) {
- 	  repeat:
- 		/* sequence * int */
- 		a = PyInt_AsLong(w);
- #if LONG_MAX != INT_MAX
- 		if (a > INT_MAX) {
- 			PyErr_SetString(PyExc_ValueError,
- 					"sequence repeat count too large");
- 			return NULL;
- 		}
- 		else if (a < INT_MIN)
- 			a = INT_MIN;
- 		/* XXX Why don't I either
- 
- 		   - set a to -1 whenever it's negative (after all,
- 		     sequence repeat usually treats negative numbers
- 		     as zero(); or
- 
- 		   - raise an exception when it's less than INT_MIN?
- 
- 		   I'm thinking about a hypothetical use case where some
- 		   sequence type might use a negative value as a flag of
- 		   some kind.  In those cases I don't want to break the
- 		   code by mapping all negative values to -1.  But I also
- 		   don't want to break e.g. []*(-sys.maxint), which is
- 		   perfectly safe, returning [].  As a compromise, I do
- 		   map out-of-range negative values.
- 		*/
- #endif
- 		return (*v->ob_type->tp_as_sequence->sq_repeat)(v, a);
- 	}
- 	if (USE_SQ_REPEAT(w)) {
- 		PyObject *tmp = v;
- 		v = w;
- 		w = tmp;
- 		goto repeat;
- 	}
  
  	CONVERT_TO_LONG(v, a);
--- 362,365 ----

Index: longobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/longobject.c,v
retrieving revision 1.145
retrieving revision 1.146
diff -C2 -d -r1.145 -r1.146
*** longobject.c	19 Nov 2002 20:49:15 -0000	1.145
--- longobject.c	30 Dec 2002 20:19:02 -0000	1.146
***************
*** 1509,1523 ****
  }
  
- static PyObject *
- long_repeat(PyObject *v, PyLongObject *w)
- {
- 	/* sequence * long */
- 	long n = PyLong_AsLong((PyObject *) w);
- 	if (n == -1 && PyErr_Occurred())
- 		return NULL;
- 	else
- 		return (*v->ob_type->tp_as_sequence->sq_repeat)(v, n);
- }
- 
  /* Grade school multiplication, ignoring the signs.
   * Returns the absolute value of the product, or NULL if error.
--- 1509,1512 ----
***************
*** 1869,1880 ****
  
  	if (!convert_binop((PyObject *)v, (PyObject *)w, &a, &b)) {
- 		if (!PyLong_Check(v) &&
- 		    v->ob_type->tp_as_sequence &&
- 		    v->ob_type->tp_as_sequence->sq_repeat)
- 			return long_repeat((PyObject *)v, w);
- 		if (!PyLong_Check(w) &&
- 		    w->ob_type->tp_as_sequence &&
- 		    w->ob_type->tp_as_sequence->sq_repeat)
- 			return long_repeat((PyObject *)w, v);
  		Py_INCREF(Py_NotImplemented);
  		return Py_NotImplemented;
--- 1858,1861 ----