Merge branch 'empty-query' into devel

This commit is contained in:
Daniele Varrazzo 2011-03-04 20:33:51 +00:00
commit f34e44b3f4
3 changed files with 25 additions and 2 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

@ -172,16 +172,19 @@ pq_raise(connectionObject *conn, cursorObject *curs, PGresult *pgres)
if (pgres) {
err = PQresultErrorMessage(pgres);
if (err != NULL) {
Dprintf("pq_raise: PQresultErrorMessage: err=%s", err);
code = PQresultErrorField(pgres, PG_DIAG_SQLSTATE);
}
}
if (err == NULL)
if (err == NULL) {
err = PQerrorMessage(conn->pgconn);
Dprintf("pq_raise: PQerrorMessage: err=%s", err);
}
/* if the is no error message we probably called pq_raise without reason:
we need to set an exception anyway because the caller will probably
raise and a meaningful message is better than an empty one */
if (err == NULL) {
if (err == NULL || err[0] == '\0') {
PyErr_SetString(Error, "psycopg went psycotic without error set");
return;
}
@ -194,6 +197,7 @@ pq_raise(connectionObject *conn, cursorObject *curs, PGresult *pgres)
/* try to remove the initial "ERROR: " part from the postgresql error */
err2 = strip_severity(err);
Dprintf("pq_raise: err2=%s", err2);
psyco_set_error(exc, curs, err2, err, code);
}
@ -1355,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()