From: Daniele Varrazzo Date: Fri, 22 May 2020 06:32:03 +0000 (+1200) Subject: Make sure connections and cursors can be gc'd correctly X-Git-Tag: 3.0.dev0~501 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d5f663b30cffdf3cf7f4f39e1e41907c48d54bae;p=thirdparty%2Fpsycopg.git Make sure connections and cursors can be gc'd correctly --- diff --git a/psycopg3/pq/pq_cython.pxd b/psycopg3/pq/pq_cython.pxd index 0acf6a85e..479b85c4d 100644 --- a/psycopg3/pq/pq_cython.pxd +++ b/psycopg3/pq/pq_cython.pxd @@ -6,6 +6,7 @@ ctypedef int(*conn_int_f) (const impl.PGconn *) cdef class PGconn: cdef impl.PGconn* pgconn_ptr + cdef object __weakref__ @staticmethod cdef PGconn _from_ptr(impl.PGconn *ptr) diff --git a/tests/pq/test_pgconn.py b/tests/pq/test_pgconn.py index a0bafc6b1..473f8c459 100644 --- a/tests/pq/test_pgconn.py +++ b/tests/pq/test_pgconn.py @@ -1,5 +1,7 @@ +import gc import os import logging +import weakref from select import select import pytest @@ -71,6 +73,15 @@ def test_finish(pgconn, pq): assert pgconn.status == pq.ConnStatus.BAD +def test_weakref(pq, dsn): + conn = pq.PGconn.connect(dsn.encode("utf8")) + w = weakref.ref(conn) + conn.finish() + del conn + gc.collect() + assert w() is None + + def test_info(pq, dsn, pgconn): info = pgconn.info assert len(info) > 20 diff --git a/tests/test_async_connection.py b/tests/test_async_connection.py index 75ec95e2b..6aa10317d 100644 --- a/tests/test_async_connection.py +++ b/tests/test_async_connection.py @@ -1,5 +1,7 @@ +import gc import pytest import logging +import weakref import psycopg3 from psycopg3 import AsyncConnection @@ -26,6 +28,15 @@ def test_close(aconn, loop): assert aconn.status == aconn.ConnStatus.BAD +def test_weakref(dsn, loop): + conn = loop.run_until_complete(psycopg3.AsyncConnection.connect(dsn)) + w = weakref.ref(conn) + loop.run_until_complete(conn.close()) + del conn + gc.collect() + assert w() is None + + def test_commit(loop, aconn): aconn.pgconn.exec_(b"drop table if exists foo") aconn.pgconn.exec_(b"create table foo (id int primary key)") diff --git a/tests/test_async_cursor.py b/tests/test_async_cursor.py index 5c428853a..afda8070f 100644 --- a/tests/test_async_cursor.py +++ b/tests/test_async_cursor.py @@ -1,4 +1,7 @@ +import gc import pytest +import weakref + import psycopg3 @@ -15,6 +18,15 @@ def test_close(aconn, loop): assert cur.closed +def test_weakref(aconn, loop): + cur = aconn.cursor() + w = weakref.ref(cur) + loop.run_until_complete(cur.close()) + del cur + gc.collect() + assert w() is None + + def test_status(aconn, loop): cur = aconn.cursor() assert cur.status is None diff --git a/tests/test_connection.py b/tests/test_connection.py index 259212149..c6a5af76d 100644 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -1,5 +1,7 @@ +import gc import pytest import logging +import weakref import psycopg3 from psycopg3 import Connection @@ -26,6 +28,15 @@ def test_close(conn): assert conn.status == conn.ConnStatus.BAD +def test_weakref(dsn): + conn = psycopg3.connect(dsn) + w = weakref.ref(conn) + conn.close() + del conn + gc.collect() + assert w() is None + + def test_commit(conn): conn.pgconn.exec_(b"drop table if exists foo") conn.pgconn.exec_(b"create table foo (id int primary key)") diff --git a/tests/test_cursor.py b/tests/test_cursor.py index 29db7007d..d59417aa0 100644 --- a/tests/test_cursor.py +++ b/tests/test_cursor.py @@ -1,4 +1,7 @@ +import gc import pytest +import weakref + import psycopg3 @@ -15,6 +18,15 @@ def test_close(conn): assert cur.closed +def test_weakref(conn): + cur = conn.cursor() + w = weakref.ref(cur) + cur.close() + del cur + gc.collect() + assert w() is None + + def test_status(conn): cur = conn.cursor() assert cur.status is None