Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Lib/test/test_sqlite3/test_dbapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1138,6 +1138,13 @@ def test_blob_write_error_length(self):
with self.assertRaisesRegex(ValueError, "data longer than blob"):
self.blob.write(b"a" * 1000)

self.blob.seek(0, SEEK_SET)
n = len(self.blob)
self.blob.write(b"a" * (n-1))
self.blob.write(b"a" * 1)
with self.assertRaisesRegex(ValueError, "data longer than blob"):
self.blob.write(b"a" * 1)

def test_blob_write_error_row_changed(self):
self.cx.execute("update test set b='aaaa' where rowid=1")
with self.assertRaises(sqlite.OperationalError):
Expand Down Expand Up @@ -1178,6 +1185,15 @@ def test_blob_set_item(self):
actual = self.cx.execute("select b from test").fetchone()[0]
self.assertEqual(actual, expected)

def test_blob_set_item_with_offset(self):
self.blob.seek(0, SEEK_END)
self.assertEqual(self.blob.read(), b"") # verify that we're at EOB
self.blob[0] = b"T"
self.blob[-1] = b"."
self.blob.seek(0, SEEK_SET)
expected = b"This blob data string is exactly fifty bytes long."
self.assertEqual(self.blob.read(), expected)

def test_blob_set_buffer_object(self):
from array import array
self.blob[0] = memoryview(b"1")
Expand Down
6 changes: 3 additions & 3 deletions Modules/_sqlite/blob.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ static PyObject *
inner_read(pysqlite_Blob *self, Py_ssize_t length, Py_ssize_t offset)
{
assert(length <= sqlite3_blob_bytes(self->blob));
assert(offset <= sqlite3_blob_bytes(self->blob) - self->offset);
assert(offset <= sqlite3_blob_bytes(self->blob));

PyObject *buffer = PyBytes_FromStringAndSize(NULL, length);
if (buffer == NULL) {
Expand Down Expand Up @@ -188,13 +188,13 @@ inner_write(pysqlite_Blob *self, const void *buf, Py_ssize_t len,
Py_ssize_t offset)
{
int blob_len = sqlite3_blob_bytes(self->blob);
int remaining_len = blob_len - self->offset;
int remaining_len = blob_len - offset;
if (len > remaining_len) {
PyErr_SetString(PyExc_ValueError, "data longer than blob length");
return -1;
}

assert(offset <= remaining_len);
assert(offset <= blob_len);
int rc;
Py_BEGIN_ALLOW_THREADS
rc = sqlite3_blob_write(self->blob, buf, (int)len, (int)offset);
Expand Down