comparison Modules/arraymodule.c @ 42675:cd4591b754e0

Fix memory leak in arraymodule.c and respond to a few comments by nnorwitz.
author Travis E. Oliphant <oliphant@enthought.com>
date Mon, 20 Aug 2007 07:16:33 +0000
parents 2c51be9904de
children f3b3ffd13954
comparison
equal deleted inserted replaced
42673:692f57a5dc76 42675:cd4591b754e0
24 struct arraydescr { 24 struct arraydescr {
25 int typecode; 25 int typecode;
26 int itemsize; 26 int itemsize;
27 PyObject * (*getitem)(struct arrayobject *, Py_ssize_t); 27 PyObject * (*getitem)(struct arrayobject *, Py_ssize_t);
28 int (*setitem)(struct arrayobject *, Py_ssize_t, PyObject *); 28 int (*setitem)(struct arrayobject *, Py_ssize_t, PyObject *);
29 const char *formats; 29 char *formats;
30 }; 30 };
31 31
32 typedef struct arrayobject { 32 typedef struct arrayobject {
33 PyObject_VAR_HEAD 33 PyObject_VAR_HEAD
34 char *ob_item; 34 char *ob_item;
40 40
41 static PyTypeObject Arraytype; 41 static PyTypeObject Arraytype;
42 42
43 #ifdef Py_UNICODE_WIDE 43 #ifdef Py_UNICODE_WIDE
44 #define PyArr_UNI 'w' 44 #define PyArr_UNI 'w'
45 #define PyArr_UNISTR "w"
45 #else 46 #else
46 #define PyArr_UNI 'u' 47 #define PyArr_UNI 'u'
48 #define PyArr_UNISTR "u"
47 #endif 49 #endif
48 50
49 #define array_Check(op) PyObject_TypeCheck(op, &Arraytype) 51 #define array_Check(op) PyObject_TypeCheck(op, &Arraytype)
50 #define array_CheckExact(op) (Py_Type(op) == &Arraytype) 52 #define array_CheckExact(op) (Py_Type(op) == &Arraytype)
51 53
385 } 387 }
386 388
387 389
388 /* Description of types */ 390 /* Description of types */
389 static struct arraydescr descriptors[] = { 391 static struct arraydescr descriptors[] = {
390 {'b', sizeof(char), b_getitem, b_setitem}, 392 {'b', sizeof(char), b_getitem, b_setitem, "b"},
391 {'B', sizeof(char), BB_getitem, BB_setitem}, 393 {'B', sizeof(char), BB_getitem, BB_setitem, "B"},
392 {PyArr_UNI, sizeof(Py_UNICODE), u_getitem, u_setitem}, 394 {PyArr_UNI, sizeof(Py_UNICODE), u_getitem, u_setitem, PyArr_UNISTR},
393 {'h', sizeof(short), h_getitem, h_setitem}, 395 {'h', sizeof(short), h_getitem, h_setitem, "h"},
394 {'H', sizeof(short), HH_getitem, HH_setitem}, 396 {'H', sizeof(short), HH_getitem, HH_setitem, "H"},
395 {'i', sizeof(int), i_getitem, i_setitem}, 397 {'i', sizeof(int), i_getitem, i_setitem, "i"},
396 {'I', sizeof(int), II_getitem, II_setitem}, 398 {'I', sizeof(int), II_getitem, II_setitem, "I"},
397 {'l', sizeof(long), l_getitem, l_setitem}, 399 {'l', sizeof(long), l_getitem, l_setitem, "l"},
398 {'L', sizeof(long), LL_getitem, LL_setitem}, 400 {'L', sizeof(long), LL_getitem, LL_setitem, "L"},
399 {'f', sizeof(float), f_getitem, f_setitem}, 401 {'f', sizeof(float), f_getitem, f_setitem, "f"},
400 {'d', sizeof(double), d_getitem, d_setitem}, 402 {'d', sizeof(double), d_getitem, d_setitem, "d"},
401 {'\0', 0, 0, 0} /* Sentinel */ 403 {'\0', 0, 0, 0, 0} /* Sentinel */
402 }; 404 };
403 405
404 /**************************************************************************** 406 /****************************************************************************
405 Implementations of array object methods. 407 Implementations of array object methods.
406 ****************************************************************************/ 408 ****************************************************************************/
1768 if ((flags & PyBUF_STRIDES)==PyBUF_STRIDES) 1770 if ((flags & PyBUF_STRIDES)==PyBUF_STRIDES)
1769 view->strides = &(view->itemsize); 1771 view->strides = &(view->itemsize);
1770 view->format = NULL; 1772 view->format = NULL;
1771 view->internal = NULL; 1773 view->internal = NULL;
1772 if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) { 1774 if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) {
1773 view->internal = malloc(3); 1775 view->format = self->ob_descr->formats;
1774 /* XXX(nnorwitz): need to check for malloc failure.
1775 Should probably use PyObject_Malloc. */
1776 view->format = view->internal;
1777 view->format[0] = (char)(self->ob_descr->typecode);
1778 view->format[1] = '\0';
1779 } 1776 }
1780 1777
1781 finish: 1778 finish:
1782 self->ob_exports++; 1779 self->ob_exports++;
1783 return 0; 1780 return 0;
1784 } 1781 }
1785 1782
1786 static void 1783 static void
1787 array_buffer_relbuf(arrayobject *self, PyBuffer *view) 1784 array_buffer_relbuf(arrayobject *self, PyBuffer *view)
1788 { 1785 {
1789 free(view->internal);
1790 self->ob_exports--; 1786 self->ob_exports--;
1791 } 1787 }
1792 1788
1793 static PySequenceMethods array_as_sequence = { 1789 static PySequenceMethods array_as_sequence = {
1794 (lenfunc)array_length, /*sq_length*/ 1790 (lenfunc)array_length, /*sq_length*/