Skip to content

Commit b079374

Browse files
committed
Don't fetch warnings when there is next result
Fixes PyMySQL#48
1 parent 4154756 commit b079374

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

MySQLdb/cursors.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,14 @@ def _check_executed(self):
109109
def _warning_check(self):
110110
from warnings import warn
111111
if self._warnings:
112+
# When there is next result, fetching warnings cause "command
113+
# out of sync" error.
114+
if self._result.has_next:
115+
msg = "There are %d MySQL warnings." % (self._warnings,)
116+
self.messages.append(msg)
117+
warn(msg, self.Warning, 3)
118+
return
119+
112120
warnings = self._get_db().show_warnings()
113121
if warnings:
114122
# This is done in two loops in case
@@ -204,23 +212,21 @@ def execute(self, query, args=None):
204212
if isinstance(query, unicode):
205213
query = query.encode(db.unicode_literal.charset, 'surrogateescape')
206214

215+
res = None
207216
try:
208-
r = None
209-
r = self._query(query)
217+
res = self._query(query)
210218
except TypeError as m:
211219
if m.args[0] in ("not enough arguments for format string",
212220
"not all arguments converted"):
213221
self.errorhandler(self, ProgrammingError, m.args[0])
214222
else:
215223
self.errorhandler(self, TypeError, m)
216-
except (SystemExit, KeyboardInterrupt):
217-
raise
218-
except:
224+
except Exception:
219225
exc, value = sys.exc_info()[:2]
220226
self.errorhandler(self, exc, value)
221227
self._executed = query
222228
if not self._defer_warnings: self._warning_check()
223-
return r
229+
return res
224230

225231
def executemany(self, query, args):
226232
"""Execute a multi-row query.

_mysql.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ typedef struct {
8888
MYSQL_RES *result;
8989
int nfields;
9090
int use;
91+
char has_next;
9192
PyObject *converter;
9293
} _mysql_ResultObject;
9394

@@ -406,6 +407,7 @@ _mysql_ResultObject_Initialize(
406407
else
407408
result = mysql_store_result(&(conn->connection));
408409
self->result = result;
410+
self->has_next = (char)mysql_more_results(&(conn->connection));
409411
Py_END_ALLOW_THREADS ;
410412
if (!result) {
411413
if (mysql_errno(&(conn->connection))) {
@@ -1520,7 +1522,7 @@ _mysql__fetch_row(
15201522
int maxrows,
15211523
_PYFUNC *convert_row)
15221524
{
1523-
unsigned int i;
1525+
int i;
15241526
MYSQL_ROW row;
15251527

15261528
for (i = skiprows; i<(skiprows+maxrows); i++) {
@@ -1573,14 +1575,14 @@ _mysql_ResultObject_fetch_row(
15731575
_mysql_row_to_dict_old
15741576
};
15751577
_PYFUNC *convert_row;
1576-
unsigned int maxrows=1, how=0, skiprows=0, rowsadded;
1578+
int maxrows=1, how=0, skiprows=0, rowsadded;
15771579
PyObject *r=NULL;
15781580

15791581
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ii:fetch_row", kwlist,
15801582
&maxrows, &how))
15811583
return NULL;
15821584
check_result_connection(self);
1583-
if (how >= sizeof(row_converters)) {
1585+
if (how >= (int)sizeof(row_converters)) {
15841586
PyErr_SetString(PyExc_ValueError, "how out of range");
15851587
return NULL;
15861588
}
@@ -2653,6 +2655,13 @@ static struct PyMemberDef _mysql_ResultObject_memberlist[] = {
26532655
READONLY,
26542656
"Type conversion mapping"
26552657
},
2658+
{
2659+
"has_next",
2660+
T_BOOL,
2661+
offsetof(_mysql_ResultObject, has_next),
2662+
READONLY,
2663+
"Has next result"
2664+
},
26562665
{NULL} /* Sentinel */
26572666
};
26582667

0 commit comments

Comments
 (0)