[Python-checkins] python/dist/src/Modules arraymodule.c,2.86,2.87

rhettinger@users.sourceforge.net rhettinger@users.sourceforge.net
Wed, 23 Apr 2003 10:27:03 -0700


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

Modified Files:
	arraymodule.c 
Log Message:
SF Patch 685051:  fix for 680789: reprs in arraymodule
(contributed by logistix; substantially reworked by rhettinger).

To create a representation of non-string arrays, array_repr() was
starting with a base Python string object and repeatedly using += 
to concatenate the representation of individual objects.

Logistix had the idea to convert to an intermediate tuple form and
then join it all at once.  I took advantage of existing tools and
formed a list with array_tolist() and got its representation through
PyObject_Repr(v) which already has a fast implementation for lists.



Index: arraymodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/arraymodule.c,v
retrieving revision 2.86
retrieving revision 2.87
diff -C2 -d -r2.86 -r2.87
*** arraymodule.c	17 Mar 2003 19:46:07 -0000	2.86
--- arraymodule.c	23 Apr 2003 17:27:00 -0000	2.87
***************
*** 1457,1462 ****
  {
  	char buf[256], typecode;
! 	PyObject *s, *t, *comma, *v;
! 	int i, len;
  
  	len = a->ob_size;
--- 1457,1462 ----
  {
  	char buf[256], typecode;
! 	PyObject *s, *t, *v = NULL;
! 	int len;
  
  	len = a->ob_size;
***************
*** 1466,1500 ****
  		return PyString_FromString(buf);
  	}
!         
! 	if (typecode == 'c' || typecode == 'u') {
! 		PyOS_snprintf(buf, sizeof(buf), "array('%c', ", typecode);
! 		s = PyString_FromString(buf);
! #ifdef Py_USING_UNICODE
! 		if (typecode == 'c')
! #endif
! 			v = array_tostring(a, NULL);
  #ifdef Py_USING_UNICODE
! 		else
! 			v = array_tounicode(a, NULL);
  #endif
! 		t = PyObject_Repr(v);
! 		Py_XDECREF(v);
! 		PyString_ConcatAndDel(&s, t);
! 		PyString_ConcatAndDel(&s, PyString_FromString(")"));
! 		return s;
! 	}
! 	PyOS_snprintf(buf, sizeof(buf), "array('%c', [", typecode);
  	s = PyString_FromString(buf);
! 	comma = PyString_FromString(", ");
! 	for (i = 0; i < len && !PyErr_Occurred(); i++) {
! 		if (i > 0)
! 			PyString_Concat(&s, comma);
! 		v = (a->ob_descr->getitem)(a, i);
! 		t = PyObject_Repr(v);
! 		Py_XDECREF(v);
! 		PyString_ConcatAndDel(&s, t);
! 	}
! 	Py_XDECREF(comma);
! 	PyString_ConcatAndDel(&s, PyString_FromString("])"));
  	return s;
  }
--- 1466,1485 ----
  		return PyString_FromString(buf);
  	}
! 		
! 	if (typecode == 'c')
! 		v = array_tostring(a, NULL);
  #ifdef Py_USING_UNICODE
! 	else if (typecode == 'u')
! 		v = array_tounicode(a, NULL);
  #endif
! 	else
! 		v = array_tolist(a, NULL);
! 	t = PyObject_Repr(v);
! 	Py_XDECREF(v);
! 
! 	PyOS_snprintf(buf, sizeof(buf), "array('%c', ", typecode);
  	s = PyString_FromString(buf);
! 	PyString_ConcatAndDel(&s, t);
! 	PyString_ConcatAndDel(&s, PyString_FromString(")"));
  	return s;
  }