aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Modules/_sqlite
diff options
context:
space:
mode:
Diffstat (limited to 'Modules/_sqlite')
-rw-r--r--Modules/_sqlite/cache.c21
-rw-r--r--Modules/_sqlite/connection.c99
-rw-r--r--Modules/_sqlite/connection.h3
-rw-r--r--Modules/_sqlite/cursor.c31
-rw-r--r--Modules/_sqlite/microprotocols.c8
-rw-r--r--Modules/_sqlite/module.c19
-rw-r--r--Modules/_sqlite/module.h2
-rw-r--r--Modules/_sqlite/row.c10
-rw-r--r--Modules/_sqlite/statement.c12
-rw-r--r--Modules/_sqlite/statement.h4
10 files changed, 122 insertions, 87 deletions
diff --git a/Modules/_sqlite/cache.c b/Modules/_sqlite/cache.c
index 735a2426ccf..3693363bb2a 100644
--- a/Modules/_sqlite/cache.c
+++ b/Modules/_sqlite/cache.c
@@ -217,8 +217,6 @@ PyObject* pysqlite_cache_display(pysqlite_Cache* self, PyObject* args)
pysqlite_Node* ptr;
PyObject* prevkey;
PyObject* nextkey;
- PyObject* fmt_args;
- PyObject* template;
PyObject* display_str;
ptr = self->first;
@@ -229,36 +227,21 @@ PyObject* pysqlite_cache_display(pysqlite_Cache* self, PyObject* args)
} else {
prevkey = Py_None;
}
- Py_INCREF(prevkey);
if (ptr->next) {
nextkey = ptr->next->key;
} else {
nextkey = Py_None;
}
- Py_INCREF(nextkey);
- fmt_args = Py_BuildValue("OOO", prevkey, ptr->key, nextkey);
- if (!fmt_args) {
- return NULL;
- }
- template = PyUnicode_FromString("%s <- %s ->%s\n");
- if (!template) {
- Py_DECREF(fmt_args);
- return NULL;
- }
- display_str = PyUnicode_Format(template, fmt_args);
- Py_DECREF(template);
- Py_DECREF(fmt_args);
+ display_str = PyUnicode_FromFormat("%S <- %S -> %S\n",
+ prevkey, ptr->key, nextkey);
if (!display_str) {
return NULL;
}
PyObject_Print(display_str, stdout, Py_PRINT_RAW);
Py_DECREF(display_str);
- Py_DECREF(prevkey);
- Py_DECREF(nextkey);
-
ptr = ptr->next;
}
diff --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c
index d52bea49c35..28bd647b3f5 100644
--- a/Modules/_sqlite/connection.c
+++ b/Modules/_sqlite/connection.c
@@ -677,7 +677,7 @@ void _pysqlite_final_callback(sqlite3_context* context)
{
PyObject* function_result;
PyObject** aggregate_instance;
- PyObject* aggregate_class;
+ _Py_IDENTIFIER(finalize);
int ok;
#ifdef WITH_THREAD
@@ -686,8 +686,6 @@ void _pysqlite_final_callback(sqlite3_context* context)
threadstate = PyGILState_Ensure();
#endif
- aggregate_class = (PyObject*)sqlite3_user_data(context);
-
aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
if (!*aggregate_instance) {
/* this branch is executed if there was an exception in the aggregate's
@@ -696,7 +694,7 @@ void _pysqlite_final_callback(sqlite3_context* context)
goto error;
}
- function_result = PyObject_CallMethod(*aggregate_instance, "finalize", "");
+ function_result = _PyObject_CallMethodId(*aggregate_instance, &PyId_finalize, "");
Py_DECREF(*aggregate_instance);
ok = 0;
@@ -916,6 +914,38 @@ static int _progress_handler(void* user_arg)
return rc;
}
+static void _trace_callback(void* user_arg, const char* statement_string)
+{
+ PyObject *py_statement = NULL;
+ PyObject *ret = NULL;
+
+#ifdef WITH_THREAD
+ PyGILState_STATE gilstate;
+
+ gilstate = PyGILState_Ensure();
+#endif
+ py_statement = PyUnicode_DecodeUTF8(statement_string,
+ strlen(statement_string), "replace");
+ if (py_statement) {
+ ret = PyObject_CallFunctionObjArgs((PyObject*)user_arg, py_statement, NULL);
+ Py_DECREF(py_statement);
+ }
+
+ if (ret) {
+ Py_DECREF(ret);
+ } else {
+ if (_enable_callback_tracebacks) {
+ PyErr_Print();
+ } else {
+ PyErr_Clear();
+ }
+ }
+
+#ifdef WITH_THREAD
+ PyGILState_Release(gilstate);
+#endif
+}
+
static PyObject* pysqlite_connection_set_authorizer(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
{
PyObject* authorizer_cb;
@@ -975,6 +1005,34 @@ static PyObject* pysqlite_connection_set_progress_handler(pysqlite_Connection* s
return Py_None;
}
+static PyObject* pysqlite_connection_set_trace_callback(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
+{
+ PyObject* trace_callback;
+
+ static char *kwlist[] = { "trace_callback", NULL };
+
+ if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
+ return NULL;
+ }
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_trace_callback",
+ kwlist, &trace_callback)) {
+ return NULL;
+ }
+
+ if (trace_callback == Py_None) {
+ /* None clears the trace callback previously set */
+ sqlite3_trace(self->db, 0, (void*)0);
+ } else {
+ if (PyDict_SetItem(self->function_pinboard, trace_callback, Py_None) == -1)
+ return NULL;
+ sqlite3_trace(self->db, _trace_callback, trace_callback);
+ }
+
+ Py_INCREF(Py_None);
+ return Py_None;
+}
+
#ifdef HAVE_LOAD_EXTENSION
static PyObject* pysqlite_enable_load_extension(pysqlite_Connection* self, PyObject* args)
{
@@ -1180,8 +1238,9 @@ PyObject* pysqlite_connection_execute(pysqlite_Connection* self, PyObject* args,
PyObject* cursor = 0;
PyObject* result = 0;
PyObject* method = 0;
+ _Py_IDENTIFIER(cursor);
- cursor = PyObject_CallMethod((PyObject*)self, "cursor", "");
+ cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, "");
if (!cursor) {
goto error;
}
@@ -1209,8 +1268,9 @@ PyObject* pysqlite_connection_executemany(pysqlite_Connection* self, PyObject* a
PyObject* cursor = 0;
PyObject* result = 0;
PyObject* method = 0;
+ _Py_IDENTIFIER(cursor);
- cursor = PyObject_CallMethod((PyObject*)self, "cursor", "");
+ cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, "");
if (!cursor) {
goto error;
}
@@ -1238,8 +1298,9 @@ PyObject* pysqlite_connection_executescript(pysqlite_Connection* self, PyObject*
PyObject* cursor = 0;
PyObject* result = 0;
PyObject* method = 0;
+ _Py_IDENTIFIER(cursor);
- cursor = PyObject_CallMethod((PyObject*)self, "cursor", "");
+ cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, "");
if (!cursor) {
goto error;
}
@@ -1394,10 +1455,12 @@ pysqlite_connection_create_collation(pysqlite_Connection* self, PyObject* args)
PyObject* uppercase_name = 0;
PyObject* name;
PyObject* retval;
- Py_UNICODE* chk;
Py_ssize_t i, len;
+ _Py_IDENTIFIER(upper);
char *uppercase_name_str;
int rc;
+ unsigned int kind;
+ void *data;
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
goto finally;
@@ -1407,17 +1470,21 @@ pysqlite_connection_create_collation(pysqlite_Connection* self, PyObject* args)
goto finally;
}
- uppercase_name = PyObject_CallMethod(name, "upper", "");
+ uppercase_name = _PyObject_CallMethodId(name, &PyId_upper, "");
if (!uppercase_name) {
goto finally;
}
- len = PyUnicode_GET_SIZE(uppercase_name);
- chk = PyUnicode_AS_UNICODE(uppercase_name);
- for (i=0; i<len; i++, chk++) {
- if ((*chk >= '0' && *chk <= '9')
- || (*chk >= 'A' && *chk <= 'Z')
- || (*chk == '_'))
+ if (PyUnicode_READY(uppercase_name))
+ goto finally;
+ len = PyUnicode_GET_LENGTH(uppercase_name);
+ kind = PyUnicode_KIND(uppercase_name);
+ data = PyUnicode_DATA(uppercase_name);
+ for (i=0; i<len; i++) {
+ Py_UCS4 ch = PyUnicode_READ(kind, data, i);
+ if ((ch >= '0' && ch <= '9')
+ || (ch >= 'A' && ch <= 'Z')
+ || (ch == '_'))
{
continue;
} else {
@@ -1536,6 +1603,8 @@ static PyMethodDef connection_methods[] = {
#endif
{"set_progress_handler", (PyCFunction)pysqlite_connection_set_progress_handler, METH_VARARGS|METH_KEYWORDS,
PyDoc_STR("Sets progress handler callback. Non-standard.")},
+ {"set_trace_callback", (PyCFunction)pysqlite_connection_set_trace_callback, METH_VARARGS|METH_KEYWORDS,
+ PyDoc_STR("Sets a trace callback called for each SQL statement (passed as unicode). Non-standard.")},
{"execute", (PyCFunction)pysqlite_connection_execute, METH_VARARGS,
PyDoc_STR("Executes a SQL statement. Non-standard.")},
{"executemany", (PyCFunction)pysqlite_connection_executemany, METH_VARARGS,
diff --git a/Modules/_sqlite/connection.h b/Modules/_sqlite/connection.h
index c8e2f7cbd2b..0c9734caf71 100644
--- a/Modules/_sqlite/connection.h
+++ b/Modules/_sqlite/connection.h
@@ -83,8 +83,7 @@ typedef struct
/* Determines how bytestrings from SQLite are converted to Python objects:
* - PyUnicode_Type: Python Unicode objects are constructed from UTF-8 bytestrings
- * - OptimizedUnicode: Like before, but for ASCII data, only PyStrings are created.
- * - PyBytes_Type: PyStrings are created as-is.
+ * - PyBytes_Type: The bytestrings are returned as-is.
* - Any custom callable: Any object returned from the callable called with the bytestring
* as single parameter.
*/
diff --git a/Modules/_sqlite/cursor.c b/Modules/_sqlite/cursor.c
index fc4b608abfa..1005d88c8e0 100644
--- a/Modules/_sqlite/cursor.c
+++ b/Modules/_sqlite/cursor.c
@@ -118,11 +118,9 @@ static int pysqlite_cursor_init(pysqlite_Cursor* self, PyObject* args, PyObject*
static void pysqlite_cursor_dealloc(pysqlite_Cursor* self)
{
- int rc;
-
/* Reset the statement if the user has not closed the cursor */
if (self->statement) {
- rc = pysqlite_statement_reset(self->statement);
+ pysqlite_statement_reset(self->statement);
Py_DECREF(self->statement);
}
@@ -144,8 +142,9 @@ PyObject* _pysqlite_get_converter(PyObject* key)
{
PyObject* upcase_key;
PyObject* retval;
+ _Py_IDENTIFIER(upper);
- upcase_key = PyObject_CallMethod(key, "upper", "");
+ upcase_key = _PyObject_CallMethodId(key, &PyId_upper, "");
if (!upcase_key) {
return NULL;
}
@@ -260,11 +259,6 @@ PyObject* _pysqlite_build_column_name(const char* colname)
}
}
-PyObject* pysqlite_unicode_from_string(const char* val_str, Py_ssize_t size, int optimize)
-{
- return PyUnicode_FromStringAndSize(val_str, size);
-}
-
/*
* Returns a row from the currently active SQLite statement
*
@@ -342,12 +336,8 @@ PyObject* _pysqlite_fetch_one_row(pysqlite_Cursor* self)
} else if (coltype == SQLITE_TEXT) {
val_str = (const char*)sqlite3_column_text(self->statement->st, i);
nbytes = sqlite3_column_bytes(self->statement->st, i);
- if ((self->connection->text_factory == (PyObject*)&PyUnicode_Type)
- || (self->connection->text_factory == pysqlite_OptimizedUnicode)) {
-
- converted = pysqlite_unicode_from_string(val_str, nbytes,
- self->connection->text_factory == pysqlite_OptimizedUnicode ? 1 : 0);
-
+ if (self->connection->text_factory == (PyObject*)&PyUnicode_Type) {
+ converted = PyUnicode_FromStringAndSize(val_str, nbytes);
if (!converted) {
colname = sqlite3_column_name(self->statement->st, i);
if (!colname) {
@@ -445,7 +435,6 @@ PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject*
int statement_type;
PyObject* descriptor;
PyObject* second_argument = NULL;
- int allow_8bit_chars;
if (!check_cursor(self)) {
goto error;
@@ -454,10 +443,6 @@ PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject*
self->locked = 1;
self->reset = 0;
- /* Make shooting yourself in the foot with not utf-8 decodable 8-bit-strings harder */
- allow_8bit_chars = ((self->connection->text_factory != (PyObject*)&PyUnicode_Type) &&
- (self->connection->text_factory != pysqlite_OptimizedUnicode));
-
Py_XDECREF(self->next_row);
self->next_row = NULL;
@@ -521,7 +506,7 @@ PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject*
if (self->statement != NULL) {
/* There is an active statement */
- rc = pysqlite_statement_reset(self->statement);
+ pysqlite_statement_reset(self->statement);
}
operation_cstr = _PyUnicode_AsStringAndSize(operation, &operation_len);
@@ -616,7 +601,7 @@ PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject*
pysqlite_statement_mark_dirty(self->statement);
- pysqlite_statement_bind_parameters(self->statement, parameters, allow_8bit_chars);
+ pysqlite_statement_bind_parameters(self->statement, parameters);
if (PyErr_Occurred()) {
goto error;
}
@@ -727,7 +712,7 @@ PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject*
}
if (multiple) {
- rc = pysqlite_statement_reset(self->statement);
+ pysqlite_statement_reset(self->statement);
}
Py_XDECREF(parameters);
}
diff --git a/Modules/_sqlite/microprotocols.c b/Modules/_sqlite/microprotocols.c
index c730afa7bd7..2261b8013ce 100644
--- a/Modules/_sqlite/microprotocols.c
+++ b/Modules/_sqlite/microprotocols.c
@@ -95,7 +95,9 @@ pysqlite_microprotocols_adapt(PyObject *obj, PyObject *proto, PyObject *alt)
/* try to have the protocol adapt this object*/
if (PyObject_HasAttrString(proto, "__adapt__")) {
- PyObject *adapted = PyObject_CallMethod(proto, "__adapt__", "O", obj);
+ _Py_IDENTIFIER(__adapt__);
+ PyObject *adapted = _PyObject_CallMethodId(proto, &PyId___adapt__, "O", obj);
+
if (adapted) {
if (adapted != Py_None) {
return adapted;
@@ -110,7 +112,9 @@ pysqlite_microprotocols_adapt(PyObject *obj, PyObject *proto, PyObject *alt)
/* and finally try to have the object adapt itself */
if (PyObject_HasAttrString(obj, "__conform__")) {
- PyObject *adapted = PyObject_CallMethod(obj, "__conform__","O", proto);
+ _Py_IDENTIFIER(__conform__);
+ PyObject *adapted = _PyObject_CallMethodId(obj, &PyId___conform__,"O", proto);
+
if (adapted) {
if (adapted != Py_None) {
return adapted;
diff --git a/Modules/_sqlite/module.c b/Modules/_sqlite/module.c
index cbc3b8e90be..bea6d6aba1e 100644
--- a/Modules/_sqlite/module.c
+++ b/Modules/_sqlite/module.c
@@ -37,7 +37,7 @@
PyObject* pysqlite_Error, *pysqlite_Warning, *pysqlite_InterfaceError, *pysqlite_DatabaseError,
*pysqlite_InternalError, *pysqlite_OperationalError, *pysqlite_ProgrammingError,
- *pysqlite_IntegrityError, *pysqlite_DataError, *pysqlite_NotSupportedError, *pysqlite_OptimizedUnicode;
+ *pysqlite_IntegrityError, *pysqlite_DataError, *pysqlite_NotSupportedError;
PyObject* converters;
int _enable_callback_tracebacks;
@@ -179,13 +179,14 @@ static PyObject* module_register_converter(PyObject* self, PyObject* args)
PyObject* name = NULL;
PyObject* callable;
PyObject* retval = NULL;
+ _Py_IDENTIFIER(upper);
if (!PyArg_ParseTuple(args, "UO", &orig_name, &callable)) {
return NULL;
}
/* convert the name to upper case */
- name = PyObject_CallMethod(orig_name, "upper", "");
+ name = _PyObject_CallMethodId(orig_name, &PyId_upper, "");
if (!name) {
goto error;
}
@@ -406,13 +407,13 @@ PyMODINIT_FUNC PyInit__sqlite3(void)
}
PyDict_SetItemString(dict, "NotSupportedError", pysqlite_NotSupportedError);
- /* We just need "something" unique for pysqlite_OptimizedUnicode. It does not really
- * need to be a string subclass. Just anything that can act as a special
- * marker for us. So I pulled PyCell_Type out of my magic hat.
- */
- Py_INCREF((PyObject*)&PyCell_Type);
- pysqlite_OptimizedUnicode = (PyObject*)&PyCell_Type;
- PyDict_SetItemString(dict, "OptimizedUnicode", pysqlite_OptimizedUnicode);
+ /* In Python 2.x, setting Connection.text_factory to
+ OptimizedUnicode caused Unicode objects to be returned for
+ non-ASCII data and bytestrings to be returned for ASCII data.
+ Now OptimizedUnicode is an alias for str, so it has no
+ effect. */
+ Py_INCREF((PyObject*)&PyUnicode_Type);
+ PyDict_SetItemString(dict, "OptimizedUnicode", (PyObject*)&PyUnicode_Type);
/* Set integer constants */
for (i = 0; _int_constants[i].constant_name != 0; i++) {
diff --git a/Modules/_sqlite/module.h b/Modules/_sqlite/module.h
index 0eddb679483..b51724bf4ce 100644
--- a/Modules/_sqlite/module.h
+++ b/Modules/_sqlite/module.h
@@ -38,8 +38,6 @@ extern PyObject* pysqlite_IntegrityError;
extern PyObject* pysqlite_DataError;
extern PyObject* pysqlite_NotSupportedError;
-extern PyObject* pysqlite_OptimizedUnicode;
-
/* the functions time.time() and time.sleep() */
extern PyObject* time_time;
extern PyObject* time_sleep;
diff --git a/Modules/_sqlite/row.c b/Modules/_sqlite/row.c
index 3d440942f9d..b50658c220d 100644
--- a/Modules/_sqlite/row.c
+++ b/Modules/_sqlite/row.c
@@ -173,10 +173,9 @@ static Py_hash_t pysqlite_row_hash(pysqlite_Row *self)
static PyObject* pysqlite_row_richcompare(pysqlite_Row *self, PyObject *_other, int opid)
{
- if (opid != Py_EQ && opid != Py_NE) {
- Py_INCREF(Py_NotImplemented);
- return Py_NotImplemented;
- }
+ if (opid != Py_EQ && opid != Py_NE)
+ Py_RETURN_NOTIMPLEMENTED;
+
if (PyType_IsSubtype(Py_TYPE(_other), &pysqlite_RowType)) {
pysqlite_Row *other = (pysqlite_Row *)_other;
PyObject *res = PyObject_RichCompare(self->description, other->description, opid);
@@ -186,8 +185,7 @@ static PyObject* pysqlite_row_richcompare(pysqlite_Row *self, PyObject *_other,
return PyObject_RichCompare(self->data, other->data, opid);
}
}
- Py_INCREF(Py_NotImplemented);
- return Py_NotImplemented;
+ Py_RETURN_NOTIMPLEMENTED;
}
PyMappingMethods pysqlite_row_as_mapping = {
diff --git a/Modules/_sqlite/statement.c b/Modules/_sqlite/statement.c
index 09d95d9a67f..471a0676a83 100644
--- a/Modules/_sqlite/statement.c
+++ b/Modules/_sqlite/statement.c
@@ -88,7 +88,7 @@ int pysqlite_statement_create(pysqlite_Statement* self, pysqlite_Connection* con
return rc;
}
-int pysqlite_statement_bind_parameter(pysqlite_Statement* self, int pos, PyObject* parameter, int allow_8bit_chars)
+int pysqlite_statement_bind_parameter(pysqlite_Statement* self, int pos, PyObject* parameter)
{
int rc = SQLITE_OK;
const char* buffer;
@@ -169,7 +169,7 @@ static int _need_adapt(PyObject* obj)
}
}
-void pysqlite_statement_bind_parameters(pysqlite_Statement* self, PyObject* parameters, int allow_8bit_chars)
+void pysqlite_statement_bind_parameters(pysqlite_Statement* self, PyObject* parameters)
{
PyObject* current_param;
PyObject* adapted;
@@ -223,7 +223,7 @@ void pysqlite_statement_bind_parameters(pysqlite_Statement* self, PyObject* para
}
}
- rc = pysqlite_statement_bind_parameter(self, i + 1, adapted, allow_8bit_chars);
+ rc = pysqlite_statement_bind_parameter(self, i + 1, adapted);
Py_DECREF(adapted);
if (rc != SQLITE_OK) {
@@ -268,7 +268,7 @@ void pysqlite_statement_bind_parameters(pysqlite_Statement* self, PyObject* para
}
}
- rc = pysqlite_statement_bind_parameter(self, i, adapted, allow_8bit_chars);
+ rc = pysqlite_statement_bind_parameter(self, i, adapted);
Py_DECREF(adapted);
if (rc != SQLITE_OK) {
@@ -372,11 +372,9 @@ void pysqlite_statement_mark_dirty(pysqlite_Statement* self)
void pysqlite_statement_dealloc(pysqlite_Statement* self)
{
- int rc;
-
if (self->st) {
Py_BEGIN_ALLOW_THREADS
- rc = sqlite3_finalize(self->st);
+ sqlite3_finalize(self->st);
Py_END_ALLOW_THREADS
}
diff --git a/Modules/_sqlite/statement.h b/Modules/_sqlite/statement.h
index e5da42e7d83..4681443e760 100644
--- a/Modules/_sqlite/statement.h
+++ b/Modules/_sqlite/statement.h
@@ -46,8 +46,8 @@ extern PyTypeObject pysqlite_StatementType;
int pysqlite_statement_create(pysqlite_Statement* self, pysqlite_Connection* connection, PyObject* sql);
void pysqlite_statement_dealloc(pysqlite_Statement* self);
-int pysqlite_statement_bind_parameter(pysqlite_Statement* self, int pos, PyObject* parameter, int allow_8bit_chars);
-void pysqlite_statement_bind_parameters(pysqlite_Statement* self, PyObject* parameters, int allow_8bit_chars);
+int pysqlite_statement_bind_parameter(pysqlite_Statement* self, int pos, PyObject* parameter);
+void pysqlite_statement_bind_parameters(pysqlite_Statement* self, PyObject* parameters);
int pysqlite_statement_recompile(pysqlite_Statement* self, PyObject* parameters);
int pysqlite_statement_finalize(pysqlite_Statement* self);