From dcd63e7ea56d8d3ab67c7a364d5f5e8140520a6a Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Fri, 21 Aug 2026 10:12:58 +0300 Subject: [PATCH] gh-156101: Fix sqlite3 Cursor.arraysize on a failed assignment (GH-156105) PyLong_AsUInt32() stores 0 in the target on error, so the attribute was clobbered when the assigned value was too large. (cherry picked from commit 53760b3f8d76ecc5a69204b880afcec6d8ed706f) Co-authored-by: Serhiy Storchaka --- Lib/test/test_sqlite3/test_dbapi.py | 5 +++++ .../Library/2026-08-20-12-10-00.gh-issue-156101.Qb2xNv.rst | 3 +++ Modules/_sqlite/cursor.c | 7 ++++++- 3 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2026-08-20-12-10-00.gh-issue-156101.Qb2xNv.rst diff --git a/Lib/test/test_sqlite3/test_dbapi.py b/Lib/test/test_sqlite3/test_dbapi.py index 7165729cd524f01..0bb3fb80f04c5ca 100644 --- a/Lib/test/test_sqlite3/test_dbapi.py +++ b/Lib/test/test_sqlite3/test_dbapi.py @@ -1083,9 +1083,14 @@ def test_invalid_array_size(self): UINT32_MAX = (1 << 32) - 1 setter = functools.partial(setattr, self.cu, 'arraysize') + self.cu.arraysize = 2 self.assertRaises(TypeError, setter, 1.0) self.assertRaises(ValueError, setter, -3) self.assertRaises(OverflowError, setter, UINT32_MAX + 1) + self.assertRaises(OverflowError, setter, 2**1000) + self.assertRaises(ValueError, setter, -2**1000) + # a failed assignment does not change the value + self.assertEqual(self.cu.arraysize, 2) def test_fetchmany(self): # no active SQL statement diff --git a/Misc/NEWS.d/next/Library/2026-08-20-12-10-00.gh-issue-156101.Qb2xNv.rst b/Misc/NEWS.d/next/Library/2026-08-20-12-10-00.gh-issue-156101.Qb2xNv.rst new file mode 100644 index 000000000000000..817f4a7207d7fa1 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-08-20-12-10-00.gh-issue-156101.Qb2xNv.rst @@ -0,0 +1,3 @@ +Fix :attr:`sqlite3.Cursor.arraysize` being set to 0 if the assigned value is +too large. +The attribute is now left unchanged if the assignment fails. diff --git a/Modules/_sqlite/cursor.c b/Modules/_sqlite/cursor.c index cb0f9adcc45a96b..a4b5769040282af 100644 --- a/Modules/_sqlite/cursor.c +++ b/Modules/_sqlite/cursor.c @@ -1321,7 +1321,12 @@ static int _sqlite3_Cursor_arraysize_set_impl(pysqlite_Cursor *self, PyObject *value) /*[clinic end generated code: output=af59a6b09f8cce6e input=ace48cb114e26060]*/ { - return PyLong_AsUInt32(value, &self->arraysize); + uint32_t arraysize; + if (PyLong_AsUInt32(value, &arraysize) < 0) { + return -1; + } + self->arraysize = arraysize; + return 0; } static PyMethodDef cursor_methods[] = {