mirror of
https://github.com/python/cpython.git
synced 2025-09-08 01:41:19 +00:00
In the first discussion showing how to handle exceptions from C, make the
Python equivalent actually equivalent to the C code. Also, in the C code, place the "goto" statements on a line by themselves for better visibility of statements that affect control flow. This closes bug #123398.
This commit is contained in:
parent
15ffc71c0f
commit
6b3f3f2861
1 changed files with 11 additions and 7 deletions
|
@ -456,7 +456,7 @@ def incr_item(dict, key):
|
||||||
item = dict[key]
|
item = dict[key]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
item = 0
|
item = 0
|
||||||
return item + 1
|
dict[key] = item + 1
|
||||||
\end{verbatim}
|
\end{verbatim}
|
||||||
\ttindex{incr_item()}
|
\ttindex{incr_item()}
|
||||||
|
|
||||||
|
@ -472,21 +472,25 @@ int incr_item(PyObject *dict, PyObject *key)
|
||||||
item = PyObject_GetItem(dict, key);
|
item = PyObject_GetItem(dict, key);
|
||||||
if (item == NULL) {
|
if (item == NULL) {
|
||||||
/* Handle KeyError only: */
|
/* Handle KeyError only: */
|
||||||
if (!PyErr_ExceptionMatches(PyExc_KeyError)) goto error;
|
if (!PyErr_ExceptionMatches(PyExc_KeyError))
|
||||||
|
goto error;
|
||||||
|
|
||||||
/* Clear the error and use zero: */
|
/* Clear the error and use zero: */
|
||||||
PyErr_Clear();
|
PyErr_Clear();
|
||||||
item = PyInt_FromLong(0L);
|
item = PyInt_FromLong(0L);
|
||||||
if (item == NULL) goto error;
|
if (item == NULL)
|
||||||
|
goto error;
|
||||||
}
|
}
|
||||||
|
|
||||||
const_one = PyInt_FromLong(1L);
|
const_one = PyInt_FromLong(1L);
|
||||||
if (const_one == NULL) goto error;
|
if (const_one == NULL)
|
||||||
|
goto error;
|
||||||
|
|
||||||
incremented_item = PyNumber_Add(item, const_one);
|
incremented_item = PyNumber_Add(item, const_one);
|
||||||
if (incremented_item == NULL) goto error;
|
if (incremented_item == NULL)
|
||||||
|
goto error;
|
||||||
|
|
||||||
if (PyObject_SetItem(dict, key, incremented_item) < 0) goto error;
|
if (PyObject_SetItem(dict, key, incremented_item) < 0)
|
||||||
|
goto error;
|
||||||
rv = 0; /* Success */
|
rv = 0; /* Success */
|
||||||
/* Continue with cleanup code */
|
/* Continue with cleanup code */
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue