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
Prev Previous commit
Next Next commit
Accept all buffer objects
  • Loading branch information
Erlend E. Aasland committed Apr 17, 2022
commit a1797495a901bc708b2227e6f6262e0d1ae1a174
9 changes: 9 additions & 0 deletions Lib/test/test_sqlite3/test_dbapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1178,6 +1178,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_buffer_object(self):
from array import array
self.blob[0] = memoryview(b"1")
Comment thread
erlend-aasland marked this conversation as resolved.
self.blob[0] = bytearray(b"1")
self.blob[0] = array("b", [1])
self.blob[0:5] = memoryview(b"12345")
self.blob[0:5] = bytearray(b"12345")
self.blob[0:5] = array("b", [1, 2, 3, 4, 5])
Comment thread
erlend-aasland marked this conversation as resolved.

def test_blob_set_item_negative_index(self):
self.blob[-1] = b"z"
self.assertEqual(self.blob[-1], b"z")
Expand Down
20 changes: 13 additions & 7 deletions Modules/_sqlite/blob.c
Original file line number Diff line number Diff line change
Expand Up @@ -455,18 +455,24 @@ ass_subscript_index(pysqlite_Blob *self, PyObject *item, PyObject *value)
"Blob doesn't support item deletion");
return -1;
}
if (!PyBytes_Check(value) || PyBytes_Size(value) != 1) {
PyErr_SetString(PyExc_ValueError,
"Blob assignment must be a single byte");
Py_ssize_t i = get_subscript_index(self, item);
if (i < 0) {
return -1;
}

Py_ssize_t i = get_subscript_index(self, item);
if (i < 0) {
Py_buffer vbuf;
if (PyObject_GetBuffer(value, &vbuf, PyBUF_SIMPLE) < 0) {
return -1;
}
const char *buf = PyBytes_AS_STRING(value);
return inner_write(self, buf, 1, i);
int rc = -1;
if (vbuf.len != 1) {
PyErr_SetString(PyExc_ValueError, "Blob assignment must be a single byte");
}
else {
rc = inner_write(self, (const char *)vbuf.buf, 1, i);
}
PyBuffer_Release(&vbuf);
return rc;
}

static int
Expand Down