gh-156101: Fix sqlite3 Cursor.arraysize on a failed assignment (GH-156105)

serhiy-storchaka <[email protected]>
Newsgroups gmane.comp.python.cvs
Message-ID <[email protected]>
https://github.com/python/cpython/commit/53760b3f8d76ecc5a69204b880afcec6d8ed706f
commit: 53760b3f8d76ecc5a69204b880afcec6d8ed706f
branch: main
author: Serhiy Storchaka <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2026-08-21T10:12:58+03:00
summary:

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.

files:
A Misc/NEWS.d/next/Library/2026-08-20-12-10-00.gh-issue-156101.Qb2xNv.rst
M Lib/test/test_sqlite3/test_dbapi.py
M Modules/_sqlite/cursor.c

diff --git a/Lib/test/test_sqlite3/test_dbapi.py b/Lib/test/test_sqlite3/test_dbapi.py
index c21448a92361d7..375f12e8d4791d 100644
--- a/Lib/test/test_sqlite3/test_dbapi.py
+++ b/Lib/test/test_sqlite3/test_dbapi.py
@@ -1075,9 +1075,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 00000000000000..817f4a7207d7fa
--- /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 5a61e43617984d..3778ccd32fe4c4 100644
--- a/Modules/_sqlite/cursor.c
+++ b/Modules/_sqlite/cursor.c
@@ -1378,7 +1378,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[] = {

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.