Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions piconumpy/_piconumpy_cpython_capi.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ static int Array_init(ArrayObject *self, PyObject *args, PyObject *kwds) {
self->size = (int)PyList_Size(data);

self->data = (double *)malloc(self->size * sizeof(double));
if (self->data == NULL)
return PyErr_NoMemory();
if (self->data == NULL) {
PyErr_NoMemory();
return -1;
}

for (index = 0; index < self->size; index++) {
item = PyList_GET_ITEM(data, index);
Expand Down Expand Up @@ -63,15 +65,15 @@ static ArrayObject *Array_empty(int size);
static ArrayObject *Array_multiply(PyObject *o1, PyObject *o2) {
int index;
double number;
PyObject *obj_number;
ArrayObject *result = NULL, *arr;
PyObject *obj_number = NULL;
ArrayObject *result = NULL, *arr = NULL;

if (PyNumber_Check(o2)) {
obj_number = o2;
arr = o1;
arr = (ArrayObject *)o1;
} else if (PyNumber_Check(o1)) {
obj_number = o1;
arr = o2;
arr = (ArrayObject *)o2;
}

if (PyNumber_Check(o1) | PyNumber_Check(o2)) {
Expand All @@ -88,8 +90,8 @@ static ArrayObject *Array_multiply(PyObject *o1, PyObject *o2) {
static ArrayObject *Array_add(PyObject *o1, PyObject *o2) {
int index;
ArrayObject *result = NULL, *a1, *a2;
a1 = o1;
a2 = o2;
a1 = (ArrayObject *)o1;
a2 = (ArrayObject *)o2;

if (a1->size != a2->size)
return result;
Expand All @@ -110,7 +112,7 @@ static ArrayObject *Array_divide(PyObject *o1, PyObject *o2) {
if (!PyNumber_Check(o2)) {
return result;
}
a1 = o1;
a1 = (ArrayObject *)o1;
number = PyFloat_AsDouble(o2);
result = Array_empty(a1->size);
for (index = 0; index < a1->size; index++) {
Expand All @@ -125,9 +127,9 @@ Py_ssize_t Array_length(ArrayObject *arr) {
return result;
};

PyFloatObject *Array_item(ArrayObject *arr, Py_ssize_t index) {
PyFloatObject *item = NULL;
if (index < 0 | index >= arr->size) {
PyObject *Array_item(ArrayObject *arr, Py_ssize_t index) {
PyObject *item = NULL;
if (index < 0 || index >= arr->size) {
return item;
}
item = PyFloat_FromDouble(arr->data[index]);
Expand All @@ -140,7 +142,7 @@ static PyMethodDef Array_methods[] = {
{NULL} /* Sentinel */
};

static const PyType_Slot Array_type_slots[] = {
static PyType_Slot Array_type_slots[] = {
{Py_tp_new, PyType_GenericNew},
{Py_tp_init, (initproc)Array_init},
{Py_tp_dealloc, (destructor)Array_dealloc},
Expand All @@ -154,7 +156,7 @@ static const PyType_Slot Array_type_slots[] = {
{0, NULL},
};

static const PyType_Spec Array_type_spec = {
static PyType_Spec Array_type_spec = {
.name = "_piconumpy_cpython_capi.array",
.basicsize = sizeof(ArrayObject),
.itemsize = 0,
Expand All @@ -169,8 +171,10 @@ static ArrayObject *Array_empty(int size) {
new_array = PyObject_New(ArrayObject, ptr_ArrayType);
new_array->size = size;
new_array->data = (double *)malloc(size * sizeof(double));
if (new_array->data == NULL)
return PyErr_NoMemory();
if (new_array->data == NULL) {
PyErr_NoMemory();
return NULL;
}
return new_array;
};

Expand Down
4 changes: 4 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
Extension(
"piconumpy._piconumpy_cpython_capi",
["piconumpy/_piconumpy_cpython_capi.c"],
extra_compile_args = [
'-Wfatal-errors', # stop after one error (unrelated to warnings)
'-Werror', # turn warnings into errors (all, for now)
]
),
*cythonize("piconumpy/_piconumpy_cython.pyx"),
],
Expand Down