Skip to content

Commit 4bb444a

Browse files
committed
Improve cext memory handling
1 parent 1f80aeb commit 4bb444a

File tree

1 file changed

+30
-22
lines changed

1 file changed

+30
-22
lines changed

src/mysql_capi.c

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -280,12 +280,13 @@ MySQL_dealloc(MySQL *self)
280280
{
281281
if (self) {
282282
MySQL_free_result(self);
283-
if (&self->session)
284-
{
285-
mysql_close(&self->session);
286-
}
283+
mysql_close(&self->session);
284+
285+
Py_DECREF(self->charset_name);
286+
Py_DECREF(self->auth_plugin);
287+
287288
Py_TYPE(self)->tp_free((PyObject*)self);
288-
}
289+
}
289290
}
290291

291292
/**
@@ -358,7 +359,7 @@ MySQL_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
358359
int
359360
MySQL_init(MySQL *self, PyObject *args, PyObject *kwds)
360361
{
361-
PyObject *use_unicode= NULL, *auth_plugin= NULL, *tmp, *con_timeout= NULL;
362+
PyObject *charset_name= NULL, *use_unicode= NULL, *auth_plugin= NULL, *tmp, *con_timeout= NULL;
362363

363364
static char *kwlist[]=
364365
{
@@ -375,7 +376,7 @@ MySQL_init(MySQL *self, PyObject *args, PyObject *kwds)
375376
"|O!O!O!O!O!O!", kwlist,
376377
&PyBool_Type, &self->buffered_at_connect,
377378
&PyBool_Type, &self->raw_at_connect,
378-
&PyStringType, &self->charset_name,
379+
&PyStringType, &charset_name,
379380
&PyIntType, &con_timeout,
380381
&PyBool_Type, &use_unicode,
381382
&PyStringType, &auth_plugin))
@@ -400,6 +401,12 @@ MySQL_init(MySQL *self, PyObject *args, PyObject *kwds)
400401
}
401402
}
402403

404+
if (charset_name) {
405+
Py_DECREF(self->charset_name);
406+
self->charset_name = charset_name;
407+
Py_INCREF(self->charset_name);
408+
}
409+
403410
if (auth_plugin)
404411
{
405412
if (strcmp(PyStringAsString(auth_plugin), "") == 0)
@@ -974,7 +981,9 @@ MySQL_set_character_set(MySQL *self, PyObject *args)
974981
return NULL;
975982
}
976983

984+
Py_DECREF(self->charset_name);
977985
self->charset_name= value;
986+
Py_INCREF(self->charset_name);
978987

979988
Py_RETURN_NONE;
980989
}
@@ -1740,22 +1749,17 @@ MySQL_ping(MySQL *self)
17401749
PyObject*
17411750
MySQL_convert_to_mysql(MySQL *self, PyObject *args)
17421751
{
1743-
PyObject *value, *new_value;
1744-
PyObject *prepared, *quoted;
1752+
PyObject *prepared;
17451753
int i;
17461754
Py_ssize_t size;
17471755
char error[100];
1748-
#ifndef PY3
1749-
int tmp_size;
1750-
PyObject *numeric, *new_num;
1751-
char *tmp;
1752-
#endif
17531756

17541757
size = PyTuple_Size(args);
17551758
prepared = PyTuple_New(size);
17561759

17571760
for (i= 0; i < size; i++) {
1758-
value= PyTuple_GetItem(args, i);
1761+
PyObject *value= PyTuple_GetItem(args, i);
1762+
PyObject *new_value= NULL;
17591763

17601764
if (value == NULL)
17611765
{
@@ -1772,19 +1776,23 @@ MySQL_convert_to_mysql(MySQL *self, PyObject *args)
17721776
if (PyIntLong_Check(value) || PyFloat_Check(value))
17731777
{
17741778
#ifdef PY3
1779+
PyObject *str = PyObject_Str(value);
17751780
PyTuple_SET_ITEM(prepared, i,
17761781
PyBytes_FromString(
17771782
(const char *)PyUnicode_1BYTE_DATA(
1778-
PyObject_Str(value))));
1783+
str)));
1784+
Py_DECREF(str);
17791785
#else
1780-
numeric= PyObject_Repr(value);
1781-
tmp= PyString_AsString(numeric);
1786+
int tmp_size;
1787+
PyObject *numeric= PyObject_Repr(value);
1788+
char *tmp= PyString_AsString(numeric);
17821789
tmp_size= (int)PyString_Size(numeric);
17831790
if (tmp[tmp_size - 1] == 'L')
17841791
{
1785-
new_num= PyString_FromStringAndSize(tmp, tmp_size);
1792+
PyObject *new_num= PyString_FromStringAndSize(tmp, tmp_size);
17861793
_PyString_Resize(&new_num, tmp_size - 1);
17871794
PyTuple_SET_ITEM(prepared, i, new_num);
1795+
Py_DECREF(numeric);
17881796
}
17891797
else
17901798
{
@@ -1854,16 +1862,16 @@ MySQL_convert_to_mysql(MySQL *self, PyObject *args)
18541862
}
18551863
else if (PyBytes_Check(new_value))
18561864
{
1857-
quoted= PyBytes_FromFormat("'%s'", PyBytes_AsString(new_value));
1865+
PyObject *quoted= PyBytes_FromFormat("'%s'", PyBytes_AsString(new_value));
18581866
PyTuple_SET_ITEM(prepared, i, quoted);
18591867
#endif
18601868
}
18611869
else if (PyString_Check(new_value))
18621870
{
18631871
#ifdef PY3
1864-
quoted= PyBytes_FromFormat("'%s'", PyUnicode_AS_DATA(new_value));
1872+
PyObject *quoted= PyBytes_FromFormat("'%s'", PyUnicode_AS_DATA(new_value));
18651873
#else
1866-
quoted= PyString_FromFormat("'%s'", PyStringAsString(new_value));
1874+
PyObject *quoted= PyString_FromFormat("'%s'", PyStringAsString(new_value));
18671875
#endif
18681876
PyTuple_SET_ITEM(prepared, i, quoted);
18691877
}

0 commit comments

Comments
 (0)