Skip to content

Commit 771a546

Browse files
author
Erlend Egeberg Aasland
authored
bpo-45040: Simplify sqlite3 transaction control functions (GH-28019)
1 parent 1d42408 commit 771a546

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
@@ -459,43 +459,29 @@ static PyObject *
459459
pysqlite_connection_commit_impl(pysqlite_Connection *self)
460460
/*[clinic end generated code: output=3da45579e89407f2 input=39c12c04dda276a8]*/
461461
{
462-
int rc;
463-
sqlite3_stmt* statement;
464-
465462
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
466463
return NULL;
467464
}
468465

469466
if (!sqlite3_get_autocommit(self->db)) {
467+
int rc;
470468

471469
Py_BEGIN_ALLOW_THREADS
470+
sqlite3_stmt *statement;
472471
rc = sqlite3_prepare_v2(self->db, "COMMIT", 7, &statement, NULL);
473-
Py_END_ALLOW_THREADS
474-
if (rc != SQLITE_OK) {
475-
_pysqlite_seterror(self->state, self->db);
476-
goto error;
472+
if (rc == SQLITE_OK) {
473+
(void)sqlite3_step(statement);
474+
rc = sqlite3_finalize(statement);
477475
}
478-
479-
rc = pysqlite_step(statement);
480-
if (rc != SQLITE_DONE) {
481-
_pysqlite_seterror(self->state, self->db);
482-
}
483-
484-
Py_BEGIN_ALLOW_THREADS
485-
rc = sqlite3_finalize(statement);
486476
Py_END_ALLOW_THREADS
487-
if (rc != SQLITE_OK && !PyErr_Occurred()) {
488-
_pysqlite_seterror(self->state, self->db);
489-
}
490477

478+
if (rc != SQLITE_OK) {
479+
(void)_pysqlite_seterror(self->state, self->db);
480+
return NULL;
481+
}
491482
}
492483

493-
error:
494-
if (PyErr_Occurred()) {
495-
return NULL;
496-
} else {
497-
Py_RETURN_NONE;
498-
}
484+
Py_RETURN_NONE;
499485
}
500486

501487
/*[clinic input]
@@ -508,44 +494,32 @@ static PyObject *
508494
pysqlite_connection_rollback_impl(pysqlite_Connection *self)
509495
/*[clinic end generated code: output=b66fa0d43e7ef305 input=12d4e8d068942830]*/
510496
{
511-
int rc;
512-
sqlite3_stmt* statement;
513-
514497
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
515498
return NULL;
516499
}
517500

518501
if (!sqlite3_get_autocommit(self->db)) {
519502
pysqlite_do_all_statements(self);
520503

504+
int rc;
505+
521506
Py_BEGIN_ALLOW_THREADS
507+
sqlite3_stmt *statement;
522508
rc = sqlite3_prepare_v2(self->db, "ROLLBACK", 9, &statement, NULL);
523-
Py_END_ALLOW_THREADS
524-
if (rc != SQLITE_OK) {
525-
_pysqlite_seterror(self->state, self->db);
526-
goto error;
527-
}
528-
529-
rc = pysqlite_step(statement);
530-
if (rc != SQLITE_DONE) {
531-
_pysqlite_seterror(self->state, self->db);
509+
if (rc == SQLITE_OK) {
510+
(void)sqlite3_step(statement);
511+
rc = sqlite3_finalize(statement);
532512
}
533-
534-
Py_BEGIN_ALLOW_THREADS
535-
rc = sqlite3_finalize(statement);
536513
Py_END_ALLOW_THREADS
537-
if (rc != SQLITE_OK && !PyErr_Occurred()) {
538-
_pysqlite_seterror(self->state, self->db);
514+
515+
if (rc != SQLITE_OK) {
516+
(void)_pysqlite_seterror(self->state, self->db);
517+
return NULL;
539518
}
540519

541520
}
542521

543-
error:
544-
if (PyErr_Occurred()) {
545-
return NULL;
546-
} else {
547-
Py_RETURN_NONE;
548-
}
522+
Py_RETURN_NONE;
549523
}
550524

551525
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)