The cursor is weakly referenceable

This commit is contained in:
Daniele Varrazzo 2011-01-03 12:47:01 +01:00
parent 04cf90cc21
commit 19ff51ae75
4 changed files with 19 additions and 4 deletions

View File

@ -6,7 +6,7 @@ What's new in psycopg 2.3.3
- Added `register_composite()` function to cast PostgreSQL composite types - Added `register_composite()` function to cast PostgreSQL composite types
into Python tuples/namedtuples. into Python tuples/namedtuples.
- The build script refuses to guess values if pg_config is not found. - The build script refuses to guess values if pg_config is not found.
- Connections are weakly referenceable. - Connections and cursors are weakly referenceable.
* Bug fixes: * Bug fixes:

View File

@ -81,6 +81,8 @@ typedef struct {
PyObject *string_types; /* a set of typecasters for string types */ PyObject *string_types; /* a set of typecasters for string types */
PyObject *binary_types; /* a set of typecasters for binary types */ PyObject *binary_types; /* a set of typecasters for binary types */
PyObject *weakreflist; /* list of weak references */
} cursorObject; } cursorObject;
/* C-callable functions in cursor_int.c and cursor_ext.c */ /* C-callable functions in cursor_int.c and cursor_ext.c */

View File

@ -1642,6 +1642,7 @@ cursor_setup(cursorObject *self, connectionObject *conn, const char *name)
self->string_types = NULL; self->string_types = NULL;
self->binary_types = NULL; self->binary_types = NULL;
self->weakreflist = NULL;
Py_INCREF(Py_None); Py_INCREF(Py_None);
self->description = Py_None; self->description = Py_None;
@ -1668,6 +1669,10 @@ cursor_dealloc(PyObject* obj)
{ {
cursorObject *self = (cursorObject *)obj; cursorObject *self = (cursorObject *)obj;
if (self->weakreflist) {
PyObject_ClearWeakRefs(obj);
}
PyObject_GC_UnTrack(self); PyObject_GC_UnTrack(self);
if (self->name) PyMem_Free(self->name); if (self->name) PyMem_Free(self->name);
@ -1769,14 +1774,15 @@ PyTypeObject cursorType = {
0, /*tp_as_buffer*/ 0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_ITER | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_ITER |
Py_TPFLAGS_HAVE_GC, /*tp_flags*/ Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_WEAKREFS ,
/*tp_flags*/
cursorType_doc, /*tp_doc*/ cursorType_doc, /*tp_doc*/
(traverseproc)cursor_traverse, /*tp_traverse*/ (traverseproc)cursor_traverse, /*tp_traverse*/
0, /*tp_clear*/ 0, /*tp_clear*/
0, /*tp_richcompare*/ 0, /*tp_richcompare*/
0, /*tp_weaklistoffset*/ offsetof(cursorObject, weakreflist), /*tp_weaklistoffset*/
cursor_iter, /*tp_iter*/ cursor_iter, /*tp_iter*/
cursor_next, /*tp_iternext*/ cursor_next, /*tp_iternext*/

View File

@ -99,6 +99,13 @@ class CursorTests(unittest.TestCase):
curs2 = self.conn.cursor() curs2 = self.conn.cursor()
self.assertEqual("foofoo", curs2.cast(705, 'foo')) self.assertEqual("foofoo", curs2.cast(705, 'foo'))
def test_weakref(self):
from weakref import ref
curs = self.conn.cursor()
w = ref(curs)
del curs
self.assert_(w() is None)
def test_suite(): def test_suite():
return unittest.TestLoader().loadTestsFromName(__name__) return unittest.TestLoader().loadTestsFromName(__name__)