Fix leak in PyC_AsArray

Would only happen when the list-length was an unexpected size.

Also add PyC_AsArray_FAST
This commit is contained in:
Campbell Barton 2015-07-29 09:31:53 +10:00
parent 31cb14f5de
commit d226a4ba6d
2 changed files with 27 additions and 15 deletions

@ -48,21 +48,16 @@
#endif #endif
/* array utility function */ /* array utility function */
int PyC_AsArray(void *array, PyObject *value, const Py_ssize_t length, int PyC_AsArray_FAST(
void *array, PyObject *value_fast, const Py_ssize_t length,
const PyTypeObject *type, const bool is_double, const char *error_prefix) const PyTypeObject *type, const bool is_double, const char *error_prefix)
{ {
PyObject *value_fast; const Py_ssize_t value_len = PySequence_Fast_GET_SIZE(value_fast);
Py_ssize_t value_len;
Py_ssize_t i; Py_ssize_t i;
if (!(value_fast = PySequence_Fast(value, error_prefix))) { BLI_assert(PyList_Check(value_fast) || PyTuple_Check(value_fast));
return -1;
}
value_len = PySequence_Fast_GET_SIZE(value_fast);
if (value_len != length) { if (value_len != length) {
Py_DECREF(value);
PyErr_Format(PyExc_TypeError, PyErr_Format(PyExc_TypeError,
"%.200s: invalid sequence length. expected %d, got %d", "%.200s: invalid sequence length. expected %d, got %d",
error_prefix, length, value_len); error_prefix, length, value_len);
@ -98,15 +93,12 @@ int PyC_AsArray(void *array, PyObject *value, const Py_ssize_t length,
} }
} }
else { else {
Py_DECREF(value_fast);
PyErr_Format(PyExc_TypeError, PyErr_Format(PyExc_TypeError,
"%s: internal error %s is invalid", "%s: internal error %s is invalid",
error_prefix, type->tp_name); error_prefix, type->tp_name);
return -1; return -1;
} }
Py_DECREF(value_fast);
if (PyErr_Occurred()) { if (PyErr_Occurred()) {
PyErr_Format(PyExc_TypeError, PyErr_Format(PyExc_TypeError,
"%s: one or more items could not be used as a %s", "%s: one or more items could not be used as a %s",
@ -117,6 +109,22 @@ int PyC_AsArray(void *array, PyObject *value, const Py_ssize_t length,
return 0; return 0;
} }
int PyC_AsArray(
void *array, PyObject *value, const Py_ssize_t length,
const PyTypeObject *type, const bool is_double, const char *error_prefix)
{
PyObject *value_fast;
int ret;
if (!(value_fast = PySequence_Fast(value, error_prefix))) {
return -1;
}
ret = PyC_AsArray_FAST(array, value_fast, length, type, is_double, error_prefix);
Py_DECREF(value_fast);
return ret;
}
/* array utility function */ /* array utility function */
PyObject *PyC_FromArray(const void *array, int length, const PyTypeObject *type, PyObject *PyC_FromArray(const void *array, int length, const PyTypeObject *type,
const bool is_double, const char *error_prefix) const bool is_double, const char *error_prefix)

@ -38,7 +38,11 @@ PyObject * PyC_FrozenSetFromStrings(const char **strings);
PyObject * PyC_Err_Format_Prefix(PyObject *exception_type_prefix, const char *format, ...); PyObject * PyC_Err_Format_Prefix(PyObject *exception_type_prefix, const char *format, ...);
void PyC_FileAndNum(const char **filename, int *lineno); void PyC_FileAndNum(const char **filename, int *lineno);
void PyC_FileAndNum_Safe(const char **filename, int *lineno); /* checks python is running */ void PyC_FileAndNum_Safe(const char **filename, int *lineno); /* checks python is running */
int PyC_AsArray(void *array, PyObject *value, const Py_ssize_t length, int PyC_AsArray_FAST(
void *array, PyObject *value_fast, const Py_ssize_t length,
const PyTypeObject *type, const bool is_double, const char *error_prefix);
int PyC_AsArray(
void *array, PyObject *value, const Py_ssize_t length,
const PyTypeObject *type, const bool is_double, const char *error_prefix); const PyTypeObject *type, const bool is_double, const char *error_prefix);
PyObject * PyC_FromArray(const void *array, int length, const PyTypeObject *type, PyObject * PyC_FromArray(const void *array, int length, const PyTypeObject *type,
const bool is_double, const char *error_prefix); const bool is_double, const char *error_prefix);