Author: cito
Date: Mon Feb  8 18:15:53 2016
New Revision: 844

Log:
New connection attribute "closed" in pgdb

This can be useful when implementing connection pools, e.g. in SQLAlchemy.

Modified:
   trunk/docs/contents/pgdb/connection.rst
   trunk/pgdb.py
   trunk/pgmodule.c
   trunk/tests/test_dbapi20.py

Modified: trunk/docs/contents/pgdb/connection.rst
==============================================================================
--- trunk/docs/contents/pgdb/connection.rst     Mon Feb  8 16:23:14 2016        
(r843)
+++ trunk/docs/contents/pgdb/connection.rst     Mon Feb  8 18:15:53 2016        
(r844)
@@ -69,6 +69,10 @@
 
     The following attributes are not part of the DB-API 2 standard.
 
+.. attribute:: Connection.closed
+
+    This is *True* if the connection has been closed or has become invalid
+
 .. attribute:: Connection.cursor_type
 
     The default cursor type used by the connection

Modified: trunk/pgdb.py
==============================================================================
--- trunk/pgdb.py       Mon Feb  8 16:23:14 2016        (r843)
+++ trunk/pgdb.py       Mon Feb  8 18:15:53 2016        (r844)
@@ -1341,6 +1341,14 @@
         else:
             raise _op_error("Connection has been closed")
 
+    @property
+    def closed(self):
+        """Check whether the connection has been closed or is broken."""
+        try:
+            return not self._cnx or self._cnx.status != 1
+        except TypeError:
+            return True
+
     def commit(self):
         """Commit any pending transaction to the database."""
         if self._cnx:

Modified: trunk/pgmodule.c
==============================================================================
--- trunk/pgmodule.c    Mon Feb  8 16:23:14 2016        (r843)
+++ trunk/pgmodule.c    Mon Feb  8 18:15:53 2016        (r844)
@@ -1441,7 +1441,7 @@
 static int
 check_cnx_obj(connObject *self)
 {
-       if (!self->valid)
+       if (!self || !self->valid || !self->cnx)
        {
                set_error_msg(OperationalError, "Connection has been closed");
                return 0;
@@ -3629,10 +3629,6 @@
        if (!check_source_obj(self, CHECK_CNX))
                return NULL;
 
-       /* make sure that the connection object is valid */
-       if (!self->pgcnx->cnx)
-               return NULL;
-
        encoding = PQclientEncoding(self->pgcnx->cnx);
 
        if (PyBytes_Check(sql))
@@ -3771,7 +3767,7 @@
 #endif
 
        /* checks validity */
-       if (!check_source_obj(self, CHECK_RESULT | CHECK_DQL))
+       if (!check_source_obj(self, CHECK_RESULT | CHECK_DQL | CHECK_CNX))
                return NULL;
 
        /* checks args */
@@ -3980,7 +3976,6 @@
 
        /* checks validity */
        if (!check_source_obj(self, CHECK_CNX | CHECK_RESULT) ||
-                       !self->pgcnx->cnx ||
                        PQresultStatus(self->result) != PGRES_COPY_IN)
        {
                PyErr_SetString(PyExc_IOError,
@@ -4068,7 +4063,6 @@
 
        /* checks validity */
        if (!check_source_obj(self, CHECK_CNX | CHECK_RESULT) ||
-                       !self->pgcnx->cnx ||
                        PQresultStatus(self->result) != PGRES_COPY_OUT)
        {
                PyErr_SetString(PyExc_IOError,

Modified: trunk/tests/test_dbapi20.py
==============================================================================
--- trunk/tests/test_dbapi20.py Mon Feb  8 16:23:14 2016        (r843)
+++ trunk/tests/test_dbapi20.py Mon Feb  8 18:15:53 2016        (r844)
@@ -419,6 +419,16 @@
         cur.execute("select 1 union select 2 union select 3")
         self.assertEqual([r[0] for r in cur], [1, 2, 3])
 
+    def test_cursor_invalidation(self):
+        con = self._connect()
+        cur = con.cursor()
+        cur.execute("select 1 union select 2")
+        self.assertEqual(cur.fetchone(), (1,))
+        self.assertFalse(con.closed)
+        con.close()
+        self.assertTrue(con.closed)
+        self.assertRaises(pgdb.OperationalError, cur.fetchone)
+
     def test_fetch_2_rows(self):
         Decimal = pgdb.decimal_type()
         values = ('test', pgdb.Binary(b'\xff\x52\xb2'),
_______________________________________________
PyGreSQL mailing list
[email protected]
https://mail.vex.net/mailman/listinfo.cgi/pygresql

Reply via email to