summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorGerhard Häring <gh@ghaering.de>2006-06-13 22:24:47 (GMT)
committerGerhard Häring <gh@ghaering.de>2006-06-13 22:24:47 (GMT)
commit1541ef08afda1da09ec99cfb5305fdc0af04ef40 (patch)
tree5ece2f7b0508920b06f4f6c001066418c483cb87 /Modules
parentea3912b0da71e16b8a37e04fcf3969dc85c27fa1 (diff)
downloadcpython-1541ef08afda1da09ec99cfb5305fdc0af04ef40.zip
cpython-1541ef08afda1da09ec99cfb5305fdc0af04ef40.tar.gz
cpython-1541ef08afda1da09ec99cfb5305fdc0af04ef40.tar.bz2
Merged changes from external pysqlite 2.3.0 release. Documentation updates will
follow in a few hours at the latest. Then we should be ready for beta1.
Diffstat (limited to 'Modules')
-rw-r--r--Modules/_sqlite/connection.c119
-rw-r--r--Modules/_sqlite/cursor.c51
-rw-r--r--Modules/_sqlite/module.c150
-rw-r--r--Modules/_sqlite/module.h4
4 files changed, 266 insertions, 58 deletions
diff --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c
index 64e43eb..bf74710 100644
--- a/Modules/_sqlite/connection.c
+++ b/Modules/_sqlite/connection.c
@@ -405,8 +405,6 @@ void _set_result(sqlite3_context* context, PyObject* py_val)
PyObject* stringval;
if ((!py_val) || PyErr_Occurred()) {
- /* Errors in callbacks are ignored, and we return NULL */
- PyErr_Clear();
sqlite3_result_null(context);
} else if (py_val == Py_None) {
sqlite3_result_null(context);
@@ -519,8 +517,17 @@ void _func_callback(sqlite3_context* context, int argc, sqlite3_value** argv)
Py_DECREF(args);
}
- _set_result(context, py_retval);
- Py_XDECREF(py_retval);
+ if (py_retval) {
+ _set_result(context, py_retval);
+ Py_DECREF(py_retval);
+ } else {
+ if (_enable_callback_tracebacks) {
+ PyErr_Print();
+ } else {
+ PyErr_Clear();
+ }
+ sqlite3_result_error(context, "user-defined function raised exception", -1);
+ }
PyGILState_Release(threadstate);
}
@@ -545,8 +552,13 @@ static void _step_callback(sqlite3_context *context, int argc, sqlite3_value** p
*aggregate_instance = PyObject_CallFunction(aggregate_class, "");
if (PyErr_Occurred()) {
- PyErr_Clear();
*aggregate_instance = 0;
+ if (_enable_callback_tracebacks) {
+ PyErr_Print();
+ } else {
+ PyErr_Clear();
+ }
+ sqlite3_result_error(context, "user-defined aggregate's '__init__' method raised error", -1);
goto error;
}
}
@@ -565,7 +577,12 @@ static void _step_callback(sqlite3_context *context, int argc, sqlite3_value** p
Py_DECREF(args);
if (!function_result) {
- PyErr_Clear();
+ if (_enable_callback_tracebacks) {
+ PyErr_Print();
+ } else {
+ PyErr_Clear();
+ }
+ sqlite3_result_error(context, "user-defined aggregate's 'step' method raised error", -1);
}
error:
@@ -597,13 +614,16 @@ void _final_callback(sqlite3_context* context)
function_result = PyObject_CallMethod(*aggregate_instance, "finalize", "");
if (!function_result) {
- PyErr_Clear();
- Py_INCREF(Py_None);
- function_result = Py_None;
+ if (_enable_callback_tracebacks) {
+ PyErr_Print();
+ } else {
+ PyErr_Clear();
+ }
+ sqlite3_result_error(context, "user-defined aggregate's 'finalize' method raised error", -1);
+ } else {
+ _set_result(context, function_result);
}
- _set_result(context, function_result);
-
error:
Py_XDECREF(*aggregate_instance);
Py_XDECREF(function_result);
@@ -699,6 +719,61 @@ PyObject* connection_create_aggregate(Connection* self, PyObject* args, PyObject
}
}
+int _authorizer_callback(void* user_arg, int action, const char* arg1, const char* arg2 , const char* dbname, const char* access_attempt_source)
+{
+ PyObject *ret;
+ int rc;
+ PyGILState_STATE gilstate;
+
+ gilstate = PyGILState_Ensure();
+ ret = PyObject_CallFunction((PyObject*)user_arg, "issss", action, arg1, arg2, dbname, access_attempt_source);
+
+ if (!ret) {
+ if (_enable_callback_tracebacks) {
+ PyErr_Print();
+ } else {
+ PyErr_Clear();
+ }
+
+ rc = SQLITE_DENY;
+ } else {
+ if (PyInt_Check(ret)) {
+ rc = (int)PyInt_AsLong(ret);
+ } else {
+ rc = SQLITE_DENY;
+ }
+ Py_DECREF(ret);
+ }
+
+ PyGILState_Release(gilstate);
+ return rc;
+}
+
+PyObject* connection_set_authorizer(Connection* self, PyObject* args, PyObject* kwargs)
+{
+ PyObject* authorizer_cb;
+
+ static char *kwlist[] = { "authorizer_callback", NULL };
+ int rc;
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_authorizer",
+ kwlist, &authorizer_cb)) {
+ return NULL;
+ }
+
+ rc = sqlite3_set_authorizer(self->db, _authorizer_callback, (void*)authorizer_cb);
+
+ if (rc != SQLITE_OK) {
+ PyErr_SetString(OperationalError, "Error setting authorizer callback");
+ return NULL;
+ } else {
+ PyDict_SetItem(self->function_pinboard, authorizer_cb, Py_None);
+
+ Py_INCREF(Py_None);
+ return Py_None;
+ }
+}
+
int check_thread(Connection* self)
{
if (self->check_same_thread) {
@@ -975,6 +1050,24 @@ finally:
}
static PyObject *
+connection_interrupt(Connection* self, PyObject* args)
+{
+ PyObject* retval = NULL;
+
+ if (!check_connection(self)) {
+ goto finally;
+ }
+
+ sqlite3_interrupt(self->db);
+
+ Py_INCREF(Py_None);
+ retval = Py_None;
+
+finally:
+ return retval;
+}
+
+static PyObject *
connection_create_collation(Connection* self, PyObject* args)
{
PyObject* callable;
@@ -1067,6 +1160,8 @@ static PyMethodDef connection_methods[] = {
PyDoc_STR("Creates a new function. Non-standard.")},
{"create_aggregate", (PyCFunction)connection_create_aggregate, METH_VARARGS|METH_KEYWORDS,
PyDoc_STR("Creates a new aggregate. Non-standard.")},
+ {"set_authorizer", (PyCFunction)connection_set_authorizer, METH_VARARGS|METH_KEYWORDS,
+ PyDoc_STR("Sets authorizer callback. Non-standard.")},
{"execute", (PyCFunction)connection_execute, METH_VARARGS,
PyDoc_STR("Executes a SQL statement. Non-standard.")},
{"executemany", (PyCFunction)connection_executemany, METH_VARARGS,
@@ -1075,6 +1170,8 @@ static PyMethodDef connection_methods[] = {
PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")},
{"create_collation", (PyCFunction)connection_create_collation, METH_VARARGS,
PyDoc_STR("Creates a collation function. Non-standard.")},
+ {"interrupt", (PyCFunction)connection_interrupt, METH_NOARGS,
+ PyDoc_STR("Abort any pending database operation. Non-standard.")},
{NULL, NULL}
};
diff --git a/Modules/_sqlite/cursor.c b/Modules/_sqlite/cursor.c
index 6ee8bea..98f5e0d 100644
--- a/Modules/_sqlite/cursor.c
+++ b/Modules/_sqlite/cursor.c
@@ -137,6 +137,22 @@ void cursor_dealloc(Cursor* self)
self->ob_type->tp_free((PyObject*)self);
}
+PyObject* _get_converter(PyObject* key)
+{
+ PyObject* upcase_key;
+ PyObject* retval;
+
+ upcase_key = PyObject_CallMethod(key, "upper", "");
+ if (!upcase_key) {
+ return NULL;
+ }
+
+ retval = PyDict_GetItem(converters, upcase_key);
+ Py_DECREF(upcase_key);
+
+ return retval;
+}
+
int build_row_cast_map(Cursor* self)
{
int i;
@@ -174,7 +190,7 @@ int build_row_cast_map(Cursor* self)
break;
}
- converter = PyDict_GetItem(converters, key);
+ converter = _get_converter(key);
Py_DECREF(key);
break;
}
@@ -195,7 +211,7 @@ int build_row_cast_map(Cursor* self)
}
}
- converter = PyDict_GetItem(converters, py_decltype);
+ converter = _get_converter(py_decltype);
Py_DECREF(py_decltype);
}
}
@@ -228,7 +244,10 @@ PyObject* _build_column_name(const char* colname)
}
for (pos = colname;; pos++) {
- if (*pos == 0 || *pos == ' ') {
+ if (*pos == 0 || *pos == '[') {
+ if ((*pos == '[') && (pos > colname) && (*(pos-1) == ' ')) {
+ pos--;
+ }
return PyString_FromStringAndSize(colname, pos - colname);
}
}
@@ -312,13 +331,10 @@ PyObject* _fetch_one_row(Cursor* self)
return NULL;
}
converted = PyObject_CallFunction(converter, "O", item);
+ Py_DECREF(item);
if (!converted) {
- /* TODO: have a way to log these errors */
- Py_INCREF(Py_None);
- converted = Py_None;
- PyErr_Clear();
+ break;
}
- Py_DECREF(item);
}
} else {
Py_BEGIN_ALLOW_THREADS
@@ -346,10 +362,10 @@ PyObject* _fetch_one_row(Cursor* self)
if (!converted) {
colname = sqlite3_column_name(self->statement->st, i);
- if (colname) {
+ if (!colname) {
colname = "<unknown column name>";
}
- PyOS_snprintf(buf, sizeof(buf) - 1, "Could not decode to UTF-8 column %s with text %s",
+ PyOS_snprintf(buf, sizeof(buf) - 1, "Could not decode to UTF-8 column '%s' with text '%s'",
colname , val_str);
PyErr_SetString(OperationalError, buf);
}
@@ -373,7 +389,12 @@ PyObject* _fetch_one_row(Cursor* self)
}
}
- PyTuple_SetItem(row, i, converted);
+ if (converted) {
+ PyTuple_SetItem(row, i, converted);
+ } else {
+ Py_INCREF(Py_None);
+ PyTuple_SetItem(row, i, Py_None);
+ }
}
if (PyErr_Occurred()) {
@@ -598,6 +619,14 @@ PyObject* _query_execute(Cursor* self, int multiple, PyObject* args)
goto error;
}
} else {
+ if (PyErr_Occurred()) {
+ /* there was an error that occured in a user-defined callback */
+ if (_enable_callback_tracebacks) {
+ PyErr_Print();
+ } else {
+ PyErr_Clear();
+ }
+ }
_seterror(self->connection->db);
goto error;
}
diff --git a/Modules/_sqlite/module.c b/Modules/_sqlite/module.c
index 71d0aaa..606454c 100644
--- a/Modules/_sqlite/module.c
+++ b/Modules/_sqlite/module.c
@@ -1,25 +1,25 @@
-/* module.c - the module itself
- *
- * Copyright (C) 2004-2006 Gerhard Häring <gh@ghaering.de>
- *
- * This file is part of pysqlite.
- *
- * This software is provided 'as-is', without any express or implied
- * warranty. In no event will the authors be held liable for any damages
- * arising from the use of this software.
- *
- * Permission is granted to anyone to use this software for any purpose,
- * including commercial applications, and to alter it and redistribute it
- * freely, subject to the following restrictions:
- *
- * 1. The origin of this software must not be misrepresented; you must not
- * claim that you wrote the original software. If you use this software
- * in a product, an acknowledgment in the product documentation would be
- * appreciated but is not required.
- * 2. Altered source versions must be plainly marked as such, and must not be
- * misrepresented as being the original software.
- * 3. This notice may not be removed or altered from any source distribution.
- */
+ /* module.c - the module itself
+ *
+ * Copyright (C) 2004-2006 Gerhard Häring <gh@ghaering.de>
+ *
+ * This file is part of pysqlite.
+ *
+ * This software is provided 'as-is', without any express or implied
+ * warranty. In no event will the authors be held liable for any damages
+ * arising from the use of this software.
+ *
+ * Permission is granted to anyone to use this software for any purpose,
+ * including commercial applications, and to alter it and redistribute it
+ * freely, subject to the following restrictions:
+ *
+ * 1. The origin of this software must not be misrepresented; you must not
+ * claim that you wrote the original software. If you use this software
+ * in a product, an acknowledgment in the product documentation would be
+ * appreciated but is not required.
+ * 2. Altered source versions must be plainly marked as such, and must not be
+ * misrepresented as being the original software.
+ * 3. This notice may not be removed or altered from any source distribution.
+ */
#include "connection.h"
#include "statement.h"
@@ -40,6 +40,7 @@ PyObject* Error, *Warning, *InterfaceError, *DatabaseError, *InternalError,
*NotSupportedError, *OptimizedUnicode;
PyObject* converters;
+int _enable_callback_tracebacks;
static PyObject* module_connect(PyObject* self, PyObject* args, PyObject*
kwargs)
@@ -140,14 +141,42 @@ static PyObject* module_register_adapter(PyObject* self, PyObject* args, PyObjec
static PyObject* module_register_converter(PyObject* self, PyObject* args, PyObject* kwargs)
{
- PyObject* name;
+ char* orig_name;
+ char* name = NULL;
+ char* c;
PyObject* callable;
+ PyObject* retval = NULL;
- if (!PyArg_ParseTuple(args, "OO", &name, &callable)) {
+ if (!PyArg_ParseTuple(args, "sO", &orig_name, &callable)) {
return NULL;
}
- if (PyDict_SetItem(converters, name, callable) != 0) {
+ /* convert the name to lowercase */
+ name = PyMem_Malloc(strlen(orig_name) + 2);
+ if (!name) {
+ goto error;
+ }
+ strcpy(name, orig_name);
+ for (c = name; *c != (char)0; c++) {
+ *c = (*c) & 0xDF;
+ }
+
+ if (PyDict_SetItemString(converters, name, callable) != 0) {
+ goto error;
+ }
+
+ Py_INCREF(Py_None);
+ retval = Py_None;
+error:
+ if (name) {
+ PyMem_Free(name);
+ }
+ return retval;
+}
+
+static PyObject* enable_callback_tracebacks(PyObject* self, PyObject* args, PyObject* kwargs)
+{
+ if (!PyArg_ParseTuple(args, "i", &_enable_callback_tracebacks)) {
return NULL;
}
@@ -174,13 +203,64 @@ static PyMethodDef module_methods[] = {
{"register_adapter", (PyCFunction)module_register_adapter, METH_VARARGS, PyDoc_STR("Registers an adapter with pysqlite's adapter registry. Non-standard.")},
{"register_converter", (PyCFunction)module_register_converter, METH_VARARGS, PyDoc_STR("Registers a converter with pysqlite. Non-standard.")},
{"adapt", (PyCFunction)psyco_microprotocols_adapt, METH_VARARGS, psyco_microprotocols_adapt_doc},
+ {"enable_callback_tracebacks", (PyCFunction)enable_callback_tracebacks, METH_VARARGS, PyDoc_STR("Enable or disable callback functions throwing errors to stderr.")},
{NULL, NULL}
};
+struct _IntConstantPair {
+ char* constant_name;
+ int constant_value;
+};
+
+typedef struct _IntConstantPair IntConstantPair;
+
+static IntConstantPair _int_constants[] = {
+ {"PARSE_DECLTYPES", PARSE_DECLTYPES},
+ {"PARSE_COLNAMES", PARSE_COLNAMES},
+
+ {"SQLITE_OK", SQLITE_OK},
+ {"SQLITE_DENY", SQLITE_DENY},
+ {"SQLITE_IGNORE", SQLITE_IGNORE},
+ {"SQLITE_CREATE_INDEX", SQLITE_CREATE_INDEX},
+ {"SQLITE_CREATE_TABLE", SQLITE_CREATE_TABLE},
+ {"SQLITE_CREATE_TEMP_INDEX", SQLITE_CREATE_TEMP_INDEX},
+ {"SQLITE_CREATE_TEMP_TABLE", SQLITE_CREATE_TEMP_TABLE},
+ {"SQLITE_CREATE_TEMP_TRIGGER", SQLITE_CREATE_TEMP_TRIGGER},
+ {"SQLITE_CREATE_TEMP_VIEW", SQLITE_CREATE_TEMP_VIEW},
+ {"SQLITE_CREATE_TRIGGER", SQLITE_CREATE_TRIGGER},
+ {"SQLITE_CREATE_VIEW", SQLITE_CREATE_VIEW},
+ {"SQLITE_DELETE", SQLITE_DELETE},
+ {"SQLITE_DROP_INDEX", SQLITE_DROP_INDEX},
+ {"SQLITE_DROP_TABLE", SQLITE_DROP_TABLE},
+ {"SQLITE_DROP_TEMP_INDEX", SQLITE_DROP_TEMP_INDEX},
+ {"SQLITE_DROP_TEMP_TABLE", SQLITE_DROP_TEMP_TABLE},
+ {"SQLITE_DROP_TEMP_TRIGGER", SQLITE_DROP_TEMP_TRIGGER},
+ {"SQLITE_DROP_TEMP_VIEW", SQLITE_DROP_TEMP_VIEW},
+ {"SQLITE_DROP_TRIGGER", SQLITE_DROP_TRIGGER},
+ {"SQLITE_DROP_VIEW", SQLITE_DROP_VIEW},
+ {"SQLITE_INSERT", SQLITE_INSERT},
+ {"SQLITE_PRAGMA", SQLITE_PRAGMA},
+ {"SQLITE_READ", SQLITE_READ},
+ {"SQLITE_SELECT", SQLITE_SELECT},
+ {"SQLITE_TRANSACTION", SQLITE_TRANSACTION},
+ {"SQLITE_UPDATE", SQLITE_UPDATE},
+ {"SQLITE_ATTACH", SQLITE_ATTACH},
+ {"SQLITE_DETACH", SQLITE_DETACH},
+#if SQLITE_VERSION_NUMBER >= 3002001
+ {"SQLITE_ALTER_TABLE", SQLITE_ALTER_TABLE},
+ {"SQLITE_REINDEX", SQLITE_REINDEX},
+#endif
+#if SQLITE_VERSION_NUMBER >= 3003000
+ {"SQLITE_ANALYZE", SQLITE_ANALYZE},
+#endif
+ {(char*)NULL, 0}
+};
+
PyMODINIT_FUNC init_sqlite3(void)
{
PyObject *module, *dict;
PyObject *tmp_obj;
+ int i;
module = Py_InitModule("_sqlite3", module_methods);
@@ -276,17 +356,15 @@ PyMODINIT_FUNC init_sqlite3(void)
OptimizedUnicode = (PyObject*)&PyCell_Type;
PyDict_SetItemString(dict, "OptimizedUnicode", OptimizedUnicode);
- if (!(tmp_obj = PyInt_FromLong(PARSE_DECLTYPES))) {
- goto error;
+ /* Set integer constants */
+ for (i = 0; _int_constants[i].constant_name != 0; i++) {
+ tmp_obj = PyInt_FromLong(_int_constants[i].constant_value);
+ if (!tmp_obj) {
+ goto error;
+ }
+ PyDict_SetItemString(dict, _int_constants[i].constant_name, tmp_obj);
+ Py_DECREF(tmp_obj);
}
- PyDict_SetItemString(dict, "PARSE_DECLTYPES", tmp_obj);
- Py_DECREF(tmp_obj);
-
- if (!(tmp_obj = PyInt_FromLong(PARSE_COLNAMES))) {
- goto error;
- }
- PyDict_SetItemString(dict, "PARSE_COLNAMES", tmp_obj);
- Py_DECREF(tmp_obj);
if (!(tmp_obj = PyString_FromString(PYSQLITE_VERSION))) {
goto error;
@@ -306,6 +384,8 @@ PyMODINIT_FUNC init_sqlite3(void)
/* initialize the default converters */
converters_init(dict);
+ _enable_callback_tracebacks = 0;
+
/* Original comment form _bsddb.c in the Python core. This is also still
* needed nowadays for Python 2.3/2.4.
*
diff --git a/Modules/_sqlite/module.h b/Modules/_sqlite/module.h
index f3e2aa1..00eff1f 100644
--- a/Modules/_sqlite/module.h
+++ b/Modules/_sqlite/module.h
@@ -25,7 +25,7 @@
#define PYSQLITE_MODULE_H
#include "Python.h"
-#define PYSQLITE_VERSION "2.2.2"
+#define PYSQLITE_VERSION "2.3.0"
extern PyObject* Error;
extern PyObject* Warning;
@@ -50,6 +50,8 @@ extern PyObject* time_sleep;
*/
extern PyObject* converters;
+extern int _enable_callback_tracebacks;
+
#define PARSE_DECLTYPES 1
#define PARSE_COLNAMES 2
#endif