The connection is weakly referenceable

This commit is contained in:
Daniele Varrazzo 2011-01-03 11:52:38 +01:00
parent 5888b03608
commit 04cf90cc21
4 changed files with 21 additions and 4 deletions

View File

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

View File

@ -119,6 +119,7 @@ typedef struct {
PyObject *binary_types; /* a set of typecasters for binary types */
int equote; /* use E''-style quotes for escaped strings */
PyObject *weakreflist; /* list of weak references */
} connectionObject;

View File

@ -867,6 +867,7 @@ connection_setup(connectionObject *self, const char *dsn, long int async)
self->binary_types = PyDict_New();
self->notice_pending = NULL;
self->encoding = NULL;
self->weakreflist = NULL;
pthread_mutex_init(&(self->lock), NULL);
@ -896,11 +897,15 @@ static void
connection_dealloc(PyObject* obj)
{
connectionObject *self = (connectionObject *)obj;
if (self->weakreflist) {
PyObject_ClearWeakRefs(obj);
}
PyObject_GC_UnTrack(self);
if (self->closed == 0) conn_close(self);
conn_notice_clean(self);
if (self->dsn) free(self->dsn);
@ -1002,14 +1007,16 @@ PyTypeObject connectionType = {
0, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /*tp_flags*/
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
Py_TPFLAGS_HAVE_WEAKREFS,
/*tp_flags*/
connectionType_doc, /*tp_doc*/
(traverseproc)connection_traverse, /*tp_traverse*/
0, /*tp_clear*/
0, /*tp_richcompare*/
0, /*tp_weaklistoffset*/
offsetof(connectionObject, weakreflist), /* tp_weaklistoffset */
0, /*tp_iter*/
0, /*tp_iternext*/

View File

@ -111,6 +111,14 @@ class ConnectionTests(unittest.TestCase):
self.assert_(time.time() - t0 < 3,
"something broken in concurrency")
def test_weakref(self):
from weakref import ref
conn = psycopg2.connect(self.conn.dsn)
w = ref(conn)
conn.close()
del conn
self.assert_(w() is None)
class IsolationLevelsTestCase(unittest.TestCase):