]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Fix executemany crash with empty input sequence
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Tue, 14 Dec 2021 21:42:18 +0000 (22:42 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Tue, 14 Dec 2021 22:07:09 +0000 (23:07 +0100)
Close #179.

docs/news.rst
psycopg/psycopg/cursor.py
tests/test_cursor.py
tests/test_cursor_async.py

index c0cfc423b77fab8c3bd7d436ede918ba912830e3..db6e3bf5be079febbbd401c35cd255763f783444 100644 (file)
@@ -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
 ---------------
 
index 6a69f411e77da92e55d75a7432dc089479e8cde8..03db0a50b3e6c3b2a949b3de74647e85dc5f358b 100644 (file)
@@ -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.
index 0874aede972e2ab07f568cc151febb2d38143fd0..42c8bed37b5fb936a70e0ff295d10586b4d19198 100644 (file)
@@ -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(
index 70bd3fc571d81ec2c6a2a9191f6cfc29f062da63..ef92692c63751021d6b5907aa724061a709b41fd 100644 (file)
@@ -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(