Skip to content

Commit 4bb923e

Browse files
authored
Get rid of borrowed references in getters (#1182)
The PR doesn't change anything but uses the modern `Py_GetConstant()` instead of borrowed reference to `Py_None`.
1 parent 76abaac commit 4bb923e

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

multidict/_multidict.c

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,9 @@ static inline PyObject *
304304
multidict_get(MultiDictObject *self, PyObject *const *args, Py_ssize_t nargs,
305305
PyObject *kwnames)
306306
{
307-
PyObject *key = NULL, *_default = NULL, *ret;
307+
PyObject *key = NULL;
308+
PyObject *_default = NULL;
309+
bool decref_default = false;
308310

309311
if (parse2("get",
310312
args,
@@ -318,10 +320,17 @@ multidict_get(MultiDictObject *self, PyObject *const *args, Py_ssize_t nargs,
318320
return NULL;
319321
}
320322
if (_default == NULL) {
321-
// fixme, _default is potentially dangerous borrowed ref here
322-
_default = Py_None;
323+
_default = Py_GetConstant(Py_CONSTANT_NONE);
324+
if (_default == NULL) {
325+
return NULL;
326+
}
327+
decref_default = true;
328+
}
329+
ASSERT_CONSISTENT(self, false);
330+
PyObject *ret = _multidict_getone(self, key, _default);
331+
if (decref_default) {
332+
Py_CLEAR(_default);
323333
}
324-
ret = _multidict_getone(self, key, _default);
325334
return ret;
326335
}
327336

@@ -609,7 +618,9 @@ static PyObject *
609618
multidict_setdefault(MultiDictObject *self, PyObject *const *args,
610619
Py_ssize_t nargs, PyObject *kwnames)
611620
{
612-
PyObject *key = NULL, *_default = NULL;
621+
PyObject *key = NULL;
622+
PyObject *_default = NULL;
623+
bool decref_default = false;
613624

614625
if (parse2("setdefault",
615626
args,
@@ -623,11 +634,18 @@ multidict_setdefault(MultiDictObject *self, PyObject *const *args,
623634
return NULL;
624635
}
625636
if (_default == NULL) {
626-
// fixme, _default is potentially dangerous borrowed ref here
627-
_default = Py_None;
637+
_default = Py_GetConstant(Py_CONSTANT_NONE);
638+
if (_default == NULL) {
639+
return NULL;
640+
}
641+
decref_default = true;
628642
}
629643
ASSERT_CONSISTENT(self, false);
630-
return md_set_default(self, key, _default);
644+
PyObject *ret = md_set_default(self, key, _default);
645+
if (decref_default) {
646+
Py_CLEAR(_default);
647+
}
648+
return ret;
631649
}
632650

633651
static PyObject *

0 commit comments

Comments
 (0)