From: Daniele Varrazzo Date: Tue, 14 Dec 2021 21:42:18 +0000 (+0100) Subject: Fix executemany crash with empty input sequence X-Git-Tag: pool-3.1~75 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=aab65009e0e829cb35211bf7d109633226b9bdea;p=thirdparty%2Fpsycopg.git Fix executemany crash with empty input sequence Close #179. --- diff --git a/docs/news.rst b/docs/news.rst index c0cfc423b..db6e3bf5b 100644 --- a/docs/news.rst +++ b/docs/news.rst @@ -14,6 +14,13 @@ Psycopg 3.1 (unreleased) - Add `pq.PGconn.trace()` and related trace functions (:ticket:`#167`). +Psycopg 3.0.7 (unreleased) +^^^^^^^^^^^^^^^^^^^^^^^^^^ + +- Fix crash in `~Cursor.executemany()` with no input sequence + (:ticket:`#179`). + + Current release --------------- diff --git a/psycopg/psycopg/cursor.py b/psycopg/psycopg/cursor.py index 6a69f411e..03db0a50b 100644 --- a/psycopg/psycopg/cursor.py +++ b/psycopg/psycopg/cursor.py @@ -225,7 +225,9 @@ class BaseCursor(Generic[ConnectionType, Row]): for res in results: nrows += res.command_tuples or 0 - self._set_result(0) + if self._results: + self._set_result(0) + # Override rowcout for the first result. Calls to nextset() will change # it to the value of that result only, but we hope nobody will notice. # You haven't read this comment. diff --git a/tests/test_cursor.py b/tests/test_cursor.py index 0874aede9..42c8bed37 100644 --- a/tests/test_cursor.py +++ b/tests/test_cursor.py @@ -267,6 +267,12 @@ def test_executemany_name(conn, execmany): assert cur.fetchall() == [(11, "hello"), (21, "world")] +def test_executemany_no_data(conn, execmany): + cur = conn.cursor() + cur.executemany("insert into execmany(num, data) values (%s, %s)", []) + assert cur.rowcount == 0 + + def test_executemany_rowcount(conn, execmany): cur = conn.cursor() cur.executemany( diff --git a/tests/test_cursor_async.py b/tests/test_cursor_async.py index 70bd3fc57..ef92692c6 100644 --- a/tests/test_cursor_async.py +++ b/tests/test_cursor_async.py @@ -266,6 +266,14 @@ async def test_executemany_name(aconn, execmany): assert rv == [(11, "hello"), (21, "world")] +async def test_executemany_no_data(aconn, execmany): + cur = aconn.cursor() + await cur.executemany( + "insert into execmany(num, data) values (%s, %s)", [] + ) + assert cur.rowcount == 0 + + async def test_executemany_rowcount(aconn, execmany): cur = aconn.cursor() await cur.executemany(