[Stackless-checkins] CVS: slpdev/src/2.2/src/Objects methodobject.c, 1.7, 1.8 rangeobject.c, 1.3, 1.4 structseq.c, 1.2, 1.3 typeobject.c, 1.9, 1.10

Christian Tismer tismer at centera.de
Mon May 3 18:54:33 CEST 2004


Update of /home/cvs/slpdev/src/2.2/src/Objects
In directory centera.de:/tmp/cvs-serv20604/src/2.2/src/Objects

Modified Files:
	methodobject.c rangeobject.c structseq.c typeobject.c 
Log Message:
completely back-ported Stackless to Python 2.2.3.
It passes all relevant tests, supports pickling etc.
The gola is to keep the core files identical as long
as possible. This caused a few changes to the test files,
which need to figure out which tests to omit, for instance.

This 2.2.3 versiion is almost identical to the original, but
a few changes:
- Pickling does not check for __safe_for_unpickling__
- Generators don't need from __future__ import generators
- code objects have a builtin tp_new method.

These changes *all* vanish if STACKLESS_OFF is defined.

Index: methodobject.c
===================================================================
RCS file: /home/cvs/slpdev/src/2.2/src/Objects/methodobject.c,v
retrieving revision 1.7
retrieving revision 1.8
diff -C2 -d -r1.7 -r1.8
*** methodobject.c	2 May 2004 15:07:06 -0000	1.7
--- methodobject.c	3 May 2004 16:54:29 -0000	1.8
***************
*** 256,260 ****
  		Py_TPFLAGS_HAVE_STACKLESS_CALL, /* tp_flags */
  #else
! 	Py_TPFLAGS_DEFAULT			/* tp_flags */
  #endif
   	0,					/* tp_doc */
--- 256,260 ----
  		Py_TPFLAGS_HAVE_STACKLESS_CALL, /* tp_flags */
  #else
! 	Py_TPFLAGS_DEFAULT,			/* tp_flags */
  #endif
   	0,					/* tp_doc */

Index: rangeobject.c
===================================================================
RCS file: /home/cvs/slpdev/src/2.2/src/Objects/rangeobject.c,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** rangeobject.c	1 May 2004 20:31:00 -0000	1.3
--- rangeobject.c	3 May 2004 16:54:29 -0000	1.4
***************
*** 108,111 ****
--- 108,188 ----
  }
  
+ #ifdef STACKLESS
+ 	/* since this is a backport to 2.2.3, we don't bother
+ 	   to keep the source clean of patches. Instead, try to
+ 	   keep the clobbering of the Stackless files to a minimum
+ 	 */
+ /* Return number of items in range/xrange (lo, hi, step).  step > 0
+  * required.  Return a value < 0 if & only if the true value is too
+  * large to fit in a signed long.
+  */
+ static long
+ get_len_of_range(long lo, long hi, long step)
+ {
+ 	/* -------------------------------------------------------------
+ 	If lo >= hi, the range is empty.
+ 	Else if n values are in the range, the last one is
+ 	lo + (n-1)*step, which must be <= hi-1.  Rearranging,
+ 	n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives
+ 	the proper value.  Since lo < hi in this case, hi-lo-1 >= 0, so
+ 	the RHS is non-negative and so truncation is the same as the
+ 	floor.  Letting M be the largest positive long, the worst case
+ 	for the RHS numerator is hi=M, lo=-M-1, and then
+ 	hi-lo-1 = M-(-M-1)-1 = 2*M.  Therefore unsigned long has enough
+ 	precision to compute the RHS exactly.
+ 	---------------------------------------------------------------*/
+ 	long n = 0;
+ 	if (lo < hi) {
+ 		unsigned long uhi = (unsigned long)hi;
+ 		unsigned long ulo = (unsigned long)lo;
+ 		unsigned long diff = uhi - ulo - 1;
+ 		n = (long)(diff / (unsigned long)step + 1);
+ 	}
+ 	return n;
+ }
+ 
+ static PyObject *
+ range_new(PyTypeObject *type, PyObject *args, PyObject *kw)
+ {
+ 	long ilow = 0, ihigh = 0, istep = 1;
+ 	long n;
+ 
+ 	if (PyTuple_Size(args) <= 1) {
+ 		if (!PyArg_ParseTuple(args,
+ 				"l;xrange() requires 1-3 int arguments",
+ 				&ihigh))
+ 			return NULL;
+ 	}
+ 	else {
+ 		if (!PyArg_ParseTuple(args,
+ 				"ll|l;xrange() requires 1-3 int arguments",
+ 				&ilow, &ihigh, &istep))
+ 			return NULL;
+ 	}
+ 	if (istep == 0) {
+ 		PyErr_SetString(PyExc_ValueError, "xrange() arg 3 must not be zero");
+ 		return NULL;
+ 	}
+ 	if (istep > 0)
+ 		n = get_len_of_range(ilow, ihigh, istep);
+ 	else
+ 		n = get_len_of_range(ihigh, ilow, -istep);
+ 	if (n < 0) {
+ 		PyErr_SetString(PyExc_OverflowError,
+ 				"xrange() result has too many items");
+ 		return NULL;
+ 	}
+ 	return PyRange_New(ilow, n, istep, 1);
+ }
+ 
+ static char range_doc[] =
+ "xrange([start,] stop[, step]) -> xrange object\n\
+ \n\
+ Like range(), but instead of returning a list, returns an object that\n\
+ generates the numbers in the range on demand.  For looping, this is \n\
+ slightly faster than range() and more memory efficient.";
+ 
+ #endif
+ 
  static PyObject *
  range_item(rangeobject *r, int i)
