FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-129813: PEP 782: Use PyBytesWriter in _sqlite by vstinner · Pull Request #138956 · python/cpython · GitHub

Repository navigation

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
33 changes: 19 additions & 14 deletions Modules/_sqlite/blob.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -143,23 +143,23 @@ read_multiple(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));

PyObject *buffer = PyBytes_FromStringAndSize(NULL, length);
if (buffer == NULL) {
PyBytesWriter *writer = PyBytesWriter_Create(length);
if (writer == NULL) {
return NULL;
}
char *raw_buffer = PyBytesWriter_GetData(writer);

char *raw_buffer = PyBytes_AS_STRING(buffer);
int rc;
Py_BEGIN_ALLOW_THREADS
rc = sqlite3_blob_read(self->blob, raw_buffer, (int)length, (int)offset);
Py_END_ALLOW_THREADS

if (rc != SQLITE_OK) {
Py_DECREF(buffer);
PyBytesWriter_Discard(writer);
blob_seterror(self, rc);
return NULL;
}
return buffer;
return PyBytesWriter_Finish(writer);
}


Expand Down Expand Up @@ -196,7 +196,7 @@ blob_read_impl(pysqlite_Blob *self, int length)

assert(length >= 0);
if (length == 0) {
return PyBytes_FromStringAndSize(NULL, 0);
return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
}

PyObject *buffer = read_multiple(self, length, self->offset);
Expand Down Expand Up @@ -440,20 +440,25 @@ subscript_slice(pysqlite_Blob *self, PyObject *item)
if (step == 1) {
return read_multiple(self, len, start);
}

PyObject *blob = read_multiple(self, stop - start, start);
if (blob == NULL) {
return NULL;
}
PyObject *result = PyBytes_FromStringAndSize(NULL, len);
if (result != NULL) {
char *blob_buf = PyBytes_AS_STRING(blob);
char *res_buf = PyBytes_AS_STRING(result);
for (Py_ssize_t i = 0, j = 0; i < len; i++, j += step) {
res_buf[i] = blob_buf[j];
}

PyBytesWriter *writer = PyBytesWriter_Create(len);
if (writer == NULL) {
Py_DECREF(blob);
return NULL;
}
char *res_buf = PyBytesWriter_GetData(writer);

char *blob_buf = PyBytes_AS_STRING(blob);
for (Py_ssize_t i = 0, j = 0; i < len; i++, j += step) {
res_buf[i] = blob_buf[j];
}
return result;
Py_DECREF(blob);
return PyBytesWriter_Finish(writer);
}

static PyObject *
Expand Down
Loading

Back | FazBrowse Home | New Git URL