Correctly detect an empty query sent to the backend

Closes ticket #46.
This commit is contained in:
Daniele Varrazzo 2011-03-04 20:30:43 +00:00
parent f1d69f6dec
commit fcbe0466a6
3 changed files with 19 additions and 0 deletions

6
NEWS
View File

@ -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
-------------------------

View File

@ -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);

View File

@ -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()