diff --git a/aiomysql/connection.py b/aiomysql/connection.py index 3f4b9e04..f2f84bc0 100644 --- a/aiomysql/connection.py +++ b/aiomysql/connection.py @@ -589,6 +589,7 @@ def _write_bytes(self, data): return self._writer.write(data) async def _read_query_result(self, unbuffered=False): + self._result = None if unbuffered: try: result = MySQLResult(self) diff --git a/aiomysql/cursors.py b/aiomysql/cursors.py index d337e3f2..059abc85 100644 --- a/aiomysql/cursors.py +++ b/aiomysql/cursors.py @@ -184,6 +184,8 @@ async def nextset(self): return if not current_result.has_next: return + self._result = None + self._clear_result() await conn.next_result() await self._do_get_result() return True @@ -449,9 +451,19 @@ def scroll(self, value, mode='relative'): async def _query(self, q): conn = self._get_db() self._last_executed = q + self._clear_result() await conn.query(q) await self._do_get_result() + def _clear_result(self): + self._rownumber = 0 + self._result = None + + self._rowcount = 0 + self._description = None + self._lastrowid = None + self._rows = None + async def _do_get_result(self): conn = self._get_db() self._rownumber = 0 diff --git a/tests/test_nextset.py b/tests/test_nextset.py index 58b7e536..ca897331 100644 --- a/tests/test_nextset.py +++ b/tests/test_nextset.py @@ -1,4 +1,7 @@ +import asyncio + import pytest +from pymysql.err import ProgrammingError @pytest.mark.run_loop @@ -27,6 +30,15 @@ async def test_skip_nextset(cursor): assert [(42,)] == list(r) +@pytest.mark.run_loop +async def test_nextset_error(cursor): + await cursor.execute("SELECT 1; xyzzy;") + + # nextset shouldn't hang on error, it should raise syntax error + with pytest.raises(ProgrammingError): + await asyncio.wait_for(cursor.nextset(), 5) + + @pytest.mark.run_loop async def test_ok_and_next(cursor): await cursor.execute("SELECT 1; commit; SELECT 2;")