summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTravis E. Oliphant <oliphant@enthought.com>2007-08-20 07:16:33 (GMT)
committerTravis E. Oliphant <oliphant@enthought.com>2007-08-20 07:16:33 (GMT)
commitb803c517bf6950cd91790b731d2bea32b7e9e03e (patch)
tree29b8a39711e8e88451718857547e0a949dfe6ca6
parent30d1c51ac97a93f68015a0c646bd130140b69a98 (diff)
downloadcpython-b803c517bf6950cd91790b731d2bea32b7e9e03e.zip
cpython-b803c517bf6950cd91790b731d2bea32b7e9e03e.tar.gz
cpython-b803c517bf6950cd91790b731d2bea32b7e9e03e.tar.bz2
Fix memory leak in arraymodule.c and respond to a few comments by nnorwitz.
-rw-r--r--Include/object.h5
-rw-r--r--Modules/arraymodule.c36
-rw-r--r--Objects/bufferobject.c1
3 files changed, 19 insertions, 23 deletions
diff --git a/Include/object.h b/Include/object.h
index dfabefd..9dd100f 100644
--- a/Include/object.h
+++ b/Include/object.h
@@ -145,9 +145,10 @@ typedef int(*objobjargproc)(PyObject *, PyObject *, PyObject *);
typedef struct bufferinfo {
void *buf;
Py_ssize_t len;
- Py_ssize_t itemsize;
+ Py_ssize_t itemsize; /* This is Py_ssize_t so it can be
+ pointed to by strides in simple case.*/
int readonly;
- int ndim; /* XXX(nnorwitz): should be Py_ssize_t??? */
+ int ndim;
char *format;
Py_ssize_t *shape;
Py_ssize_t *strides;
diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c
index 0e8673f..eb26698 100644
--- a/Modules/arraymodule.c
+++ b/Modules/arraymodule.c
@@ -26,7 +26,7 @@ struct arraydescr {
int itemsize;
PyObject * (*getitem)(struct arrayobject *, Py_ssize_t);
int (*setitem)(struct arrayobject *, Py_ssize_t, PyObject *);
- const char *formats;
+ char *formats;
};
typedef struct arrayobject {
@@ -42,8 +42,10 @@ static PyTypeObject Arraytype;
#ifdef Py_UNICODE_WIDE
#define PyArr_UNI 'w'
+#define PyArr_UNISTR "w"
#else
#define PyArr_UNI 'u'
+#define PyArr_UNISTR "u"
#endif
#define array_Check(op) PyObject_TypeCheck(op, &Arraytype)
@@ -387,18 +389,18 @@ d_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
/* Description of types */
static struct arraydescr descriptors[] = {
- {'b', sizeof(char), b_getitem, b_setitem},
- {'B', sizeof(char), BB_getitem, BB_setitem},
- {PyArr_UNI, sizeof(Py_UNICODE), u_getitem, u_setitem},
- {'h', sizeof(short), h_getitem, h_setitem},
- {'H', sizeof(short), HH_getitem, HH_setitem},
- {'i', sizeof(int), i_getitem, i_setitem},
- {'I', sizeof(int), II_getitem, II_setitem},
- {'l', sizeof(long), l_getitem, l_setitem},
- {'L', sizeof(long), LL_getitem, LL_setitem},
- {'f', sizeof(float), f_getitem, f_setitem},
- {'d', sizeof(double), d_getitem, d_setitem},
- {'\0', 0, 0, 0} /* Sentinel */
+ {'b', sizeof(char), b_getitem, b_setitem, "b"},
+ {'B', sizeof(char), BB_getitem, BB_setitem, "B"},
+ {PyArr_UNI, sizeof(Py_UNICODE), u_getitem, u_setitem, PyArr_UNISTR},
+ {'h', sizeof(short), h_getitem, h_setitem, "h"},
+ {'H', sizeof(short), HH_getitem, HH_setitem, "H"},
+ {'i', sizeof(int), i_getitem, i_setitem, "i"},
+ {'I', sizeof(int), II_getitem, II_setitem, "I"},
+ {'l', sizeof(long), l_getitem, l_setitem, "l"},
+ {'L', sizeof(long), LL_getitem, LL_setitem, "L"},
+ {'f', sizeof(float), f_getitem, f_setitem, "f"},
+ {'d', sizeof(double), d_getitem, d_setitem, "d"},
+ {'\0', 0, 0, 0, 0} /* Sentinel */
};
/****************************************************************************
@@ -1770,12 +1772,7 @@ array_buffer_getbuf(arrayobject *self, PyBuffer *view, int flags)
view->format = NULL;
view->internal = NULL;
if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) {
- view->internal = malloc(3);
- /* XXX(nnorwitz): need to check for malloc failure.
- Should probably use PyObject_Malloc. */
- view->format = view->internal;
- view->format[0] = (char)(self->ob_descr->typecode);
- view->format[1] = '\0';
+ view->format = self->ob_descr->formats;
}
finish:
@@ -1786,7 +1783,6 @@ array_buffer_getbuf(arrayobject *self, PyBuffer *view, int flags)
static void
array_buffer_relbuf(arrayobject *self, PyBuffer *view)
{
- free(view->internal);
self->ob_exports--;
}
diff --git a/Objects/bufferobject.c b/Objects/bufferobject.c
index ccd4980..c415013 100644
--- a/Objects/bufferobject.c
+++ b/Objects/bufferobject.c
@@ -64,7 +64,6 @@ buffer_releasebuf(PyBufferObject *self, PyBuffer *view)
(*bp->bf_releasebuffer)(self->b_base, view);
}
}
- /* XXX(nnorwitz): do we need to release view here? it leaks. */
}
static PyObject *