From: Gaƫtan de Menten Date: Sat, 27 Nov 2010 21:27:42 +0000 (+0100) Subject: fixed a small potential memory leak in UnicodeResultProcessor (for some weird X-Git-Tag: rel_0_6_6~31^2~6 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=20418096d5862ed40f22b6c7b7cc53dd212bbd21;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git fixed a small potential memory leak in UnicodeResultProcessor (for some weird 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. --- diff --git a/lib/sqlalchemy/cextension/processors.c b/lib/sqlalchemy/cextension/processors.c index 193fb19f30..36745c8178 100644 --- a/lib/sqlalchemy/cextension/processors.c +++ b/lib/sqlalchemy/cextension/processors.c @@ -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 */ diff --git a/lib/sqlalchemy/cextension/resultproxy.c b/lib/sqlalchemy/cextension/resultproxy.c index 73e1273457..93471073bc 100644 --- a/lib/sqlalchemy/cextension/resultproxy.c +++ b/lib/sqlalchemy/cextension/resultproxy.c @@ -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 */ }; diff --git a/test/aaa_profiling/test_memusage.py b/test/aaa_profiling/test_memusage.py index dbf6e8ce16..43a4a3eb55 100644 --- a/test/aaa_profiling/test_memusage.py +++ b/test/aaa_profiling/test_memusage.py @@ -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() + +