Kaydet (Commit) 04707c03 authored tarafından Antoine Pitrou's avatar Antoine Pitrou

Fix error handling in examples of C API use.

üst 84a0fbf6
...@@ -246,17 +246,19 @@ sets all items of a list (actually, any mutable sequence) to a given item:: ...@@ -246,17 +246,19 @@ sets all items of a list (actually, any mutable sequence) to a given item::
int int
set_all(PyObject *target, PyObject *item) set_all(PyObject *target, PyObject *item)
{ {
int i, n; Py_ssize_t i, n;
n = PyObject_Length(target); n = PyObject_Length(target);
if (n < 0) if (n < 0)
return -1; return -1;
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
PyObject *index = PyLong_FromLong(i); PyObject *index = PyLong_FromSsize_t(i);
if (!index) if (!index)
return -1; return -1;
if (PyObject_SetItem(target, index, item) < 0) if (PyObject_SetItem(target, index, item) < 0) {
Py_DECREF(index);
return -1; return -1;
}
Py_DECREF(index); Py_DECREF(index);
} }
return 0; return 0;
...@@ -292,8 +294,8 @@ using :c:func:`PySequence_GetItem`. :: ...@@ -292,8 +294,8 @@ using :c:func:`PySequence_GetItem`. ::
long long
sum_list(PyObject *list) sum_list(PyObject *list)
{ {
int i, n; Py_ssize_t i, n;
long total = 0; long total = 0, value;
PyObject *item; PyObject *item;
n = PyList_Size(list); n = PyList_Size(list);
...@@ -302,7 +304,11 @@ using :c:func:`PySequence_GetItem`. :: ...@@ -302,7 +304,11 @@ using :c:func:`PySequence_GetItem`. ::
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
item = PyList_GetItem(list, i); /* Can't fail */ item = PyList_GetItem(list, i); /* Can't fail */
if (!PyLong_Check(item)) continue; /* Skip non-integers */ if (!PyLong_Check(item)) continue; /* Skip non-integers */
total += PyLong_AsLong(item); value = PyLong_AsLong(item);
if (value == -1 && PyErr_Occurred())
/* Integer too big to fit in a C long, bail out */
return -1;
total += value;
} }
return total; return total;
} }
...@@ -314,8 +320,8 @@ using :c:func:`PySequence_GetItem`. :: ...@@ -314,8 +320,8 @@ using :c:func:`PySequence_GetItem`. ::
long long
sum_sequence(PyObject *sequence) sum_sequence(PyObject *sequence)
{ {
int i, n; Py_ssize_t i, n;
long total = 0; long total = 0, value;
PyObject *item; PyObject *item;
n = PySequence_Length(sequence); n = PySequence_Length(sequence);
if (n < 0) if (n < 0)
...@@ -324,9 +330,17 @@ using :c:func:`PySequence_GetItem`. :: ...@@ -324,9 +330,17 @@ using :c:func:`PySequence_GetItem`. ::
item = PySequence_GetItem(sequence, i); item = PySequence_GetItem(sequence, i);
if (item == NULL) if (item == NULL)
return -1; /* Not a sequence, or other failure */ return -1; /* Not a sequence, or other failure */
if (PyLong_Check(item)) if (PyLong_Check(item)) {
total += PyLong_AsLong(item); value = PyLong_AsLong(item);
Py_DECREF(item); /* Discard reference ownership */ Py_DECREF(item);
if (value == -1 && PyErr_Occurred())
/* Integer too big to fit in a C long, bail out */
return -1;
total += value;
}
else {
Py_DECREF(item); /* Discard reference ownership */
}
} }
return total; return total;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment