Skip to content

Commit

Permalink
Merge pull request numpy#5124 from larsmans/c-fixes
Browse files Browse the repository at this point in the history
FIX: core: remove sprintf calls
  • Loading branch information
charris committed Sep 30, 2014
2 parents 5eb6e36 + bfc4bf4 commit 6a4aa59
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 43 deletions.
79 changes: 50 additions & 29 deletions numpy/core/src/multiarray/arrayobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -429,37 +429,61 @@ array_dealloc(PyArrayObject *self)
Py_TYPE(self)->tp_free((PyObject *)self);
}

/*
* Extend string. On failure, returns NULL and leaves *strp alone.
* XXX we do this in multiple places; time for a string library?
*/
static char *
extend(char **strp, Py_ssize_t n, Py_ssize_t *maxp)
{
char *str = *strp;
Py_ssize_t new_cap;

if (n >= *maxp - 16) {
new_cap = *maxp * 2;

if (new_cap <= *maxp) { /* overflow */
return NULL;
}
str = PyArray_realloc(*strp, new_cap);
if (str != NULL) {
*strp = str;
*maxp = new_cap;
}
}
return str;
}

static int
dump_data(char **string, int *n, int *max_n, char *data, int nd,
dump_data(char **string, Py_ssize_t *n, Py_ssize_t *max_n, char *data, int nd,
npy_intp *dimensions, npy_intp *strides, PyArrayObject* self)
{
PyArray_Descr *descr=PyArray_DESCR(self);
PyObject *op, *sp;
PyObject *op = NULL, *sp = NULL;
char *ostring;
npy_intp i, N;
npy_intp i, N, ret = 0;

#define CHECK_MEMORY do { if (*n >= *max_n-16) { \
*max_n *= 2; \
*string = (char *)PyArray_realloc(*string, *max_n); \
}} while (0)
#define CHECK_MEMORY do { \
if (extend(string, *n, max_n) == NULL) { \
ret = -1; \
goto end; \
} \
} while (0)

if (nd == 0) {
if ((op = descr->f->getitem(data, self)) == NULL) {
return -1;
}
sp = PyObject_Repr(op);
if (sp == NULL) {
Py_DECREF(op);
return -1;
ret = -1;
goto end;
}
ostring = PyString_AsString(sp);
N = PyString_Size(sp)*sizeof(char);
*n += N;
CHECK_MEMORY;
memmove(*string + (*n - N), ostring, N);
Py_DECREF(sp);
Py_DECREF(op);
return 0;
}
else {
CHECK_MEMORY;
Expand All @@ -482,10 +506,14 @@ dump_data(char **string, int *n, int *max_n, char *data, int nd,
CHECK_MEMORY;
(*string)[*n] = ']';
*n += 1;
return 0;
}

#undef CHECK_MEMORY

end:
Py_XDECREF(op);
Py_XDECREF(sp);
return ret;
}

/*NUMPY_API
Expand Down Expand Up @@ -555,21 +583,13 @@ array_repr_builtin(PyArrayObject *self, int repr)
{
PyObject *ret;
char *string;
int n, max_n;

max_n = PyArray_NBYTES(self)*4*sizeof(char) + 7;
/* max_n initial value is arbitrary, dump_data will extend it */
Py_ssize_t n = 0, max_n = PyArray_NBYTES(self) * 4 + 7;

if ((string = PyArray_malloc(max_n)) == NULL) {
return PyErr_NoMemory();
}

if (repr) {
n = 6;
sprintf(string, "array(");
}
else {
n = 0;
}
if (dump_data(&string, &n, &max_n, PyArray_DATA(self),
PyArray_NDIM(self), PyArray_DIMS(self),
PyArray_STRIDES(self), self) < 0) {
Expand All @@ -579,14 +599,15 @@ array_repr_builtin(PyArrayObject *self, int repr)

if (repr) {
if (PyArray_ISEXTENDED(self)) {
char buf[100];
PyOS_snprintf(buf, sizeof(buf), "%d", PyArray_DESCR(self)->elsize);
sprintf(string+n, ", '%c%s')", PyArray_DESCR(self)->type, buf);
ret = PyUString_FromStringAndSize(string, n + 6 + strlen(buf));
ret = PyUString_FromFormat("array(%s, '%c%d')",
string,
PyArray_DESCR(self)->type,
PyArray_DESCR(self)->elsize);
}
else {
sprintf(string+n, ", '%c')", PyArray_DESCR(self)->type);
ret = PyUString_FromStringAndSize(string, n+6);
ret = PyUString_FromFormat("array(%s, '%c')",
string,
PyArray_DESCR(self)->type);
}
}
else {
Expand Down
18 changes: 4 additions & 14 deletions numpy/core/src/umath/ufunc_object.c
Original file line number Diff line number Diff line change
Expand Up @@ -743,16 +743,9 @@ _parse_signature(PyUFuncObject *ufunc, const char *signature)
fail:
PyArray_free((void*)var_names);
if (parse_error) {
char *buf = PyArray_malloc(sizeof(char) * (len + 200));
if (buf) {
sprintf(buf, "%s at position %d in \"%s\"",
parse_error, i, signature);
PyErr_SetString(PyExc_ValueError, signature);
PyArray_free(buf);
}
else {
PyErr_NoMemory();
}
PyErr_Format(PyExc_ValueError,
"%s at position %d in \"%s\"",
parse_error, i, signature);
}
return -1;
}
Expand Down Expand Up @@ -4729,10 +4722,7 @@ ufunc_dealloc(PyUFuncObject *ufunc)
static PyObject *
ufunc_repr(PyUFuncObject *ufunc)
{
char buf[100];

sprintf(buf, "<ufunc '%.50s'>", ufunc->name);
return PyUString_FromString(buf);
return PyUString_FromFormat("<ufunc '%s'>", ufunc->name);
}


Expand Down

0 comments on commit 6a4aa59

Please sign in to comment.