summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>2018-05-20 13:56:59 +0100
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>2018-05-20 14:00:09 +0100
commitc4da939909ea9bef99bcdef805b68dca6131eac0 (patch)
treea5fd84e8632ebfa341e59f54073ec4b4c7b30423
parent098c00d73e193f09ce9589a6bb574f5833be714e (diff)
downloadpsycopg2-c4da939909ea9bef99bcdef805b68dca6131eac0.tar.gz
Don't raise an exception closing an unused named cursorfix-716
Close #716
-rw-r--r--NEWS1
-rw-r--r--psycopg/cursor_type.c18
-rwxr-xr-xtests/test_cursor.py5
-rwxr-xr-xtests/test_with.py7
4 files changed, 24 insertions, 7 deletions
diff --git a/NEWS b/NEWS
index 5dc4ae0..cb1b581 100644
--- a/NEWS
+++ b/NEWS
@@ -24,6 +24,7 @@ What's new in psycopg 2.7.5
(:ticket:`#677`).
- Maybe fixed building on MSYS2 (as reported in :ticket:`#658`).
- Allow string subclasses in connection and other places (:ticket:`#679`).
+- Don't raise an exception closing an unused named cursor (:ticket:`#716`).
What's new in psycopg 2.7.4
diff --git a/psycopg/cursor_type.c b/psycopg/cursor_type.c
index b7fd187..d73bc3a 100644
--- a/psycopg/cursor_type.c
+++ b/psycopg/cursor_type.c
@@ -59,6 +59,11 @@ psyco_curs_close(cursorObject *self)
char buffer[128];
PGTransactionStatusType status;
+ if (!self->query) {
+ Dprintf("skipping named cursor close because unused");
+ goto close;
+ }
+
if (self->conn) {
status = PQtransactionStatus(self->conn->pgconn);
}
@@ -66,17 +71,18 @@ psyco_curs_close(cursorObject *self)
status = PQTRANS_UNKNOWN;
}
- if (!(status == PQTRANS_UNKNOWN || status == PQTRANS_INERROR)) {
- EXC_IF_NO_MARK(self);
- PyOS_snprintf(buffer, 127, "CLOSE %s", self->qname);
- if (pq_execute(self, buffer, 0, 0, 1) == -1) return NULL;
- }
- else {
+ if (status == PQTRANS_UNKNOWN || status == PQTRANS_INERROR) {
Dprintf("skipping named curs close because tx status %d",
(int)status);
+ goto close;
}
+
+ EXC_IF_NO_MARK(self);
+ PyOS_snprintf(buffer, 127, "CLOSE %s", self->qname);
+ if (pq_execute(self, buffer, 0, 0, 1) == -1) return NULL;
}
+close:
self->closed = 1;
Dprintf("psyco_curs_close: cursor at %p closed", self);
diff --git a/tests/test_cursor.py b/tests/test_cursor.py
index cc8db0f..b3e03d9 100755
--- a/tests/test_cursor.py
+++ b/tests/test_cursor.py
@@ -436,6 +436,11 @@ class CursorTests(ConnectingTestCase):
self.assertEqual([(5,), (6,), (7,)], cur2.fetchall())
@skip_before_postgres(8, 0)
+ def test_named_noop_close(self):
+ cur = self.conn.cursor('test')
+ cur.close()
+
+ @skip_before_postgres(8, 0)
def test_scroll(self):
cur = self.conn.cursor()
cur.execute("select generate_series(0,9)")
diff --git a/tests/test_with.py b/tests/test_with.py
index 1392d85..f26f8f9 100755
--- a/tests/test_with.py
+++ b/tests/test_with.py
@@ -26,7 +26,7 @@ import psycopg2
import psycopg2.extensions as ext
import unittest
-from .testutils import ConnectingTestCase
+from .testutils import ConnectingTestCase, skip_before_postgres
class WithTestCase(ConnectingTestCase):
@@ -215,6 +215,11 @@ class WithCursorTestCase(WithTestCase):
else:
self.fail("where is my exception?")
+ @skip_before_postgres(8, 0)
+ def test_named_with_noop(self):
+ with self.conn.cursor('named') as cur:
+ pass
+
def test_suite():
return unittest.TestLoader().loadTestsFromName(__name__)