From 06d0337c7565e35432fe744713260e2ef3e8a545 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Mon, 5 Sep 2016 18:16:01 -0700 Subject: Avoid calling functions with an empty string as format string Directly pass NULL rather than an empty string. --- Modules/_sqlite/connection.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'Modules/_sqlite/connection.c') diff --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c index 409fa74f16..4a10ce5ad5 100644 --- a/Modules/_sqlite/connection.c +++ b/Modules/_sqlite/connection.c @@ -672,7 +672,7 @@ static void _pysqlite_step_callback(sqlite3_context *context, int argc, sqlite3_ aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*)); if (*aggregate_instance == 0) { - *aggregate_instance = PyObject_CallFunction(aggregate_class, ""); + *aggregate_instance = PyObject_CallFunction(aggregate_class, NULL); if (PyErr_Occurred()) { *aggregate_instance = 0; @@ -744,7 +744,7 @@ void _pysqlite_final_callback(sqlite3_context* context) PyErr_Fetch(&exception, &value, &tb); restore = 1; - function_result = _PyObject_CallMethodId(*aggregate_instance, &PyId_finalize, ""); + function_result = _PyObject_CallMethodId(*aggregate_instance, &PyId_finalize, NULL); Py_DECREF(*aggregate_instance); @@ -960,7 +960,7 @@ static int _progress_handler(void* user_arg) gilstate = PyGILState_Ensure(); #endif - ret = PyObject_CallFunction((PyObject*)user_arg, ""); + ret = PyObject_CallFunction((PyObject*)user_arg, NULL); if (!ret) { if (_enable_callback_tracebacks) { @@ -1291,7 +1291,7 @@ PyObject* pysqlite_connection_execute(pysqlite_Connection* self, PyObject* args) PyObject* result = 0; PyObject* method = 0; - cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, ""); + cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, NULL); if (!cursor) { goto error; } @@ -1320,7 +1320,7 @@ PyObject* pysqlite_connection_executemany(pysqlite_Connection* self, PyObject* a PyObject* result = 0; PyObject* method = 0; - cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, ""); + cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, NULL); if (!cursor) { goto error; } @@ -1349,7 +1349,7 @@ PyObject* pysqlite_connection_executescript(pysqlite_Connection* self, PyObject* PyObject* result = 0; PyObject* method = 0; - cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, ""); + cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, NULL); if (!cursor) { goto error; } @@ -1519,7 +1519,7 @@ pysqlite_connection_create_collation(pysqlite_Connection* self, PyObject* args) goto finally; } - uppercase_name = _PyObject_CallMethodId(name, &PyId_upper, ""); + uppercase_name = _PyObject_CallMethodId(name, &PyId_upper, NULL); if (!uppercase_name) { goto finally; } @@ -1611,7 +1611,7 @@ pysqlite_connection_exit(pysqlite_Connection* self, PyObject* args) method_name = "rollback"; } - result = PyObject_CallMethod((PyObject*)self, method_name, ""); + result = PyObject_CallMethod((PyObject*)self, method_name, NULL); if (!result) { return NULL; } -- cgit v1.2.1 From b7c6a1e345172f19f51922a7e6e1490e109e9aa7 Mon Sep 17 00:00:00 2001 From: Berker Peksag Date: Sun, 11 Sep 2016 15:37:30 +0300 Subject: Issue #28036: Remove unused pysqlite_flush_statement_cache function --- Modules/_sqlite/connection.c | 20 -------------------- 1 file changed, 20 deletions(-) (limited to 'Modules/_sqlite/connection.c') diff --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c index 4a10ce5ad5..aca66fed08 100644 --- a/Modules/_sqlite/connection.c +++ b/Modules/_sqlite/connection.c @@ -202,26 +202,6 @@ int pysqlite_connection_init(pysqlite_Connection* self, PyObject* args, PyObject return 0; } -/* Empty the entire statement cache of this connection */ -void pysqlite_flush_statement_cache(pysqlite_Connection* self) -{ - pysqlite_Node* node; - pysqlite_Statement* statement; - - node = self->statement_cache->first; - - while (node) { - statement = (pysqlite_Statement*)(node->data); - (void)pysqlite_statement_finalize(statement); - node = node->next; - } - - Py_SETREF(self->statement_cache, - (pysqlite_Cache *)PyObject_CallFunction((PyObject *)&pysqlite_CacheType, "O", self)); - Py_DECREF(self); - self->statement_cache->decref_factory = 0; -} - /* action in (ACTION_RESET, ACTION_FINALIZE) */ void pysqlite_do_all_statements(pysqlite_Connection* self, int action, int reset_cursors) { -- cgit v1.2.1 From 1755aae76a4eed379286a660de1be8edb99b44ae Mon Sep 17 00:00:00 2001 From: Berker Peksag Date: Mon, 12 Sep 2016 07:16:43 +0300 Subject: Issue #28037: Use sqlite3_get_autocommit() instead of setting Connection->inTransaction manually Patch adapted from https://github.com/ghaering/pysqlite/commit/9b79188edbc50faa24dc178afe24a10454f3fcad --- Modules/_sqlite/connection.c | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) (limited to 'Modules/_sqlite/connection.c') diff --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c index aca66fed08..d29fafe3fa 100644 --- a/Modules/_sqlite/connection.c +++ b/Modules/_sqlite/connection.c @@ -165,7 +165,6 @@ int pysqlite_connection_init(pysqlite_Connection* self, PyObject* args, PyObject self->statement_cache->decref_factory = 0; Py_DECREF(self); - self->inTransaction = 0; self->detect_types = detect_types; self->timeout = timeout; (void)sqlite3_busy_timeout(self->db, (int)(timeout*1000)); @@ -385,9 +384,7 @@ PyObject* _pysqlite_connection_begin(pysqlite_Connection* self) } rc = pysqlite_step(statement, self); - if (rc == SQLITE_DONE) { - self->inTransaction = 1; - } else { + if (rc != SQLITE_DONE) { _pysqlite_seterror(self->db, statement); } @@ -418,7 +415,7 @@ PyObject* pysqlite_connection_commit(pysqlite_Connection* self, PyObject* args) return NULL; } - if (self->inTransaction) { + if (!sqlite3_get_autocommit(self->db)) { Py_BEGIN_ALLOW_THREADS rc = sqlite3_prepare(self->db, "COMMIT", -1, &statement, &tail); @@ -429,9 +426,7 @@ PyObject* pysqlite_connection_commit(pysqlite_Connection* self, PyObject* args) } rc = pysqlite_step(statement, self); - if (rc == SQLITE_DONE) { - self->inTransaction = 0; - } else { + if (rc != SQLITE_DONE) { _pysqlite_seterror(self->db, statement); } @@ -463,7 +458,7 @@ PyObject* pysqlite_connection_rollback(pysqlite_Connection* self, PyObject* args return NULL; } - if (self->inTransaction) { + if (!sqlite3_get_autocommit(self->db)) { pysqlite_do_all_statements(self, ACTION_RESET, 1); Py_BEGIN_ALLOW_THREADS @@ -475,9 +470,7 @@ PyObject* pysqlite_connection_rollback(pysqlite_Connection* self, PyObject* args } rc = pysqlite_step(statement, self); - if (rc == SQLITE_DONE) { - self->inTransaction = 0; - } else { + if (rc != SQLITE_DONE) { _pysqlite_seterror(self->db, statement); } @@ -1158,6 +1151,17 @@ static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self } } +static PyObject* pysqlite_connection_get_in_transaction(pysqlite_Connection* self, void* unused) +{ + if (!pysqlite_check_connection(self)) { + return NULL; + } + if (!sqlite3_get_autocommit(self->db)) { + Py_RETURN_TRUE; + } + Py_RETURN_FALSE; +} + static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level) { if (isolation_level == Py_None) { @@ -1168,7 +1172,6 @@ static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, Py Py_DECREF(res); self->begin_statement = NULL; - self->inTransaction = 0; } else { const char * const *candidate; PyObject *uppercase_level; @@ -1606,6 +1609,7 @@ PyDoc_STR("SQLite database connection object."); static PyGetSetDef connection_getset[] = { {"isolation_level", (getter)pysqlite_connection_get_isolation_level, (setter)pysqlite_connection_set_isolation_level}, {"total_changes", (getter)pysqlite_connection_get_total_changes, (setter)0}, + {"in_transaction", (getter)pysqlite_connection_get_in_transaction, (setter)0}, {NULL} }; @@ -1667,7 +1671,6 @@ static struct PyMemberDef connection_members[] = {"NotSupportedError", T_OBJECT, offsetof(pysqlite_Connection, NotSupportedError), READONLY}, {"row_factory", T_OBJECT, offsetof(pysqlite_Connection, row_factory)}, {"text_factory", T_OBJECT, offsetof(pysqlite_Connection, text_factory)}, - {"in_transaction", T_BOOL, offsetof(pysqlite_Connection, inTransaction), READONLY}, {NULL} }; -- cgit v1.2.1 From 57c0f2e61c8a5893c37e576a3a03ba5e57c1132c Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sun, 20 Nov 2016 09:13:07 +0200 Subject: Replaced outdated macros _PyUnicode_AsString and _PyUnicode_AsStringAndSize with PyUnicode_AsUTF8 and PyUnicode_AsUTF8AndSize. --- Modules/_sqlite/connection.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'Modules/_sqlite/connection.c') diff --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c index f37de42975..1c6aa54c22 100644 --- a/Modules/_sqlite/connection.c +++ b/Modules/_sqlite/connection.c @@ -505,7 +505,7 @@ _pysqlite_set_result(sqlite3_context* context, PyObject* py_val) } else if (PyFloat_Check(py_val)) { sqlite3_result_double(context, PyFloat_AsDouble(py_val)); } else if (PyUnicode_Check(py_val)) { - const char *str = _PyUnicode_AsString(py_val); + const char *str = PyUnicode_AsUTF8(py_val); if (str == NULL) return -1; sqlite3_result_text(context, str, -1, SQLITE_TRANSIENT); @@ -1527,7 +1527,7 @@ pysqlite_connection_create_collation(pysqlite_Connection* self, PyObject* args) } } - uppercase_name_str = _PyUnicode_AsString(uppercase_name); + uppercase_name_str = PyUnicode_AsUTF8(uppercase_name); if (!uppercase_name_str) goto finally; -- cgit v1.2.1