odict_sizeof(PyODictObject *od)
{
PyObject *pylong;
- Py_ssize_t res;
+ Py_ssize_t res, temp;
pylong = _PyDict_SizeOf((PyDictObject *)od);
if (pylong == NULL)
pylong = _PyDict_SizeOf((PyDictObject *)od->od_inst_dict);
if (pylong == NULL)
return NULL;
- res += PyLong_AsSsize_t(pylong);
+ temp = PyLong_AsSsize_t(pylong);
Py_DECREF(pylong);
- if (res == -1 && PyErr_Occurred())
+ if (temp == -1 && PyErr_Occurred())
return NULL;
+ res += temp;
res += sizeof(_ODictNode) * od->od_size; /* od_fast_nodes */
if (!_odict_EMPTY(od)) {
/* capture any instance state */
vars = _PyObject_GetAttrId((PyObject *)od, &PyId___dict__);
- if (vars != NULL) {
+ if (vars == NULL)
+ goto Done;
+ else {
PyObject *empty, *od_vars, *iterator, *key;
+ int ns_len;
/* od.__dict__ isn't necessarily a dict... */
ns = PyObject_CallMethod((PyObject *)vars, "copy", NULL);
if (PyErr_Occurred())
goto Done;
- if (!PyObject_Length(ns)) {
+ ns_len = PyObject_Length(ns);
+ if (ns_len == -1)
+ goto Done;
+ if (!ns_len) {
/* nothing novel to pickle in od.__dict__ */
Py_DECREF(ns);
ns = NULL;
return NULL;
}
if (last != NULL) {
+ int is_true;
Py_INCREF(last);
- pos = PyObject_IsTrue(last) ? -1 : 0;
+ is_true = PyObject_IsTrue(last);
Py_DECREF(last);
+ if (is_true == -1)
+ return NULL;
+ pos = is_true ? -1 : 0;
}
if (pos == 0) {
/* Only move if not already the first one. */
PyObject *first_key = _odictnode_KEY(_odict_FIRST(od));
- if (PyObject_RichCompareBool(key, first_key, Py_NE)) {
+ int not_equal = PyObject_RichCompareBool(key, first_key, Py_NE);
+ if (not_equal == -1)
+ return NULL;
+ if (not_equal) {
_ODictNode *node = _odict_pop_node(od, NULL, key);
if (node != NULL) {
_odict_add_head(od, node);
else if (pos == -1) {
/* Only move if not already the last one. */
PyObject *last_key = _odictnode_KEY(_odict_LAST(od));
- if (PyObject_RichCompareBool(key, last_key, Py_NE)) {
+ int not_equal = PyObject_RichCompareBool(key, last_key, Py_NE);
+ if (not_equal == -1)
+ return NULL;
+ if (not_equal) {
_ODictNode *node = _odict_pop_node(od, NULL, key);
if (node != NULL) {
_odict_add_tail(od, node);
int res = PyDict_SetItem(od, key, value);
if (res == 0) {
res = _odict_add_new_node((PyODictObject *)od, key);
+ /* XXX Revert setting the value on the dict? */
}
return res;
};
PyObject *result = di->di_result;
value = PyODict_GetItem((PyObject *)di->di_odict, key); /* borrowed */
+ if (value == NULL)
+ return NULL;
if (result->ob_refcnt == 1) {
/* not in use so we can reuse it
static PyObject *
odictiter_reduce(odictiterobject *di)
{
- PyObject *list;
+ PyObject *list, *iter;
list = PyList_New(0);
if (!list)
Py_DECREF(list);
return NULL;
}
- return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
+ iter = _PyObject_GetBuiltin("iter");
+ if (iter == NULL) {
+ Py_DECREF(list);
+ return NULL;
+ }
+ return Py_BuildValue("N(N)", iter, list);
}
static PyMethodDef odictiter_methods[] = {
while ((pair = PyIter_Next(iterator)) != NULL) {
/* could be more efficient (see UNPACK_SEQUENCE in ceval.c) */
- PyObject * key, *value = NULL;
+ PyObject *key = NULL, *value = NULL;
PyObject *pair_iterator = PyObject_GetIter(pair);
+ if (pair_iterator == NULL)
+ goto Done;
key = PyIter_Next(pair_iterator);
if (key == NULL) {
Done:
Py_DECREF(pair);
- Py_DECREF(pair_iterator);
+ Py_XDECREF(pair_iterator);
Py_XDECREF(key);
Py_XDECREF(value);
if (PyErr_Occurred())
Py_ssize_t len = (args != NULL) ? PyObject_Size(args) : 0;
/* first handle args, if any */
- if (len < 0)
+ if (len < 0) /* PyObject_Size raised an exception. */
return NULL;
else if (len > 1) {
char *msg = "update() takes at most 1 positional argument (%d given)";
if (other == NULL)
return NULL;
Py_INCREF(other);
- if (PyObject_HasAttrString(other, "items")) {
+ if (PyObject_HasAttrString(other, "items")) { /* never fails */
PyObject *items = PyMapping_Items(other);
Py_DECREF(other);
if (items == NULL)
if (res == -1)
return NULL;
}
- else if (PyObject_HasAttrString(other, "keys")) {
+ else if (PyObject_HasAttrString(other, "keys")) { /* never fails */
PyObject *keys, *iterator, *key;
keys = PyObject_CallMethod(other, "keys", NULL);
Py_DECREF(other);
/* now handle kwargs */
len = (kwargs != NULL) ? PyObject_Size(kwargs) : 0;
- if (len < 0)
+ if (len < 0) /* PyObject_Size raised an exception. */
return NULL;
else if (len > 0) {
PyObject *items;