Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Fix setting autocommit to an out of range integer
LEGACY_TRANSACTION_CONTROL is -1, the same value which PyLong_AsLong()
returns on error, so the converter reported success with OverflowError set.
  • Loading branch information
serhiy-storchaka committed Aug 20, 2026
commit 3b4b844a1a29ef8cd8686e61f40809c64e4a1ad4
8 changes: 7 additions & 1 deletion Lib/test/test_sqlite3/test_transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,10 +389,16 @@ def test_autocommit_setget(self):

def test_autocommit_setget_invalid(self):
msg = "autocommit must be True, False, or.*LEGACY"
for mode in "a", 12, (), None:
for mode in "a", 12, (), None, 2**1000, -2**1000:
with self.subTest(mode=mode):
with self.assertRaisesRegex(ValueError, msg):
sqlite.connect(":memory:", autocommit=mode)
with memory_database() as cx:
with self.assertRaisesRegex(ValueError, msg):
cx.autocommit = mode
# a failed assignment does not change the value
self.assertEqual(cx.autocommit,
sqlite.LEGACY_TRANSACTION_CONTROL)

def test_autocommit_delete(self):
with memory_database() as cx:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Fix a crash when deleting the :attr:`~sqlite3.Connection.autocommit`
attribute of :class:`sqlite3.Connection`.
It now raises :exc:`AttributeError`.
Fix crashes in :class:`sqlite3.Connection` when deleting the
:attr:`~sqlite3.Connection.autocommit` attribute or setting it to an integer
which does not fit in C :c:expr:`long`.
Both now raise an exception.
15 changes: 10 additions & 5 deletions Modules/_sqlite/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,16 @@ autocommit_converter(PyObject *val, enum autocommit_mode *result)
*result = AUTOCOMMIT_DISABLED;
return 1;
}
if (PyLong_Check(val) &&
PyLong_AsLong(val) == LEGACY_TRANSACTION_CONTROL)
{
*result = AUTOCOMMIT_LEGACY;
return 1;
if (PyLong_Check(val)) {
int overflow;
long value = PyLong_AsLongAndOverflow(val, &overflow);
if (value == -1 && PyErr_Occurred()) {
return 0;
}
if (!overflow && value == LEGACY_TRANSACTION_CONTROL) {
*result = AUTOCOMMIT_LEGACY;
return 1;
}
}

PyErr_SetString(PyExc_ValueError,
Expand Down
Loading