diff --git a/src/blob.c b/src/blob.c index 3112132..2d5a649 100644 --- a/src/blob.c +++ b/src/blob.c @@ -208,12 +208,17 @@ PyObject* pysqlite_blob_write(pysqlite_Blob *self, PyObject *data) return NULL; } - if (!pysqlite_check_blob(self)) { + if (data_buffer.len > self->length - self->offset) { + PyErr_SetString(PyExc_ValueError, + "data longer than blob length"); PyBuffer_Release(&data_buffer); return NULL; } - /* TODO: throw better error on data bigger then blob. */ + if (!pysqlite_check_blob(self)) { + PyBuffer_Release(&data_buffer); + return NULL; + } rc = write_inner(self, data_buffer.buf, data_buffer.len, self->offset); @@ -257,19 +262,22 @@ PyObject* pysqlite_blob_seek(pysqlite_Blob *self, PyObject *args) offset = self->length + offset; break; default: - return PyErr_Format(PyExc_ValueError, + PyErr_SetString(PyExc_ValueError, "from_what should be 0, 1 or 2"); + return NULL; } if (offset < 0 || offset > self->length) { - return PyErr_Format(PyExc_ValueError, "offset out of blob range"); + PyErr_SetString(PyExc_ValueError, "offset out of blob range"); + return NULL; } self->offset = offset; Py_RETURN_NONE; overflow: - return PyErr_Format(PyExc_OverflowError, "seek offset result in overflow"); + PyErr_SetString(PyExc_OverflowError, "seek offset result in overflow"); + return NULL; } @@ -332,7 +340,7 @@ static int pysqlite_blob_contains(pysqlite_Blob *self, PyObject *args) { if (pysqlite_check_blob(self)) { PyErr_SetString(PyExc_SystemError, - "Blob don't support cotains operation"); + "Blob don't support contains operation"); } return -1; } @@ -407,11 +415,11 @@ static PyObject * pysqlite_blob_subscript(pysqlite_Blob *self, PyObject *item) return NULL; } - if (slicelen <= 0) + if (slicelen <= 0) { return PyBytes_FromStringAndSize("", 0); - else if (step == 1) + } else if (step == 1) { return inner_read(self, slicelen, start); - else { + } else { char *result_buf = (char *)PyMem_Malloc(slicelen); char *data_buff = NULL; Py_ssize_t cur, i; @@ -586,7 +594,7 @@ static int pysqlite_blob_ass_subscript(pysqlite_Blob *self, PyObject *item, PyOb } else { PyErr_SetString(PyExc_TypeError, - "mmap indices must be integer"); + "Blob indices must be integer"); return -1; } } diff --git a/src/cache.c b/src/cache.c index 89ee894..1a8a873 100644 --- a/src/cache.c +++ b/src/cache.c @@ -112,9 +112,8 @@ void pysqlite_cache_dealloc(pysqlite_Cache* self) Py_TYPE(self)->tp_free((PyObject*)self); } -PyObject* pysqlite_cache_get(pysqlite_Cache* self, PyObject* args) +PyObject* pysqlite_cache_get(pysqlite_Cache* self, PyObject* key) { - PyObject* key = args; pysqlite_Node* node; pysqlite_Node* ptr; PyObject* data; @@ -184,6 +183,9 @@ PyObject* pysqlite_cache_get(pysqlite_Cache* self, PyObject* args) } } + /* We cannot replace this by PyObject_CallOneArg() since + * PyObject_CallFunction() has a special case when using a + * single tuple as argument. */ data = PyObject_CallFunction(self->factory, "O", key); if (!data) { diff --git a/src/connection.c b/src/connection.c index e2a0d34..77e7482 100644 --- a/src/connection.c +++ b/src/connection.c @@ -31,8 +31,6 @@ #include "prepare_protocol.h" #include "util.h" -#include "pythread.h" - #define ACTION_FINALIZE 1 #define ACTION_RESET 2 @@ -1833,7 +1831,7 @@ pysqlite_connection_create_collation(pysqlite_Connection* self, PyObject* args) const char *uppercase_name_str; int rc; unsigned int kind; - void *data; + const void *data; if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { goto finally; diff --git a/test/dbapi.py b/test/dbapi.py index 00702d6..68fe46d 100644 --- a/test/dbapi.py +++ b/test/dbapi.py @@ -593,7 +593,7 @@ class BlobTests(unittest.TestCase): self.assertEqual(self.blob.tell(), 50) def CheckBlobWriteMoreThenBlobSize(self): - with self.assertRaises(sqlite.OperationalError): + with self.assertRaises(ValueError): self.blob.write(b"a" * 1000) def CheckBlobReadAfterRowChange(self):