mirror of
				https://github.com/psycopg/psycopg2.git
				synced 2025-11-01 00:07:36 +03:00 
			
		
		
		
	Merge branch 'empty-query' into devel
This commit is contained in:
		
						commit
						f34e44b3f4
					
				
							
								
								
									
										6
									
								
								NEWS
									
									
									
									
									
								
							
							
						
						
									
										6
									
								
								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 | What's new in psycopg 2.4 | ||||||
| ------------------------- | ------------------------- | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -172,16 +172,19 @@ pq_raise(connectionObject *conn, cursorObject *curs, PGresult *pgres) | ||||||
|     if (pgres) { |     if (pgres) { | ||||||
|         err = PQresultErrorMessage(pgres); |         err = PQresultErrorMessage(pgres); | ||||||
|         if (err != NULL) { |         if (err != NULL) { | ||||||
|  |             Dprintf("pq_raise: PQresultErrorMessage: err=%s", err); | ||||||
|             code = PQresultErrorField(pgres, PG_DIAG_SQLSTATE); |             code = PQresultErrorField(pgres, PG_DIAG_SQLSTATE); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|     if (err == NULL) |     if (err == NULL) { | ||||||
|         err = PQerrorMessage(conn->pgconn); |         err = PQerrorMessage(conn->pgconn); | ||||||
|  |         Dprintf("pq_raise: PQerrorMessage: err=%s", err); | ||||||
|  |     } | ||||||
| 
 | 
 | ||||||
|     /* if the is no error message we probably called pq_raise without reason:
 |     /* 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 |        we need to set an exception anyway because the caller will probably | ||||||
|        raise and a meaningful message is better than an empty one */ |        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"); |         PyErr_SetString(Error, "psycopg went psycotic without error set"); | ||||||
|         return; |         return; | ||||||
|     } |     } | ||||||
|  | @ -194,6 +197,7 @@ pq_raise(connectionObject *conn, cursorObject *curs, PGresult *pgres) | ||||||
| 
 | 
 | ||||||
|     /* try to remove the initial "ERROR: " part from the postgresql error */ |     /* try to remove the initial "ERROR: " part from the postgresql error */ | ||||||
|     err2 = strip_severity(err); |     err2 = strip_severity(err); | ||||||
|  |     Dprintf("pq_raise: err2=%s", err2); | ||||||
| 
 | 
 | ||||||
|     psyco_set_error(exc, curs, err2, err, code); |     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! */ |         /* don't clear curs->pgres, because it contains the results! */ | ||||||
|         break; |         break; | ||||||
| 
 | 
 | ||||||
|  |     case PGRES_EMPTY_QUERY: | ||||||
|  |         PyErr_SetString(ProgrammingError, | ||||||
|  |             "can't execute an empty query"); | ||||||
|  |         IFCLEARPGRES(curs->pgres); | ||||||
|  |         ex = -1; | ||||||
|  |         break; | ||||||
|  | 
 | ||||||
|     default: |     default: | ||||||
|         Dprintf("pq_fetch: uh-oh, something FAILED: pgconn = %p", curs->conn); |         Dprintf("pq_fetch: uh-oh, something FAILED: pgconn = %p", curs->conn); | ||||||
|         pq_raise(curs->conn, curs, NULL); |         pq_raise(curs->conn, curs, NULL); | ||||||
|  |  | ||||||
|  | @ -37,6 +37,12 @@ class CursorTests(unittest.TestCase): | ||||||
|     def tearDown(self): |     def tearDown(self): | ||||||
|         self.conn.close() |         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): |     def test_executemany_propagate_exceptions(self): | ||||||
|         conn = self.conn |         conn = self.conn | ||||||
|         cur = conn.cursor() |         cur = conn.cursor() | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user