]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Make sure connections and cursors can be gc'd correctly
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Fri, 22 May 2020 06:32:03 +0000 (18:32 +1200)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Fri, 22 May 2020 17:06:33 +0000 (05:06 +1200)
psycopg3/pq/pq_cython.pxd
tests/pq/test_pgconn.py
tests/test_async_connection.py
tests/test_async_cursor.py
tests/test_connection.py
tests/test_cursor.py

index 0acf6a85e950581012d0e3cbe2e3f52be0332d52..479b85c4d676bc84a343dc07c2ba3531e713796a 100644 (file)
@@ -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)
index a0bafc6b11be35895ee0e5d571f47b508d6eedcd..473f8c459331fb9e9a20142391ae3ecd465327da 100644 (file)
@@ -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
index 75ec95e2bcf9b9fe3b3ac8c36220c408bcba6347..6aa10317d3e86ed9c0f82271893b582e5d1c8ac2 100644 (file)
@@ -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)")
index 5c428853aeef79fe569ce55f99ff3826ffa61b5f..afda8070fcce21bbabb96a874b2005740288a2c3 100644 (file)
@@ -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
index 25921214925d366e81a991591df28972a7080147..c6a5af76da0fecf32ab9f7641b3bbf1fa0872212 100644 (file)
@@ -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)")
index 29db7007deb8c06f2216d581556ea5cce154b0c7..d59417aa01ba9d6cc4f7ed4663b87b9af189ec01 100644 (file)
@@ -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