Skip to content

Always return new references in C Extension #1084

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
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
2 changes: 2 additions & 0 deletions CHANGES/1084.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

Always return new references in C Extension.
42 changes: 33 additions & 9 deletions multidict/_multidict.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ _multidict_eq(MultiDictObject *self, MultiDictObject *other)
int cmp_identity = 0,
cmp_value = 0;

int ret = 1;

if (self == other) {
return 1;
}
Expand All @@ -91,22 +93,34 @@ _multidict_eq(MultiDictObject *self, MultiDictObject *other)
_pair_list_next(&other->pairs, &pos2, &identity2, NULL, &value2, &h2))
{
if (h1 != h2) {
return 0;
ret = 0;
goto exit;
}
cmp_identity = PyObject_RichCompareBool(identity1, identity2, Py_NE);
if (cmp_identity < 0) {
return -1;
ret = -1;
goto exit;
}
cmp_value = PyObject_RichCompareBool(value1, value2, Py_NE);
if (cmp_value < 0) {
return -1;
ret = -1;
goto exit;
}
if (cmp_identity || cmp_value) {
return 0;
ret = 0;
goto exit;
}
}

return 1;
Py_CLEAR(identity1);
Py_CLEAR(identity2);
Py_CLEAR(value1);
Py_CLEAR(value2);
}
exit:
Py_CLEAR(identity1);
Py_CLEAR(identity2);
Py_CLEAR(value1);
Py_CLEAR(value2);
return ret;
}

static inline int
Expand All @@ -125,8 +139,12 @@ _multidict_append_items(MultiDictObject *self, pair_list_t *pairs)

while (_pair_list_next(pairs, &pos, NULL, &key, &value, NULL)) {
if (pair_list_add(&self->pairs, key, value) < 0) {
Py_CLEAR(key);
Py_CLEAR(value);
return -1;
}
Py_CLEAR(key);
Py_CLEAR(value);
}

return 0;
Expand Down Expand Up @@ -508,16 +526,22 @@ multidict_get(
PyObject *key = NULL,
*_default = NULL,
*ret;
bool clear_default = false;

if (parse2("get", args, nargs, kwnames, 1,
"key", &key, "default", &_default) < 0) {
return NULL;
}
if (_default == NULL) {
// fixme, _default is potentially dangerous borrowed ref here
_default = Py_None;
clear_default = true;
_default = Py_GetConstant(Py_CONSTANT_NONE);
if (_default == NULL) {
return NULL;
}
}
ret = _multidict_getone(self, key, _default);
if (clear_default)
Py_CLEAR(_default);
return ret;
}

Expand Down
6 changes: 2 additions & 4 deletions multidict/_multilib/iter.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ multidict_items_iter_iternext(MultidictIter *self)
}

ret = PyTuple_Pack(2, key, value);
Py_CLEAR(key);
Py_CLEAR(value);
if (ret == NULL) {
return NULL;
}
Expand All @@ -111,8 +113,6 @@ multidict_values_iter_iternext(MultidictIter *self)
return NULL;
}

Py_INCREF(value);

return value;
}

Expand All @@ -131,8 +131,6 @@ multidict_keys_iter_iternext(MultidictIter *self)
return NULL;
}

Py_INCREF(key);

return key;
}

Expand Down
28 changes: 20 additions & 8 deletions multidict/_multilib/pair_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -448,13 +448,13 @@ _pair_list_next(pair_list_t *list, Py_ssize_t *ppos, PyObject **pidentity,
pair = pair_list_get(list, *ppos);

if (pidentity) {
*pidentity = pair->identity;
*pidentity = Py_NewRef(pair->identity);
}
if (pkey) {
*pkey = pair->key;
*pkey = Py_NewRef(pair->key);
}
if (pvalue) {
*pvalue = pair->value;
*pvalue = Py_NewRef(pair->value);
}
if (phash) {
*phash = pair->hash;
Expand Down Expand Up @@ -502,6 +502,7 @@ pair_list_contains(pair_list_t *list, PyObject *key)
continue;
}
tmp = str_cmp(ident, identity);
Py_CLEAR(identity);
if (tmp > 0) {
Py_DECREF(ident);
return 1;
Expand Down Expand Up @@ -544,21 +545,23 @@ pair_list_get_one(pair_list_t *list, PyObject *key, PyObject **ret)
continue;
}
tmp = str_cmp(ident, identity);
Py_CLEAR(identity);
if (tmp > 0) {
Py_INCREF(value);
Py_DECREF(ident);
*ret = value;
return 0;
}
else if (tmp < 0) {
goto fail;
}
Py_CLEAR(value);
}

Py_DECREF(ident);
return 0;
fail:
Py_XDECREF(ident);
Py_CLEAR(value);
return -1;
}

Expand Down Expand Up @@ -589,16 +592,16 @@ pair_list_get_all(pair_list_t *list, PyObject *key, PyObject **ret)
continue;
}
tmp = str_cmp(ident, identity);
Py_CLEAR(identity);
if (tmp > 0) {
if (res == NULL) {
res = PyList_New(1);
if (res == NULL) {
goto fail;
}
if (PyList_SetItem(res, 0, value) < 0) {
if (PyList_SetItem(res, 0, Py_NewRef(value)) < 0) {
goto fail;
}
Py_INCREF(value);
}
else if (PyList_Append(res, value) < 0) {
goto fail;
Expand All @@ -607,12 +610,14 @@ pair_list_get_all(pair_list_t *list, PyObject *key, PyObject **ret)
else if (tmp < 0) {
goto fail;
}
Py_CLEAR(value);
}

if (res != NULL) {
*ret = res;
}
Py_DECREF(ident);
Py_CLEAR(value);
return 0;

fail:
Expand Down Expand Up @@ -647,14 +652,15 @@ pair_list_set_default(pair_list_t *list, PyObject *key, PyObject *value)
continue;
}
tmp = str_cmp(ident, identity);
Py_CLEAR(identity);
if (tmp > 0) {
Py_INCREF(value2);
Py_DECREF(ident);
return value2;
}
else if (tmp < 0) {
goto fail;
}
Py_CLEAR(value2);
}

if (_pair_list_add_with_hash(list, ident, key, value, hash1) < 0) {
Expand All @@ -666,6 +672,7 @@ pair_list_set_default(pair_list_t *list, PyObject *key, PyObject *value)
return value;
fail:
Py_XDECREF(ident);
Py_CLEAR(value2);
return NULL;
}

Expand Down Expand Up @@ -1212,14 +1219,19 @@ pair_list_eq_to_mapping(pair_list_t *list, PyObject *other)
pos = 0;
while (pair_list_next(list, &pos, NULL, &key, &avalue)) {
if (PyMapping_GetOptionalItem(other, key, &bvalue) < 0) {
Py_CLEAR(key);
Py_CLEAR(avalue);
return -1;
}
Py_CLEAR(key);
if (bvalue == NULL) {
Py_CLEAR(avalue);
return 0;
}

eq = PyObject_RichCompareBool(avalue, bvalue, Py_EQ);
Py_DECREF(bvalue);
Py_CLEAR(avalue);
Py_CLEAR(bvalue);

if (eq <= 0) {
return eq;
Expand Down
Loading