Skip to content

Commit 4fde656

Browse files
committed
fix: avoid failed assert passing more arguments than placeholders
Fix #1791
1 parent 8308c19 commit 4fde656

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ What's new in psycopg 2.9.11
55
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66

77
- Add support for Python 3.14.
8+
- Avoid a segfault passing more arguments than placeholders if Python is built
9+
with assertions enabled (:ticket:`#1791`).
810
- `~psycopg2.errorcodes` map and `~psycopg2.errors` classes updated to
911
PostgreSQL 18.
1012
- Drop support for Python 3.8.

psycopg/cursor_type.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ _psyco_curs_merge_query_args(cursorObject *self,
342342
if (PyObject_HasAttrString(arg, "args")) {
343343
PyObject *args = PyObject_GetAttrString(arg, "args");
344344
PyObject *str = PySequence_GetItem(args, 0);
345-
const char *s = Bytes_AS_STRING(str);
345+
const char *s = PyUnicode_AsUTF8(str);
346346

347347
Dprintf("curs_execute: -> %s", s);
348348

tests/test_cursor.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,11 @@ def test_bad_placeholder(self):
139139
self.assertRaises(psycopg2.ProgrammingError,
140140
cur.mogrify, "select %(foo, %(bar)", {'foo': 1, 'bar': 2})
141141

142+
def test_bad_params_number(self):
143+
cur = self.conn.cursor()
144+
self.assertRaises(IndexError, cur.execute, "select %s, %s", [1])
145+
self.assertRaises(TypeError, cur.execute, "select %s", [1, 2])
146+
142147
def test_cast(self):
143148
curs = self.conn.cursor()
144149

0 commit comments

Comments
 (0)