mirror of
https://github.com/psycopg/psycopg2.git
synced 2025-05-07 16:43:41 +03:00
The connection is weakly referenceable
This commit is contained in:
parent
5888b03608
commit
04cf90cc21
1
NEWS-2.3
1
NEWS-2.3
|
@ -6,6 +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.
|
||||||
|
|
||||||
* Bug fixes:
|
* Bug fixes:
|
||||||
|
|
||||||
|
|
|
@ -119,6 +119,7 @@ typedef struct {
|
||||||
PyObject *binary_types; /* a set of typecasters for binary types */
|
PyObject *binary_types; /* a set of typecasters for binary types */
|
||||||
|
|
||||||
int equote; /* use E''-style quotes for escaped strings */
|
int equote; /* use E''-style quotes for escaped strings */
|
||||||
|
PyObject *weakreflist; /* list of weak references */
|
||||||
|
|
||||||
} connectionObject;
|
} connectionObject;
|
||||||
|
|
||||||
|
|
|
@ -867,6 +867,7 @@ connection_setup(connectionObject *self, const char *dsn, long int async)
|
||||||
self->binary_types = PyDict_New();
|
self->binary_types = PyDict_New();
|
||||||
self->notice_pending = NULL;
|
self->notice_pending = NULL;
|
||||||
self->encoding = NULL;
|
self->encoding = NULL;
|
||||||
|
self->weakreflist = NULL;
|
||||||
|
|
||||||
pthread_mutex_init(&(self->lock), NULL);
|
pthread_mutex_init(&(self->lock), NULL);
|
||||||
|
|
||||||
|
@ -897,6 +898,10 @@ connection_dealloc(PyObject* obj)
|
||||||
{
|
{
|
||||||
connectionObject *self = (connectionObject *)obj;
|
connectionObject *self = (connectionObject *)obj;
|
||||||
|
|
||||||
|
if (self->weakreflist) {
|
||||||
|
PyObject_ClearWeakRefs(obj);
|
||||||
|
}
|
||||||
|
|
||||||
PyObject_GC_UnTrack(self);
|
PyObject_GC_UnTrack(self);
|
||||||
|
|
||||||
if (self->closed == 0) conn_close(self);
|
if (self->closed == 0) conn_close(self);
|
||||||
|
@ -1002,14 +1007,16 @@ PyTypeObject connectionType = {
|
||||||
0, /*tp_setattro*/
|
0, /*tp_setattro*/
|
||||||
0, /*tp_as_buffer*/
|
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*/
|
connectionType_doc, /*tp_doc*/
|
||||||
|
|
||||||
(traverseproc)connection_traverse, /*tp_traverse*/
|
(traverseproc)connection_traverse, /*tp_traverse*/
|
||||||
0, /*tp_clear*/
|
0, /*tp_clear*/
|
||||||
|
|
||||||
0, /*tp_richcompare*/
|
0, /*tp_richcompare*/
|
||||||
0, /*tp_weaklistoffset*/
|
offsetof(connectionObject, weakreflist), /* tp_weaklistoffset */
|
||||||
|
|
||||||
0, /*tp_iter*/
|
0, /*tp_iter*/
|
||||||
0, /*tp_iternext*/
|
0, /*tp_iternext*/
|
||||||
|
|
|
@ -111,6 +111,14 @@ class ConnectionTests(unittest.TestCase):
|
||||||
self.assert_(time.time() - t0 < 3,
|
self.assert_(time.time() - t0 < 3,
|
||||||
"something broken in concurrency")
|
"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):
|
class IsolationLevelsTestCase(unittest.TestCase):
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user