Skip to content

Commit f3a0531

Browse files
[3.14] gh-156101: Fix sqlite3 Cursor.arraysize on a failed assignment (GH-156105) (GH-156157)
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 53760b3) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent 31d0cf9 commit f3a0531

3 files changed

Lines changed: 14 additions & 1 deletion

File tree

Lib/test/test_sqlite3/test_dbapi.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,9 +1083,14 @@ def test_invalid_array_size(self):
10831083
UINT32_MAX = (1 << 32) - 1
10841084
setter = functools.partial(setattr, self.cu, 'arraysize')
10851085

1086+
self.cu.arraysize = 2
10861087
self.assertRaises(TypeError, setter, 1.0)
10871088
self.assertRaises(ValueError, setter, -3)
10881089
self.assertRaises(OverflowError, setter, UINT32_MAX + 1)
1090+
self.assertRaises(OverflowError, setter, 2**1000)
1091+
self.assertRaises(ValueError, setter, -2**1000)
1092+
# a failed assignment does not change the value
1093+
self.assertEqual(self.cu.arraysize, 2)
10891094

10901095
def test_fetchmany(self):
10911096
# no active SQL statement
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix :attr:`sqlite3.Cursor.arraysize` being set to 0 if the assigned value is
2+
too large.
3+
The attribute is now left unchanged if the assignment fails.

Modules/_sqlite/cursor.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1321,7 +1321,12 @@ static int
13211321
_sqlite3_Cursor_arraysize_set_impl(pysqlite_Cursor *self, PyObject *value)
13221322
/*[clinic end generated code: output=af59a6b09f8cce6e input=ace48cb114e26060]*/
13231323
{
1324-
return PyLong_AsUInt32(value, &self->arraysize);
1324+
uint32_t arraysize;
1325+
if (PyLong_AsUInt32(value, &arraysize) < 0) {
1326+
return -1;
1327+
}
1328+
self->arraysize = arraysize;
1329+
return 0;
13251330
}
13261331

13271332
static PyMethodDef cursor_methods[] = {

0 commit comments

Comments
 (0)