|
15 | 15 | // some indirections. |
16 | 16 | PyObject *CPyDict_GetItem(PyObject *dict, PyObject *key) { |
17 | 17 | if (PyDict_CheckExact(dict)) { |
18 | | - PyObject *res = PyDict_GetItemWithError(dict, key); |
19 | | - if (!res) { |
20 | | - if (!PyErr_Occurred()) { |
21 | | - PyErr_SetObject(PyExc_KeyError, key); |
22 | | - } |
23 | | - } else { |
24 | | - Py_INCREF(res); |
| 18 | + PyObject *res; |
| 19 | + int found = PyDict_GetItemRef(dict, key, &res); |
| 20 | + if (found == 0) { |
| 21 | + PyErr_SetObject(PyExc_KeyError, key); |
25 | 22 | } |
26 | 23 | return res; |
27 | 24 | } else { |
@@ -56,14 +53,15 @@ PyObject *CPyDict_Build(Py_ssize_t size, ...) { |
56 | 53 | PyObject *CPyDict_Get(PyObject *dict, PyObject *key, PyObject *fallback) { |
57 | 54 | // We are dodgily assuming that get on a subclass doesn't have |
58 | 55 | // different behavior. |
59 | | - PyObject *res = PyDict_GetItemWithError(dict, key); |
60 | | - if (!res) { |
61 | | - if (PyErr_Occurred()) { |
62 | | - return NULL; |
63 | | - } |
64 | | - res = fallback; |
| 56 | + PyObject *res; |
| 57 | + int found = PyDict_GetItemRef(dict, key, &res); |
| 58 | + if (found < 0) { |
| 59 | + return NULL; |
| 60 | + } |
| 61 | + if (found == 0) { |
| 62 | + Py_INCREF(fallback); |
| 63 | + return fallback; |
65 | 64 | } |
66 | | - Py_INCREF(res); |
67 | 65 | return res; |
68 | 66 | } |
69 | 67 |
|
@@ -100,17 +98,19 @@ PyObject *CPyDict_SetDefaultWithEmptyDatatype(PyObject *dict, PyObject *key, |
100 | 98 | } else if (data_type == 3) { |
101 | 99 | new_obj = PySet_New(NULL); |
102 | 100 | } else { |
103 | | - return NULL; |
| 101 | + new_obj = NULL; |
104 | 102 | } |
105 | 103 |
|
106 | | - if (CPyDict_SetItem(dict, key, new_obj) == -1) { |
107 | | - return NULL; |
| 104 | + if (new_obj == NULL) { |
| 105 | + res = NULL; |
| 106 | + } else if (CPyDict_SetItem(dict, key, new_obj) == -1) { |
| 107 | + Py_DECREF(new_obj); |
| 108 | + res = NULL; |
108 | 109 | } else { |
109 | | - return new_obj; |
| 110 | + res = new_obj; |
110 | 111 | } |
111 | | - } else { |
112 | | - return res; |
113 | 112 | } |
| 113 | + return res; |
114 | 114 | } |
115 | 115 |
|
116 | 116 | int CPyDict_SetItem(PyObject *dict, PyObject *key, PyObject *value) { |
|
0 commit comments