Skip to content

Commit def9193

Browse files
author
Erlend Egeberg Aasland
authored
bpo-43505: Explicitly initialize and shutdown sqlite3 (GH-25404)
1 parent d9ba9de commit def9193

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

Modules/_sqlite/module.c

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,7 @@ static struct PyModuleDef _sqlite3module = {
343343
#define ADD_TYPE(module, type) \
344344
do { \
345345
if (PyModule_AddType(module, &type) < 0) { \
346-
Py_DECREF(module); \
347-
return NULL; \
346+
goto error; \
348347
} \
349348
} while (0)
350349

@@ -370,6 +369,12 @@ PyMODINIT_FUNC PyInit__sqlite3(void)
370369
return NULL;
371370
}
372371

372+
int rc = sqlite3_initialize();
373+
if (rc != SQLITE_OK) {
374+
PyErr_SetString(PyExc_ImportError, sqlite3_errstr(rc));
375+
return NULL;
376+
}
377+
373378
module = PyModule_Create(&_sqlite3module);
374379

375380
if (!module ||
@@ -380,8 +385,7 @@ PyMODINIT_FUNC PyInit__sqlite3(void)
380385
(pysqlite_statement_setup_types(module) < 0) ||
381386
(pysqlite_prepare_protocol_setup_types(module) < 0)
382387
) {
383-
Py_XDECREF(module);
384-
return NULL;
388+
goto error;
385389
}
386390

387391
ADD_TYPE(module, *pysqlite_ConnectionType);
@@ -428,12 +432,11 @@ PyMODINIT_FUNC PyInit__sqlite3(void)
428432
goto error;
429433
}
430434

431-
error:
432-
if (PyErr_Occurred())
433-
{
434-
PyErr_SetString(PyExc_ImportError, MODULE_NAME ": init failed");
435-
Py_DECREF(module);
436-
module = NULL;
437-
}
438435
return module;
436+
437+
error:
438+
sqlite3_shutdown();
439+
PyErr_SetString(PyExc_ImportError, MODULE_NAME ": init failed");
440+
Py_XDECREF(module);
441+
return NULL;
439442
}

0 commit comments

Comments
 (0)