]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
fixed a small potential memory leak in UnicodeResultProcessor (for some weird
authorGaëtan de Menten <gdementen@gmail.com>
Sat, 27 Nov 2010 21:27:42 +0000 (22:27 +0100)
committerGaëtan de Menten <gdementen@gmail.com>
Sat, 27 Nov 2010 21:27:42 +0000 (22:27 +0100)
reason, it didn't actually leak in my tests) by providing a dealloc method to
the type, and added a test to ensure it stays that way. Closes #1981.

lib/sqlalchemy/cextension/processors.c
lib/sqlalchemy/cextension/resultproxy.c
test/aaa_profiling/test_memusage.py

index 193fb19f30cea75d987bda045552a68e15990fe5..36745c8178e09c96f4727a914a5975e592850978 100644 (file)
@@ -204,6 +204,14 @@ UnicodeResultProcessor_process(UnicodeResultProcessor *self, PyObject *value)
     return PyUnicode_Decode(str, len, encoding, errors);
 }
 
+static void
+UnicodeResultProcessor_dealloc(UnicodeResultProcessor *self)
+{
+    Py_XDECREF(self->encoding);
+    Py_XDECREF(self->errors);
+    self->ob_type->tp_free((PyObject*)self);
+}
+
 static PyMethodDef UnicodeResultProcessor_methods[] = {
     {"process", (PyCFunction)UnicodeResultProcessor_process, METH_O,
      "The value processor itself."},
@@ -216,7 +224,7 @@ static PyTypeObject UnicodeResultProcessorType = {
     "sqlalchemy.cprocessors.UnicodeResultProcessor",        /* tp_name */
     sizeof(UnicodeResultProcessor),             /* tp_basicsize */
     0,                                          /* tp_itemsize */
-    0,                                          /* tp_dealloc */
+    (destructor)UnicodeResultProcessor_dealloc, /* tp_dealloc */
     0,                                          /* tp_print */
     0,                                          /* tp_getattr */
     0,                                          /* tp_setattr */
index 73e12734570eb379c9b95fbf286ea93b433dd249..93471073bcdb35e5210883bd98833fcd396438df 100644 (file)
@@ -107,11 +107,11 @@ BaseRowProxy_init(BaseRowProxy *self, PyObject *args, PyObject *kwds)
 static PyObject *
 BaseRowProxy_reduce(PyObject *self)
 {
-       PyObject *method, *state;
-       PyObject *module, *reconstructor, *cls;
+    PyObject *method, *state;
+    PyObject *module, *reconstructor, *cls;
 
-       method = PyObject_GetAttrString(self, "__getstate__");
-       if (method == NULL)
+    method = PyObject_GetAttrString(self, "__getstate__");
+    if (method == NULL)
         return NULL;
 
     state = PyObject_CallObject(method, NULL);
@@ -503,8 +503,8 @@ static PyGetSetDef BaseRowProxy_getseters[] = {
 static PyMethodDef BaseRowProxy_methods[] = {
     {"values", (PyCFunction)BaseRowProxy_values, METH_NOARGS,
      "Return the values represented by this BaseRowProxy as a list."},
-       {"__reduce__",  (PyCFunction)BaseRowProxy_reduce, METH_NOARGS,
-        "Pickle support method."},
+    {"__reduce__",  (PyCFunction)BaseRowProxy_reduce, METH_NOARGS,
+     "Pickle support method."},
     {NULL}  /* Sentinel */
 };
 
index dbf6e8ce1672922d419570204b767cb464af70af..43a4a3eb55ab01e877e1eb8f3c4758d1a79cb0f9 100644 (file)
@@ -11,7 +11,8 @@ from sqlalchemy import MetaData, Integer, String, ForeignKey, \
 from sqlalchemy.test.schema import Table, Column
 import sqlalchemy as sa
 from sqlalchemy.sql import column
-from sqlalchemy.processors import to_decimal_processor_factory
+from sqlalchemy.processors import to_decimal_processor_factory, \
+    to_unicode_processor_factory
 from sqlalchemy.test.util import gc_collect
 from decimal import Decimal as _python_Decimal
 import gc
@@ -581,3 +582,12 @@ class MemUsageTest(EnsureZeroed):
         def go():
             to_decimal_processor_factory(_python_Decimal, 10)(1.2)
         go()
+
+    @testing.requires.cextensions
+    def test_UnicodeResultProcessor_init(self):
+        @profile_memory
+        def go():
+            to_unicode_processor_factory('utf8')
+        go()
+
+