Skip to content

Commit 0ec285a

Browse files
author
Erlend E. Aasland
committed
bpo-45040: Simplify sqlite3 transaction control functions
1 parent eb263f9 commit 0ec285a

File tree

2 files changed

+28
-63
lines changed

2 files changed

+28
-63
lines changed

Modules/_sqlite/connection.c

Lines changed: 21 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -396,43 +396,29 @@ static PyObject *
396396
pysqlite_connection_commit_impl(pysqlite_Connection *self)
397397
/*[clinic end generated code: output=3da45579e89407f2 input=39c12c04dda276a8]*/
398398
{
399-
int rc;
400-
sqlite3_stmt* statement;
401-
402399
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
403400
return NULL;
404401
}
405402

406403
if (!sqlite3_get_autocommit(self->db)) {
404+
int rc;
407405

408406
Py_BEGIN_ALLOW_THREADS
407+
sqlite3_stmt *statement;
409408
rc = sqlite3_prepare_v2(self->db, "COMMIT", 7, &statement, NULL);
410-
Py_END_ALLOW_THREADS
411-
if (rc != SQLITE_OK) {
412-
_pysqlite_seterror(self->state, self->db);
413-
goto error;
409+
if (rc == SQLITE_OK) {
410+
(void)sqlite3_step(statement);
411+
rc = sqlite3_finalize(statement);
414412
}
415-
416-
rc = pysqlite_step(statement);
417-
if (rc != SQLITE_DONE) {
418-
_pysqlite_seterror(self->state, self->db);
419-
}
420-
421-
Py_BEGIN_ALLOW_THREADS
422-
rc = sqlite3_finalize(statement);
423413
Py_END_ALLOW_THREADS
424-
if (rc != SQLITE_OK && !PyErr_Occurred()) {
425-
_pysqlite_seterror(self->state, self->db);
426-
}
427414

415+
if (rc != SQLITE_OK) {
416+
(void)_pysqlite_seterror(self->state, self->db);
417+
return NULL;
418+
}
428419
}
429420

430-
error:
431-
if (PyErr_Occurred()) {
432-
return NULL;
433-
} else {
434-
Py_RETURN_NONE;
435-
}
421+
Py_RETURN_NONE;
436422
}
437423

438424
/*[clinic input]
@@ -445,44 +431,32 @@ static PyObject *
445431
pysqlite_connection_rollback_impl(pysqlite_Connection *self)
446432
/*[clinic end generated code: output=b66fa0d43e7ef305 input=12d4e8d068942830]*/
447433
{
448-
int rc;
449-
sqlite3_stmt* statement;
450-
451434
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
452435
return NULL;
453436
}
454437

455438
if (!sqlite3_get_autocommit(self->db)) {
456439
pysqlite_do_all_statements(self);
457440

441+
int rc;
442+
458443
Py_BEGIN_ALLOW_THREADS
444+
sqlite3_stmt *statement;
459445
rc = sqlite3_prepare_v2(self->db, "ROLLBACK", 9, &statement, NULL);
460-
Py_END_ALLOW_THREADS
461-
if (rc != SQLITE_OK) {
462-
_pysqlite_seterror(self->state, self->db);
463-
goto error;
464-
}
465-
466-
rc = pysqlite_step(statement);
467-
if (rc != SQLITE_DONE) {
468-
_pysqlite_seterror(self->state, self->db);
446+
if (rc == SQLITE_OK) {
447+
(void)sqlite3_step(statement);
448+
rc = sqlite3_finalize(statement);
469449
}
470-
471-
Py_BEGIN_ALLOW_THREADS
472-
rc = sqlite3_finalize(statement);
473450
Py_END_ALLOW_THREADS
474-
if (rc != SQLITE_OK && !PyErr_Occurred()) {
475-
_pysqlite_seterror(self->state, self->db);
451+
452+
if (rc != SQLITE_OK) {
453+
(void)_pysqlite_seterror(self->state, self->db);
454+
return NULL;
476455
}
477456

478457
}
479458

480-
error:
481-
if (PyErr_Occurred()) {
482-
return NULL;
483-
} else {
484-
Py_RETURN_NONE;
485-
}
459+
Py_RETURN_NONE;
486460
}
487461

488462
static int

Modules/_sqlite/cursor.c

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -431,31 +431,22 @@ static int
431431
begin_transaction(pysqlite_Connection *self)
432432
{
433433
int rc;
434-
sqlite3_stmt *statement;
435434

436435
Py_BEGIN_ALLOW_THREADS
436+
sqlite3_stmt *statement;
437437
rc = sqlite3_prepare_v2(self->db, self->begin_statement, -1, &statement,
438438
NULL);
439-
Py_END_ALLOW_THREADS
440-
441-
if (rc != SQLITE_OK) {
442-
_pysqlite_seterror(self->state, self->db);
443-
goto error;
439+
if (rc == SQLITE_OK) {
440+
(void)sqlite3_step(statement);
441+
rc = sqlite3_finalize(statement);
444442
}
445-
446-
Py_BEGIN_ALLOW_THREADS
447-
sqlite3_step(statement);
448-
rc = sqlite3_finalize(statement);
449443
Py_END_ALLOW_THREADS
450444

451-
if (rc != SQLITE_OK && !PyErr_Occurred()) {
452-
_pysqlite_seterror(self->state, self->db);
453-
}
454-
455-
error:
456-
if (PyErr_Occurred()) {
445+
if (rc != SQLITE_OK) {
446+
(void)_pysqlite_seterror(self->state, self->db);
457447
return -1;
458448
}
449+
459450
return 0;
460451
}
461452

0 commit comments

Comments
 (0)