Skip to content

Commit 1b3373a

Browse files
author
travis.oliphant
committed
Fix memory leak in arraymodule.c and respond to a few comments by nnorwitz.
git-svn-id: http://svn.python.org/projects/python/branches/py3k@57212 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 6de0976 commit 1b3373a

3 files changed

Lines changed: 19 additions & 23 deletions

File tree

Include/object.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,10 @@ typedef int(*objobjargproc)(PyObject *, PyObject *, PyObject *);
145145
typedef struct bufferinfo {
146146
void *buf;
147147
Py_ssize_t len;
148-
Py_ssize_t itemsize;
148+
Py_ssize_t itemsize; /* This is Py_ssize_t so it can be
149+
pointed to by strides in simple case.*/
149150
int readonly;
150-
int ndim; /* XXX(nnorwitz): should be Py_ssize_t??? */
151+
int ndim;
151152
char *format;
152153
Py_ssize_t *shape;
153154
Py_ssize_t *strides;

Modules/arraymodule.c

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ struct arraydescr {
2626
int itemsize;
2727
PyObject * (*getitem)(struct arrayobject *, Py_ssize_t);
2828
int (*setitem)(struct arrayobject *, Py_ssize_t, PyObject *);
29-
const char *formats;
29+
char *formats;
3030
};
3131

3232
typedef struct arrayobject {
@@ -42,8 +42,10 @@ static PyTypeObject Arraytype;
4242

4343
#ifdef Py_UNICODE_WIDE
4444
#define PyArr_UNI 'w'
45+
#define PyArr_UNISTR "w"
4546
#else
4647
#define PyArr_UNI 'u'
48+
#define PyArr_UNISTR "u"
4749
#endif
4850

4951
#define array_Check(op) PyObject_TypeCheck(op, &Arraytype)
@@ -387,18 +389,18 @@ d_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
387389

388390
/* Description of types */
389391
static struct arraydescr descriptors[] = {
390-
{'b', sizeof(char), b_getitem, b_setitem},
391-
{'B', sizeof(char), BB_getitem, BB_setitem},
392-
{PyArr_UNI, sizeof(Py_UNICODE), u_getitem, u_setitem},
393-
{'h', sizeof(short), h_getitem, h_setitem},
394-
{'H', sizeof(short), HH_getitem, HH_setitem},
395-
{'i', sizeof(int), i_getitem, i_setitem},
396-
{'I', sizeof(int), II_getitem, II_setitem},
397-
{'l', sizeof(long), l_getitem, l_setitem},
398-
{'L', sizeof(long), LL_getitem, LL_setitem},
399-
{'f', sizeof(float), f_getitem, f_setitem},
400-
{'d', sizeof(double), d_getitem, d_setitem},
401-
{'\0', 0, 0, 0} /* Sentinel */
392+
{'b', sizeof(char), b_getitem, b_setitem, "b"},
393+
{'B', sizeof(char), BB_getitem, BB_setitem, "B"},
394+
{PyArr_UNI, sizeof(Py_UNICODE), u_getitem, u_setitem, PyArr_UNISTR},
395+
{'h', sizeof(short), h_getitem, h_setitem, "h"},
396+
{'H', sizeof(short), HH_getitem, HH_setitem, "H"},
397+
{'i', sizeof(int), i_getitem, i_setitem, "i"},
398+
{'I', sizeof(int), II_getitem, II_setitem, "I"},
399+
{'l', sizeof(long), l_getitem, l_setitem, "l"},
400+
{'L', sizeof(long), LL_getitem, LL_setitem, "L"},
401+
{'f', sizeof(float), f_getitem, f_setitem, "f"},
402+
{'d', sizeof(double), d_getitem, d_setitem, "d"},
403+
{'\0', 0, 0, 0, 0} /* Sentinel */
402404
};
403405

404406
/****************************************************************************
@@ -1770,12 +1772,7 @@ array_buffer_getbuf(arrayobject *self, PyBuffer *view, int flags)
17701772
view->format = NULL;
17711773
view->internal = NULL;
17721774
if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) {
1773-
view->internal = malloc(3);
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';
1775+
view->format = self->ob_descr->formats;
17791776
}
17801777

17811778
finish:
@@ -1786,7 +1783,6 @@ array_buffer_getbuf(arrayobject *self, PyBuffer *view, int flags)
17861783
static void
17871784
array_buffer_relbuf(arrayobject *self, PyBuffer *view)
17881785
{
1789-
free(view->internal);
17901786
self->ob_exports--;
17911787
}
17921788

Objects/bufferobject.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ buffer_releasebuf(PyBufferObject *self, PyBuffer *view)
6464
(*bp->bf_releasebuffer)(self->b_base, view);
6565
}
6666
}
67-
/* XXX(nnorwitz): do we need to release view here? it leaks. */
6867
}
6968

7069
static PyObject *

0 commit comments

Comments
 (0)