summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorErlend Egeberg Aasland <erlend.aasland@innova.no>2021-08-28 18:26:00 (GMT)
committerGitHub <noreply@github.com>2021-08-28 18:26:00 (GMT)
commit2a80893e5c023a73ccd32cc319f4f0404f548c00 (patch)
tree4af5a963d284756e2654bbfa4b8856d8a7204d09 /Modules
parent81fa08c5ea2cf15254b951034b9d6c7358f96d79 (diff)
downloadcpython-2a80893e5c023a73ccd32cc319f4f0404f548c00.zip
cpython-2a80893e5c023a73ccd32cc319f4f0404f548c00.tar.gz
cpython-2a80893e5c023a73ccd32cc319f4f0404f548c00.tar.bz2
[3.10] bpo-27334: roll back transaction if sqlite3 context manager fails to commit (GH-26202) (GH-27943)
Diffstat (limited to 'Modules')
-rw-r--r--Modules/_sqlite/connection.c29
1 files changed, 22 insertions, 7 deletions
diff --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c
index f060ef1..5813e34 100644
--- a/Modules/_sqlite/connection.c
+++ b/Modules/_sqlite/connection.c
@@ -1871,17 +1871,32 @@ 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);
}
-
- result = PyObject_CallMethod((PyObject*)self, method_name, NULL);
- if (!result) {
+ else {
+ result = pysqlite_connection_rollback_impl(self);
+ }
+
+ 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 {
+ Py_DECREF(result);
+ PyErr_Restore(exc, val, tb);
+ }
+ }
return NULL;
}
Py_DECREF(result);