|
19 | 19 | // Functions to store and retrieve "private" data attached to Pyhon object |
20 | 20 | %fragment("private_data", "header") { |
21 | 21 | static PyObject* _get_store(PyObject* py_self, bool create) { |
22 | | - // Return a new reference |
| 22 | + // Return a borrowed reference |
| 23 | + PyObject* dict = NULL; |
23 | 24 | if (!PyObject_HasAttrString(py_self, "_private_data_")) { |
24 | 25 | if (!create) |
25 | 26 | return NULL; |
26 | | - PyObject* dict = PyDict_New(); |
| 27 | + dict = PyDict_New(); |
27 | 28 | if (!dict) |
28 | 29 | return NULL; |
29 | 30 | int error = PyObject_SetAttrString(py_self, "_private_data_", dict); |
30 | 31 | Py_DECREF(dict); |
31 | 32 | if (error) |
32 | 33 | return NULL; |
33 | 34 | } |
34 | | - return PyObject_GetAttrString(py_self, "_private_data_"); |
| 35 | + dict = PyObject_GetAttrString(py_self, "_private_data_"); |
| 36 | + Py_DECREF(dict); |
| 37 | + return dict; |
35 | 38 | }; |
36 | 39 | static int private_store_set(PyObject* py_self, const char* name, |
37 | 40 | PyObject* val) { |
38 | 41 | PyObject* dict = _get_store(py_self, true); |
39 | 42 | if (!dict) |
40 | 43 | return -1; |
41 | | - int result = PyDict_SetItemString(dict, name, val); |
42 | | - Py_DECREF(dict); |
43 | | - return result; |
| 44 | + return PyDict_SetItemString(dict, name, val); |
44 | 45 | }; |
45 | 46 | static PyObject* private_store_get(PyObject* py_self, const char* name) { |
46 | 47 | // Return a borrowed reference |
47 | 48 | PyObject* dict = _get_store(py_self, false); |
48 | 49 | if (!dict) |
49 | 50 | return NULL; |
50 | | - PyObject* result = PyDict_GetItemString(dict, name); |
51 | | - Py_DECREF(dict); |
52 | | - return result; |
| 51 | + return PyDict_GetItemString(dict, name); |
53 | 52 | }; |
54 | 53 | static int private_store_del(PyObject* py_self, const char* name) { |
55 | 54 | PyObject* dict = _get_store(py_self, false); |
56 | 55 | if (!dict) |
57 | 56 | return 0; |
58 | | - int result = 0; |
59 | 57 | if (PyDict_GetItemString(dict, name)) |
60 | | - result = PyDict_DelItemString(dict, name); |
61 | | - Py_DECREF(dict); |
62 | | - return result; |
| 58 | + return PyDict_DelItemString(dict, name); |
| 59 | + return 0; |
63 | 60 | }; |
64 | 61 | } |
65 | 62 |
|
|
0 commit comments