[Python-checkins] CVS: python/dist/src/Modules arraymodule.c,2.66,2.67

Martin v. L?wis loewis@users.sourceforge.net
Fri, 01 Mar 2002 02:27:03 -0800


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

Modified Files:
	arraymodule.c 
Log Message:
Patch 520694: arraymodule.c improvements:
- make array.array a type
- add Py_UNICODE arrays
- support +=, *=


Index: arraymodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/arraymodule.c,v
retrieving revision 2.66
retrieving revision 2.67
diff -C2 -d -r2.66 -r2.67
*** arraymodule.c	8 Dec 2001 18:02:55 -0000	2.66
--- arraymodule.c	1 Mar 2002 10:27:01 -0000	2.67
***************
*** 28,32 ****
  
  typedef struct arrayobject {
! 	PyObject_VAR_HEAD
  	char *ob_item;
  	struct arraydescr *ob_descr;
--- 28,33 ----
  
  typedef struct arrayobject {
! 	PyObject_HEAD
! 	int ob_size;
  	char *ob_item;
  	struct arraydescr *ob_descr;
***************
*** 35,39 ****
  staticforward PyTypeObject Arraytype;
  
! #define is_arrayobject(op) ((op)->ob_type == &Arraytype)
  
  /****************************************************************************
--- 36,41 ----
  staticforward PyTypeObject Arraytype;
  
! #define array_Check(op) PyObject_TypeCheck(op, &Arraytype)
! #define array_CheckExact(op) ((op)->ob_type == &Arraytype)
  
  /****************************************************************************
***************
*** 62,66 ****
  		return -1;
  	if (i >= 0)
! 		     ((char *)ap->ob_item)[i] = x;
  	return 0;
  }
--- 64,68 ----
  		return -1;
  	if (i >= 0)
! 		((char *)ap->ob_item)[i] = x;
  	return 0;
  }
***************
*** 114,121 ****
  		return -1;
  	if (i >= 0)
! 		     ((char *)ap->ob_item)[i] = x;
  	return 0;
  }
  
  static PyObject *
  h_getitem(arrayobject *ap, int i)
--- 116,148 ----
  		return -1;
  	if (i >= 0)
! 		((char *)ap->ob_item)[i] = x;
  	return 0;
  }
  
+ #ifdef Py_USING_UNICODE
+ static PyObject *
+ u_getitem(arrayobject *ap, int i)
+ {
+ 	return PyUnicode_FromUnicode(&((Py_UNICODE *) ap->ob_item)[i], 1);
+ }
+ 
+ static int
+ u_setitem(arrayobject *ap, int i, PyObject *v)
+ {
+ 	Py_UNICODE *p;
+ 	int len;
+ 
+ 	if (!PyArg_Parse(v, "u#;array item must be unicode character", &p, &len))
+ 		return -1;
+ 	if (len != 1) {
+ 		PyErr_SetString(PyExc_TypeError, "array item must be unicode character");
+ 		return -1;
+ 	}
+ 	if (i >= 0)
+ 		((Py_UNICODE *)ap->ob_item)[i] = p[0];
+ 	return 0;
+ }
+ #endif
+ 
  static PyObject *
  h_getitem(arrayobject *ap, int i)
***************
*** 316,319 ****
--- 343,349 ----
  	{'b', sizeof(char), b_getitem, b_setitem},
  	{'B', sizeof(char), BB_getitem, BB_setitem},
+ #ifdef Py_USING_UNICODE
+ 	{'u', sizeof(Py_UNICODE), u_getitem, u_setitem},
+ #endif
  	{'h', sizeof(short), h_getitem, h_setitem},
  	{'H', sizeof(short), HH_getitem, HH_setitem},
***************
*** 332,343 ****
  
  static PyObject *
! newarrayobject(int size, struct arraydescr *descr)
  {
  	arrayobject *op;
  	size_t nbytes;
  	if (size < 0) {
  		PyErr_BadInternalCall();
  		return NULL;
  	}
  	nbytes = size * descr->itemsize;
  	/* Check for overflow */
--- 362,375 ----
  
  static PyObject *
! newarrayobject(PyTypeObject *type, int size, struct arraydescr *descr)
  {
  	arrayobject *op;
  	size_t nbytes;
+ 
  	if (size < 0) {
  		PyErr_BadInternalCall();
  		return NULL;
  	}
+ 
  	nbytes = size * descr->itemsize;
  	/* Check for overflow */
***************
*** 345,352 ****
  		return PyErr_NoMemory();
  	}
! 	op = PyObject_NewVar(arrayobject, &Arraytype, size);
  	if (op == NULL) {
! 		return PyErr_NoMemory();
  	}
  	if (size <= 0) {
  		op->ob_item = NULL;
--- 377,385 ----
  		return PyErr_NoMemory();
  	}
! 	op = (arrayobject *) type->tp_alloc(type, 0);
  	if (op == NULL) {
! 		return NULL;
  	}
+ 	op->ob_size = size;
  	if (size <= 0) {
  		op->ob_item = NULL;
***************
*** 367,371 ****
  {
  	register arrayobject *ap;
! 	assert(is_arrayobject(op));
  	ap = (arrayobject *)op;
  	if (i < 0 || i >= ap->ob_size) {
--- 400,404 ----
  {
  	register arrayobject *ap;
! 	assert(array_Check(op));
  	ap = (arrayobject *)op;
  	if (i < 0 || i >= ap->ob_size) {
***************
*** 412,416 ****
  	if (op->ob_item != NULL)
  		PyMem_DEL(op->ob_item);
! 	PyObject_Del(op);
  }
  
--- 445,449 ----
  	if (op->ob_item != NULL)
  		PyMem_DEL(op->ob_item);
! 	op->ob_type->tp_free((PyObject *)op);
  }
  
***************
*** 424,428 ****
  	PyObject *res;
  
! 	if (!is_arrayobject(v) || !is_arrayobject(w)) {
  		Py_INCREF(Py_NotImplemented);
  		return Py_NotImplemented;
--- 457,461 ----
  	PyObject *res;
  
! 	if (!array_Check(v) || !array_Check(w)) {
  		Py_INCREF(Py_NotImplemented);
  		return Py_NotImplemented;
***************
*** 531,535 ****
  	else if (ihigh > a->ob_size)
  		ihigh = a->ob_size;
! 	np = (arrayobject *) newarrayobject(ihigh - ilow, a->ob_descr);
  	if (np == NULL)
  		return NULL;
--- 564,568 ----
  	else if (ihigh > a->ob_size)
  		ihigh = a->ob_size;
! 	np = (arrayobject *) newarrayobject(&Arraytype, ihigh - ilow, a->ob_descr);
  	if (np == NULL)
  		return NULL;
***************
*** 544,548 ****
  	int size;
  	arrayobject *np;
! 	if (!is_arrayobject(bb)) {
  		PyErr_Format(PyExc_TypeError,
  		     "can only append array (not \"%.200s\") to array",
--- 577,581 ----
  	int size;
  	arrayobject *np;
! 	if (!array_Check(bb)) {
  		PyErr_Format(PyExc_TypeError,
  		     "can only append array (not \"%.200s\") to array",
***************
*** 556,560 ****
  	}
  	size = a->ob_size + b->ob_size;
! 	np = (arrayobject *) newarrayobject(size, a->ob_descr);
  	if (np == NULL) {
  		return NULL;
--- 589,593 ----
  	}
  	size = a->ob_size + b->ob_size;
! 	np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr);
  	if (np == NULL) {
  		return NULL;
***************
*** 578,582 ****
  		n = 0;
  	size = a->ob_size * n;
! 	np = (arrayobject *) newarrayobject(size, a->ob_descr);
  	if (np == NULL)
  		return NULL;
--- 611,615 ----
  		n = 0;
  	size = a->ob_size * n;
! 	np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr);
  	if (np == NULL)
  		return NULL;
***************
*** 599,603 ****
  	if (v == NULL)
  		n = 0;
! 	else if (is_arrayobject(v)) {
  		n = b->ob_size;
  		if (a == b) {
--- 632,636 ----
  	if (v == NULL)
  		n = 0;
! 	else if (array_Check(v)) {
  		n = b->ob_size;
  		if (a == b) {
***************
*** 677,684 ****
  setarrayitem(PyObject *a, int i, PyObject *v)
  {
! 	assert(is_arrayobject(a));
  	return array_ass_item((arrayobject *)a, i, v);
  }
  
  static PyObject *
  ins(arrayobject *self, int where, PyObject *v)
--- 710,792 ----
  setarrayitem(PyObject *a, int i, PyObject *v)
  {
! 	assert(array_Check(a));
  	return array_ass_item((arrayobject *)a, i, v);
  }
  
+ static int
+ array_do_extend(arrayobject *self, PyObject *bb)
+ {
+ 	int size;
+ 
+ 	if (!array_Check(bb)) {
+ 		PyErr_Format(PyExc_TypeError,
+ 			"can only extend array with array (not \"%.200s\")",
+ 			bb->ob_type->tp_name);
+ 		return -1;
+ 	}
+ #define b ((arrayobject *)bb)
+ 	if (self->ob_descr != b->ob_descr) {
+ 		PyErr_SetString(PyExc_TypeError,
+ 			     "can only extend with array of same kind");
+ 		return -1;
+ 	}
+ 	size = self->ob_size + b->ob_size;
+         PyMem_RESIZE(self->ob_item, char, size*self->ob_descr->itemsize);
+         if (self->ob_item == NULL) {
+                 PyObject_Del(self);
+                 PyErr_NoMemory();
+ 		return -1;
+         }
+ 	memcpy(self->ob_item + self->ob_size*self->ob_descr->itemsize,
+                b->ob_item, b->ob_size*b->ob_descr->itemsize);
+         self->ob_size = size;
+ 
+ 	return 0;
+ #undef b
+ }
+ 
+ static PyObject *
+ array_inplace_concat(arrayobject *self, PyObject *bb)
+ {
+ 	if (array_do_extend(self, bb) == -1)
+ 		return NULL;
+ 	Py_INCREF(self);
+ 	return (PyObject *)self;
+ }
+ 
+ static PyObject *
+ array_inplace_repeat(arrayobject *self, int n)
+ {
+ 	char *items, *p;
+ 	int size, i;
+ 
+ 	if (self->ob_size > 0) {
+ 		if (n < 0)
+ 			n = 0;
+ 		items = self->ob_item;
+ 		size = self->ob_size * self->ob_descr->itemsize;
+ 		if (n == 0) {
+ 			PyMem_FREE(items);
+ 			self->ob_item = NULL;
+ 			self->ob_size = 0;
+ 		}
+ 		else {
+ 			PyMem_Resize(items, char, n * size);
+ 			if (items == NULL)
+ 				return PyErr_NoMemory();
+ 			p = items;
+ 			for (i = 1; i < n; i++) {
+ 				p += size;
+ 				memcpy(p, items, size);
+ 			}
+ 			self->ob_item = items;
+ 			self->ob_size *= n;
+ 		}
+ 	}
+ 	Py_INCREF(self);
+ 	return (PyObject *)self;
+ }
+ 
+ 
  static PyObject *
  ins(arrayobject *self, int where, PyObject *v)
***************
*** 808,841 ****
  array_extend(arrayobject *self, PyObject *args)
  {
- 	int size;
          PyObject    *bb;
  
  	if (!PyArg_ParseTuple(args, "O:extend", &bb))
-             return NULL;
- 
- 	if (!is_arrayobject(bb)) {
- 		PyErr_Format(PyExc_TypeError,
- 			"can only extend array with array (not \"%.200s\")",
- 			bb->ob_type->tp_name);
  		return NULL;
! 	}
! #define b ((arrayobject *)bb)
! 	if (self->ob_descr != b->ob_descr) {
! 		PyErr_SetString(PyExc_TypeError,
! 			     "can only extend with array of same kind");
  		return NULL;
! 	}
! 	size = self->ob_size + b->ob_size;
!         PyMem_RESIZE(self->ob_item, char, size*self->ob_descr->itemsize);
!         if (self->ob_item == NULL) {
!                 PyObject_Del(self);
!                 return PyErr_NoMemory();
!         }
! 	memcpy(self->ob_item + self->ob_size*self->ob_descr->itemsize,
!                b->ob_item, b->ob_size*b->ob_descr->itemsize);
!         self->ob_size = size;
!         Py_INCREF(Py_None);
  	return Py_None;
- #undef b
  }
  
--- 916,927 ----
  array_extend(arrayobject *self, PyObject *args)
  {
          PyObject    *bb;
  
  	if (!PyArg_ParseTuple(args, "O:extend", &bb))
  		return NULL;
! 	if (array_do_extend(self, bb) == -1)
  		return NULL;
! 	Py_INCREF(Py_None);
  	return Py_None;
  }
  
***************
*** 1204,1207 ****
--- 1290,1381 ----
  representation.";
  
+ 
+ 
+ #ifdef Py_USING_UNICODE
+ static PyObject *
+ array_fromunicode(arrayobject *self, PyObject *args)
+ {
+ 	Py_UNICODE *ustr;
+ 	int n;
+ 
+         if (!PyArg_ParseTuple(args, "u#:fromunicode", &ustr, &n))
+ 		return NULL;
+ 	if (self->ob_descr->typecode != 'u') {
+ 		PyErr_SetString(PyExc_ValueError,
+ 			"fromunicode() may only be called on "
+ 			"type 'u' arrays");
+ 		return NULL;
+ 	}
+ 	if (n > 0) {
+ 		Py_UNICODE *item = (Py_UNICODE *) self->ob_item;
+ 		PyMem_RESIZE(item, Py_UNICODE, self->ob_size + n);
+ 		if (item == NULL) {
+ 			PyErr_NoMemory();
+ 			return NULL;
+ 		}
+ 		self->ob_item = (char *) item;
+ 		self->ob_size += n;
+ 		memcpy(item + self->ob_size - n,
+ 		       ustr, n * sizeof(Py_UNICODE));
+ 	}
+ 
+ 	Py_INCREF(Py_None);
+ 	return Py_None;
+ }
+ 
+ static char fromunicode_doc[] =
+ "fromunicode(ustr)\n\
+ \n\
+ Extends this array with data from the unicode string ustr.\n\
+ The array must be a type 'u' array; otherwise a ValueError\n\
+ is raised.  Use array.fromstring(ustr.decode(...)) to\n\
+ append Unicode data to an array of some other type.";
+ 
+ 
+ static PyObject *
+ array_tounicode(arrayobject *self, PyObject *args)
+ {
+ 	if (!PyArg_ParseTuple(args, ":tounicode"))
+ 		return NULL;
+ 	if (self->ob_descr->typecode != 'u') {
+ 		PyErr_SetString(PyExc_ValueError,
+ 			"tounicode() may only be called on type 'u' arrays");
+ 		return NULL;
+ 	}
+ 	return PyUnicode_FromUnicode((Py_UNICODE *) self->ob_item, self->ob_size);
+ }
+ 
+ static char tounicode_doc [] =
+ "tounicode() -> unicode\n\
+ \n\
+ Convert the array to a unicode string.  The array must be\n\
+ a type 'u' array; otherwise a ValueError is raised.  Use\n\
+ array.tostring().decode() to obtain a unicode string from\n\
+ an array of some other type.";
+ 
+ #endif /* Py_USING_UNICODE */
+ 
+ 
+ static PyObject *
+ array_get_typecode(arrayobject *a, void *closure)
+ {
+ 	char tc = a->ob_descr->typecode;
+ 	return PyString_FromStringAndSize(&tc, 1);
+ }
+ 
+ static PyObject *
+ array_get_itemsize(arrayobject *a, void *closure)
+ {
+ 	return PyInt_FromLong((long)a->ob_descr->itemsize);
+ }
+ 
+ static PyGetSetDef array_getsets [] = {
+ 	{"typecode", (getter) array_get_typecode, NULL,
+ 	 "the typecode character used to create the array"},
+ 	{"itemsize", (getter) array_get_itemsize, NULL,
+ 	 "the size, in bytes, of one array item"},
+ 	{NULL}
+ };
+ 
  PyMethodDef array_methods[] = {
  	{"append",	(PyCFunction)array_append,	METH_VARARGS,
***************
*** 1221,1224 ****
--- 1395,1402 ----
  	{"fromstring",	(PyCFunction)array_fromstring,	METH_VARARGS,
  	 fromstring_doc},
+ #ifdef Py_USING_UNICODE
+ 	{"fromunicode",	(PyCFunction)array_fromunicode,	METH_VARARGS,
+ 	 fromunicode_doc},
+ #endif
  	{"index",	(PyCFunction)array_index,	METH_VARARGS,
  	 index_doc},
***************
*** 1241,1244 ****
--- 1419,1426 ----
  	{"tostring",	(PyCFunction)array_tostring,	METH_VARARGS,
  	 tostring_doc},
+ #ifdef Py_USING_UNICODE
+ 	{"tounicode",   (PyCFunction)array_tounicode,	METH_VARARGS,
+ 	 tounicode_doc},
+ #endif
  	{"write",	(PyCFunction)array_tofile,	METH_VARARGS,
  	 tofile_doc},
***************
*** 1246,1276 ****
  };
  
- static PyObject *
- array_getattr(arrayobject *a, char *name)
- {
- 	if (strcmp(name, "typecode") == 0) {
- 		char tc = a->ob_descr->typecode;
- 		return PyString_FromStringAndSize(&tc, 1);
- 	}
- 	if (strcmp(name, "itemsize") == 0) {
- 		return PyInt_FromLong((long)a->ob_descr->itemsize);
- 	}
- 	if (strcmp(name, "__members__") == 0) {
- 		PyObject *list = PyList_New(2);
- 		if (list) {
- 			PyList_SetItem(list, 0,
- 				       PyString_FromString("typecode"));
- 			PyList_SetItem(list, 1,
- 				       PyString_FromString("itemsize"));
- 			if (PyErr_Occurred()) {
- 				Py_DECREF(list);
- 				list = NULL;
- 			}
- 		}
- 		return list;
- 	}
- 	return Py_FindMethod(array_methods, (PyObject *)a, name);
- }
- 
  static int
  array_print(arrayobject *a, FILE *fp, int flags)
--- 1428,1431 ----
***************
*** 1309,1326 ****
  array_repr(arrayobject *a)
  {
! 	char buf[256];
  	PyObject *s, *t, *comma, *v;
  	int i, len;
  	len = a->ob_size;
  	if (len == 0) {
! 		PyOS_snprintf(buf, sizeof(buf), "array('%c')",
! 			      a->ob_descr->typecode);
  		return PyString_FromString(buf);
  	}
! 	if (a->ob_descr->typecode == 'c') {
  		PyObject *t_empty = PyTuple_New(0);
! 		PyOS_snprintf(buf, sizeof(buf), "array('c', ");
  		s = PyString_FromString(buf);
! 		v = array_tostring(a, t_empty);
  		Py_DECREF(t_empty);
  		t = PyObject_Repr(v);
--- 1464,1486 ----
  array_repr(arrayobject *a)
  {
! 	char buf[256], typecode;
  	PyObject *s, *t, *comma, *v;
  	int i, len;
+ 
  	len = a->ob_size;
+ 	typecode = a->ob_descr->typecode;
  	if (len == 0) {
! 		PyOS_snprintf(buf, sizeof(buf), "array('%c')", typecode);
  		return PyString_FromString(buf);
  	}
!         
! 	if (typecode == 'c' || typecode == 'u') {
  		PyObject *t_empty = PyTuple_New(0);
! 		PyOS_snprintf(buf, sizeof(buf), "array('%c', ", typecode);
  		s = PyString_FromString(buf);
! 		if (typecode == 'c')
! 			v = array_tostring(a, t_empty);
! 		else
! 			v = array_tounicode(a, t_empty);
  		Py_DECREF(t_empty);
  		t = PyObject_Repr(v);
***************
*** 1330,1334 ****
  		return s;
  	}
! 	PyOS_snprintf(buf, sizeof(buf), "array('%c', [", a->ob_descr->typecode);
  	s = PyString_FromString(buf);
  	comma = PyString_FromString(", ");
--- 1490,1494 ----
  		return s;
  	}
! 	PyOS_snprintf(buf, sizeof(buf), "array('%c', [", typecode);
  	s = PyString_FromString(buf);
  	comma = PyString_FromString(", ");
***************
*** 1386,1389 ****
--- 1546,1552 ----
  	(intobjargproc)array_ass_item,		/*sq_ass_item*/
  	(intintobjargproc)array_ass_slice,	/*sq_ass_slice*/
+ 	NULL,					/*sq_contains*/
+ 	(binaryfunc)array_inplace_concat,	/*sq_inplace_concat*/
+ 	(intargfunc)array_inplace_repeat	/*sq_inplace_repeat*/
  };
  
***************
*** 1395,1413 ****
  
  static PyObject *
! a_array(PyObject *self, PyObject *args)
  {
  	char c;
  	PyObject *initial = NULL;
  	struct arraydescr *descr;
!         if (!PyArg_ParseTuple(args, "c:array", &c)) {
! 		PyErr_Clear();
!                 if (!PyArg_ParseTuple(args, "cO:array", &c, &initial))
  			return NULL;
! 		if (!PyList_Check(initial) && !PyString_Check(initial)) {
  			PyErr_SetString(PyExc_TypeError,
! 				 "array initializer must be list or string");
  			return NULL;
  		}
  	}
  	for (descr = descriptors; descr->typecode != '\0'; descr++) {
  		if (descr->typecode == c) {
--- 1558,1589 ----
  
  static PyObject *
! array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
  {
  	char c;
  	PyObject *initial = NULL;
  	struct arraydescr *descr;
! 
! 	if (kwds != NULL) {
! 		int i = PyObject_Length(kwds);
! 		if (i < 0)
  			return NULL;
! 		else if (i > 0) {
  			PyErr_SetString(PyExc_TypeError,
! 			    "array.array constructor takes "
! 			    "no keyword arguments");
  			return NULL;
  		}
  	}
+ 
+ 	if (!PyArg_ParseTuple(args, "c|O:array", &c, &initial))
+ 		return NULL;
+ 
+ 	if (!(initial == NULL || PyList_Check(initial)
+ 	      || PyString_Check(initial)
+ 	      || (c == 'u' && PyUnicode_Check(initial)))) {
+ 		PyErr_SetString(PyExc_TypeError,
+ 		    "array initializer must be list or string");
+ 		return NULL;
+ 	}
  	for (descr = descriptors; descr->typecode != '\0'; descr++) {
  		if (descr->typecode == c) {
***************
*** 1418,1429 ****
  			else
  				len = PyList_Size(initial);
! 			a = newarrayobject(len, descr);
  			if (a == NULL)
  				return NULL;
  			if (len > 0) {
  				int i;
  				for (i = 0; i < len; i++) {
  					PyObject *v =
! 					         PyList_GetItem(initial, i);
  					if (setarrayitem(a, i, v) != 0) {
  						Py_DECREF(a);
--- 1594,1607 ----
  			else
  				len = PyList_Size(initial);
! 
! 			a = newarrayobject(type, len, descr);
  			if (a == NULL)
  				return NULL;
+ 
  			if (len > 0) {
  				int i;
  				for (i = 0; i < len; i++) {
  					PyObject *v =
! 					        PyList_GetItem(initial, i);
  					if (setarrayitem(a, i, v) != 0) {
  						Py_DECREF(a);
***************
*** 1438,1442 ****
  					array_fromstring((arrayobject *)a,
  							 t_initial);
!                                 Py_DECREF(t_initial);
  				if (v == NULL) {
  					Py_DECREF(a);
--- 1616,1620 ----
  					array_fromstring((arrayobject *)a,
  							 t_initial);
! 				Py_DECREF(t_initial);
  				if (v == NULL) {
  					Py_DECREF(a);
***************
*** 1444,1447 ****
--- 1622,1642 ----
  				}
  				Py_DECREF(v);
+ #ifdef Py_USING_UNICODE
+ 			} else if (initial != NULL && PyUnicode_Check(initial))  {
+ 				int n = PyUnicode_GET_DATA_SIZE(initial);
+ 				if (n > 0) {
+ 					arrayobject *self = (arrayobject *)a;
+ 					char *item = self->ob_item;
+ 					item = PyMem_Realloc(item, n);
+ 					if (item == NULL) {
+ 						PyErr_NoMemory();
+ 						Py_DECREF(a);
+ 						return NULL;
+ 					}
+ 					self->ob_item = item;
+ 					self->ob_size = n / sizeof(Py_UNICODE);
+ 					memcpy(item, PyUnicode_AS_DATA(initial), n);
+ 				}
+ #endif
  			}
  			return a;
***************
*** 1449,1470 ****
  	}
  	PyErr_SetString(PyExc_ValueError,
! 		"bad typecode (must be c, b, B, h, H, i, I, l, L, f or d)");
  	return NULL;
  }
  
- static char a_array_doc [] =
- "array(typecode [, initializer]) -> array\n\
- \n\
- Return a new array whose items are restricted by typecode, and\n\
- initialized from the optional initializer value, which must be a list\n\
- or a string.";
- 
- static PyMethodDef a_methods[] = {
- 	{"array",	a_array, METH_VARARGS, a_array_doc},
- 	{NULL,		NULL}		/* sentinel */
- };
  
  static char module_doc [] =
! "This module defines a new object type which can efficiently represent\n\
  an array of basic values: characters, integers, floating point\n\
  numbers.  Arrays are sequence types and behave very much like lists,\n\
--- 1644,1654 ----
  	}
  	PyErr_SetString(PyExc_ValueError,
! 		"bad typecode (must be c, b, B, u, h, H, i, I, l, L, f or d)");
  	return NULL;
  }
  
  
  static char module_doc [] =
! "This module defines an object type which can efficiently represent\n\
  an array of basic values: characters, integers, floating point\n\
  numbers.  Arrays are sequence types and behave very much like lists,\n\
***************
*** 1477,1480 ****
--- 1661,1665 ----
      'b'         signed integer     1 \n\
      'B'         unsigned integer   1 \n\
+     'u'         Unicode character  2 \n\
      'h'         signed integer     2 \n\
      'H'         unsigned integer   2 \n\
***************
*** 1486,1500 ****
      'd'         floating point     8 \n\
  \n\
! Functions:\n\
  \n\
  array(typecode [, initializer]) -- create a new array\n\
- \n\
- Special Objects:\n\
- \n\
- ArrayType -- type object for array objects\n\
  ";
  
  static char arraytype_doc [] =
! "An array represents basic values and behave very much like lists, except\n\
  the type of objects stored in them is constrained.\n\
  \n\
--- 1671,1687 ----
      'd'         floating point     8 \n\
  \n\
! The constructor is:\n\
  \n\
  array(typecode [, initializer]) -- create a new array\n\
  ";
  
  static char arraytype_doc [] =
! "array(typecode [, initializer]) -> array\n\
! \n\
! Return a new array whose items are restricted by typecode, and\n\
! initialized from the optional initializer value, which must be a list\n\
! or a string.\n\
! \n\
! Arrays represent basic values and behave very much like lists, except\n\
  the type of objects stored in them is constrained.\n\
  \n\
***************
*** 1520,1524 ****
  write() -- DEPRECATED, use tofile()\n\
  \n\
! Variables:\n\
  \n\
  typecode -- the typecode character used to create the array\n\
--- 1707,1711 ----
  write() -- DEPRECATED, use tofile()\n\
  \n\
! Attributes:\n\
  \n\
  typecode -- the typecode character used to create the array\n\
***************
*** 1534,1538 ****
  	(destructor)array_dealloc,		/* tp_dealloc */
  	(printfunc)array_print,			/* tp_print */
! 	(getattrfunc)array_getattr,		/* tp_getattr */
  	0,					/* tp_setattr */
  	0,					/* tp_compare */
--- 1721,1725 ----
  	(destructor)array_dealloc,		/* tp_dealloc */
  	(printfunc)array_print,			/* tp_print */
! 	0,					/* tp_getattr */
  	0,					/* tp_setattr */
  	0,					/* tp_compare */
***************
*** 1544,1557 ****
  	0,					/* tp_call */
  	0,					/* tp_str */
! 	0,					/* tp_getattro */
  	0,					/* tp_setattro */
  	&array_as_buffer,			/* tp_as_buffer*/
! 	Py_TPFLAGS_DEFAULT,			/* tp_flags */
  	arraytype_doc,				/* tp_doc */
   	0,					/* tp_traverse */
  	0,					/* tp_clear */
  	array_richcompare,			/* tp_richcompare */
  };
  
  DL_EXPORT(void)
  initarray(void)
--- 1731,1765 ----
  	0,					/* tp_call */
  	0,					/* tp_str */
! 	PyObject_GenericGetAttr,		/* tp_getattro */
  	0,					/* tp_setattro */
  	&array_as_buffer,			/* tp_as_buffer*/
! 	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,  /* tp_flags */
  	arraytype_doc,				/* tp_doc */
   	0,					/* tp_traverse */
  	0,					/* tp_clear */
  	array_richcompare,			/* tp_richcompare */
+ 	0,					/* tp_weaklistoffset */
+ 	0,					/* tp_iter */
+ 	0,					/* tp_iternext */
+ 	array_methods,				/* tp_methods */
+ 	0,					/* tp_members */
+ 	array_getsets,				/* tp_getset */
+ 	0,					/* tp_base */
+ 	0,					/* tp_dict */
+ 	0,					/* tp_descr_get */
+ 	0,					/* tp_descr_set */
+ 	0,					/* tp_dictoffset */
+ 	0,					/* tp_init */
+ 	PyType_GenericAlloc,			/* tp_alloc */
+ 	array_new,				/* tp_new */
+ 	_PyObject_Del,				/* tp_free */
  };
  
+ /* No functions in array module. */
+ static PyMethodDef a_methods[] = {
+     {NULL, NULL, 0, NULL}        /* Sentinel */
+ };
+ 
+ 
  DL_EXPORT(void)
  initarray(void)
***************
*** 1559,1566 ****
  	PyObject *m, *d;
  
!         Arraytype.ob_type = &PyType_Type;
  	m = Py_InitModule3("array", a_methods, module_doc);
  	d = PyModule_GetDict(m);
  	PyDict_SetItemString(d, "ArrayType", (PyObject *)&Arraytype);
  	/* No need to check the error here, the caller will do that */
  }
--- 1767,1775 ----
  	PyObject *m, *d;
  
! 	Arraytype.ob_type = &PyType_Type;
  	m = Py_InitModule3("array", a_methods, module_doc);
  	d = PyModule_GetDict(m);
  	PyDict_SetItemString(d, "ArrayType", (PyObject *)&Arraytype);
+ 	PyDict_SetItemString(d, "array", (PyObject *)&Arraytype);
  	/* No need to check the error here, the caller will do that */
  }