***************
*** 337,341 ****
--- 414,422 ----
  	0,					/*tp_as_buffer*/
  	Py_TPFLAGS_DEFAULT,			/*tp_flags*/
+ #ifdef STACKLESS
+ 	range_doc,				/*tp_doc*/
+ #else
  	0,					/* tp_doc */
+ #endif
  	0,					/* tp_traverse */
  	0,					/* tp_clear */
***************
*** 349,352 ****
--- 430,441 ----
  	0,					/* tp_base */
  	0,					/* tp_dict */
+ #ifdef STACKLESS
+ 	0,					/* tp_descr_get */
+ 	0,					/* tp_descr_set */
+ 	0,					/* tp_dictoffset */
+ 	0,					/* tp_init */
+ 	0,					/* tp_alloc */
+ 	range_new,				/* tp_new */
+ #endif
  };
  

Index: structseq.c
===================================================================
RCS file: /home/cvs/slpdev/src/2.2/src/Objects/structseq.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** structseq.c	21 Aug 2003 16:59:16 -0000	1.2
--- structseq.c	3 May 2004 16:54:29 -0000	1.3
***************
*** 374,378 ****
--- 374,380 ----
  	PyDict_SetItemString(dict, real_length_key, 
  		       PyInt_FromLong((long) n_members));
+ #ifndef STACKLESS
  	PyDict_SetItemString(dict, "__safe_for_unpickling__", 
  		       PyInt_FromLong(1));
+ #endif
  }

Index: typeobject.c
===================================================================
RCS file: /home/cvs/slpdev/src/2.2/src/Objects/typeobject.c,v
retrieving revision 1.9
retrieving revision 1.10
diff -C2 -d -r1.9 -r1.10
*** typeobject.c	2 May 2004 16:58:04 -0000	1.9
--- typeobject.c	3 May 2004 16:54:29 -0000	1.10
***************
*** 3713,3717 ****
  #define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
  	{NAME, offsetof(PyHeapTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
! 	 PyDoc_STR(DOC)}
  #define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
  	ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC)
--- 3713,3717 ----
  #define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
  	{NAME, offsetof(PyHeapTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
! 	 DOC}
  #define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
  	ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC)
***************
*** 3932,3945 ****
  	char *ptr;
  
  	assert(offset >= 0);
  	assert(offset < offsetof(PyHeapTypeObject, as_buffer));
! 	if (offset >= offsetof(PyHeapTypeObject, as_mapping)) {
! 		ptr = (void *)type->tp_as_mapping;
! 		offset -= offsetof(PyHeapTypeObject, as_mapping);
! 	}
! 	else if (offset >= offsetof(PyHeapTypeObject, as_sequence)) {
  		ptr = (void *)type->tp_as_sequence;
  		offset -= offsetof(PyHeapTypeObject, as_sequence);
  	}
  	else if (offset >= offsetof(PyHeapTypeObject, as_number)) {
  		ptr = (void *)type->tp_as_number;
--- 3932,3946 ----
  	char *ptr;
  
+ 	/* Note: this depends on the order of the members of PyHeapTypeObject! */
  	assert(offset >= 0);
  	assert(offset < offsetof(PyHeapTypeObject, as_buffer));
! 	if (offset >= offsetof(PyHeapTypeObject, as_sequence)) {
  		ptr = (void *)type->tp_as_sequence;
  		offset -= offsetof(PyHeapTypeObject, as_sequence);
  	}
+ 	else if (offset >= offsetof(PyHeapTypeObject, as_mapping)) {
+ 		ptr = (void *)type->tp_as_mapping;
+ 		offset -= offsetof(PyHeapTypeObject, as_mapping);
+ 	}
  	else if (offset >= offsetof(PyHeapTypeObject, as_number)) {
  		ptr = (void *)type->tp_as_number;


_______________________________________________
Stackless-checkins mailing list
Stackless-checkins at stackless.com
http://www.stackless.com/mailman/listinfo/stackless-checkins



More information about the Stackless-checkins mailing list