From fcbe0466a6eee8d76aa4aec23e5cc6f5de1a59d8 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Fri, 4 Mar 2011 20:30:43 +0000 Subject: [PATCH] Correctly detect an empty query sent to the backend Closes ticket #46. --- NEWS | 6 ++++++ psycopg/pqpath.c | 7 +++++++ tests/test_cursor.py | 6 ++++++ 3 files changed, 19 insertions(+) diff --git a/NEWS b/NEWS index a7e634e3..1eea8af6 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,9 @@ +What's new in psycopg 2.4.1 +--------------------------- + + - Correctly detect an empty query sent to the backend (ticket #46). + + What's new in psycopg 2.4 ------------------------- diff --git a/psycopg/pqpath.c b/psycopg/pqpath.c index 1a5278b5..8d144dce 100644 --- a/psycopg/pqpath.c +++ b/psycopg/pqpath.c @@ -1359,6 +1359,13 @@ pq_fetch(cursorObject *curs) /* don't clear curs->pgres, because it contains the results! */ break; + case PGRES_EMPTY_QUERY: + PyErr_SetString(ProgrammingError, + "can't execute an empty query"); + IFCLEARPGRES(curs->pgres); + ex = -1; + break; + default: Dprintf("pq_fetch: uh-oh, something FAILED: pgconn = %p", curs->conn); pq_raise(curs->conn, curs, NULL); diff --git a/tests/test_cursor.py b/tests/test_cursor.py index 1860ddc6..836f710d 100755 --- a/tests/test_cursor.py +++ b/tests/test_cursor.py @@ -37,6 +37,12 @@ class CursorTests(unittest.TestCase): def tearDown(self): self.conn.close() + def test_empty_query(self): + cur = self.conn.cursor() + self.assertRaises(psycopg2.ProgrammingError, cur.execute, "") + self.assertRaises(psycopg2.ProgrammingError, cur.execute, " ") + self.assertRaises(psycopg2.ProgrammingError, cur.execute, ";") + def test_executemany_propagate_exceptions(self): conn = self.conn cur = conn.cursor()