Skip to content

Commit 0370366

Browse files
committed
plugins/python: Fix a memory leak coming from PyBytes_FromString
PyBytes_FromString returns a new reference, which must be freed. To fix this, use a static function instead of a macro, and clean up inside it. Also, move the function from the header to the only place where it's used, to fix GCC warnings about undefined function.
1 parent 89cb161 commit 0370366

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

plugins/python/uwsgi_pymodule.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2141,6 +2141,24 @@ PyObject *py_uwsgi_spooler_jobs(PyObject * self, PyObject * args) {
21412141
}
21422142

21432143

2144+
#ifdef PYTHREE
2145+
static PyObject *uwsgi_py_dict_get(PyObject *a, const char *key) {
2146+
PyObject *key_bytes = PyBytes_FromString(key);
2147+
PyObject *result = PyDict_GetItem(a, key_bytes);
2148+
Py_DECREF(key_bytes);
2149+
return result;
2150+
}
2151+
2152+
static void uwsgi_py_dict_del(PyObject *a, const char *key) {
2153+
PyObject *key_bytes = PyBytes_FromString(key);
2154+
PyDict_DelItem(a, key_bytes);
2155+
Py_DECREF(key_bytes);
2156+
}
2157+
#else
2158+
#define uwsgi_py_dict_get(a, b) PyDict_GetItemString(a, b)
2159+
#define uwsgi_py_dict_del(a, b) PyDict_DelItemString(a, b)
2160+
#endif
2161+
21442162
PyObject *py_uwsgi_send_spool(PyObject * self, PyObject * args, PyObject *kw) {
21452163
PyObject *spool_dict, *spool_vars;
21462164
PyObject *zero, *key, *val;

plugins/python/uwsgi_python.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,10 @@ PyAPI_FUNC(PyObject *) PyMarshal_ReadObjectFromString(char *, Py_ssize_t);
9696
#define PyString_Concat PyBytes_Concat
9797
#define PyString_AsString (char *) PyBytes_AsString
9898
#define PyFile_FromFile(A,B,C,D) PyFile_FromFd(fileno((A)), (B), (C), -1, NULL, NULL, NULL, 0)
99-
#define uwsgi_py_dict_get(a, b) PyDict_GetItem(a, PyBytes_FromString(b));
100-
#define uwsgi_py_dict_del(a, b) PyDict_DelItem(a, PyBytes_FromString(b));
10199

102100
#else
103101
#define UWSGI_PYFROMSTRING(x) PyString_FromString(x)
104102
#define UWSGI_PYFROMSTRINGSIZE(x, y) PyString_FromStringAndSize(x, y)
105-
#define uwsgi_py_dict_get(a, b) PyDict_GetItemString(a, b)
106-
#define uwsgi_py_dict_del(a, b) PyDict_DelItemString(a, b)
107103
#endif
108104

109105
#define LOADER_DYN 0

0 commit comments

Comments
 (0)