bpo-27334: roll back transaction if sqlite3 context manager fails to commit (GH-26202)

Co-authored-by: Luca Citi
Co-authored-by: Berker Peksag <berker.peksag@gmail.com>
This commit is contained in:
Erlend Egeberg Aasland 2021-08-25 12:59:42 +02:00 committed by GitHub
parent 3df0fc89bc
commit 7ecd3425d4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 101 additions and 7 deletions

View file

@ -1785,17 +1785,31 @@ pysqlite_connection_exit_impl(pysqlite_Connection *self, PyObject *exc_type,
PyObject *exc_value, PyObject *exc_tb)
/*[clinic end generated code: output=0705200e9321202a input=bd66f1532c9c54a7]*/
{
const char* method_name;
int commit = 0;
PyObject* result;
if (exc_type == Py_None && exc_value == Py_None && exc_tb == Py_None) {
method_name = "commit";
} else {
method_name = "rollback";
commit = 1;
result = pysqlite_connection_commit_impl(self);
}
else {
result = pysqlite_connection_rollback_impl(self);
}
result = PyObject_CallMethod((PyObject*)self, method_name, NULL);
if (!result) {
if (result == NULL) {
if (commit) {
/* Commit failed; try to rollback in order to unlock the database.
* If rollback also fails, chain the exceptions. */
PyObject *exc, *val, *tb;
PyErr_Fetch(&exc, &val, &tb);
result = pysqlite_connection_rollback_impl(self);
if (result == NULL) {
_PyErr_ChainExceptions(exc, val, tb);
}
else {
PyErr_Restore(exc, val, tb);
}
}
return NULL;
}
Py_DECREF(result);