Open
Description
Python has multiple C API functions which accept a Python or NULL, both are valid:
- PyObject_SetAttr(obj, attr_name, NULL)
- PyObject_SetAttrString(obj, attr_name, NULL)
- PySequence_SetItem(obj, i, NULL)
- PySequence_SetSlice(obj, i1, i2, NULL)
The problem is: what if creating a value fails and the code pass NULL instead of a Python object, without checking for failure?
The following code sets the number
attribute to 123
... or deletes the number
attribute if PyLong_FromLong()
fails (eg. MemoryError).
PyObject *num = PyLong_FromLong(123);
PyObject_SetAttrString(obj, "number", num);
Py_XDECREF(num);
// surprise surprise, was an exception raised or not?
// (... more code ...)
// let's make it even more funny, make the exception silent!
// There are many functions calling PyErr_Clear() for various reasons.
PyErr_Clear();