Skip to content

Commit 125a58c

Browse files
author
Erlend E. Aasland
committed
Let AC convert data to buffer
1 parent 74ab8a7 commit 125a58c

File tree

2 files changed

+32
-25
lines changed

2 files changed

+32
-25
lines changed

Modules/_sqlite/clinic/connection.c.h

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,7 @@ PyDoc_STRVAR(deserialize__doc__,
711711
{"deserialize", (PyCFunction)(void(*)(void))deserialize, METH_FASTCALL|METH_KEYWORDS, deserialize__doc__},
712712

713713
static PyObject *
714-
deserialize_impl(pysqlite_Connection *self, PyObject *data,
714+
deserialize_impl(pysqlite_Connection *self, Py_buffer *data,
715715
const char *schema);
716716

717717
static PyObject *
@@ -722,14 +722,30 @@ deserialize(pysqlite_Connection *self, PyObject *const *args, Py_ssize_t nargs,
722722
static _PyArg_Parser _parser = {NULL, _keywords, "deserialize", 0};
723723
PyObject *argsbuf[2];
724724
Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
725-
PyObject *data;
725+
Py_buffer data = {NULL, NULL};
726726
const char *schema = "main";
727727

728728
args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
729729
if (!args) {
730730
goto exit;
731731
}
732-
data = args[0];
732+
if (PyUnicode_Check(args[0])) {
733+
Py_ssize_t len;
734+
const char *ptr = PyUnicode_AsUTF8AndSize(args[0], &len);
735+
if (ptr == NULL) {
736+
goto exit;
737+
}
738+
PyBuffer_FillInfo(&data, args[0], (void *)ptr, len, 1, 0);
739+
}
740+
else { /* any bytes-like object */
741+
if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
742+
goto exit;
743+
}
744+
if (!PyBuffer_IsContiguous(&data, 'C')) {
745+
_PyArg_BadArgument("deserialize", "argument 1", "contiguous buffer", args[0]);
746+
goto exit;
747+
}
748+
}
733749
if (!noptargs) {
734750
goto skip_optional_kwonly;
735751
}
@@ -747,9 +763,14 @@ deserialize(pysqlite_Connection *self, PyObject *const *args, Py_ssize_t nargs,
747763
goto exit;
748764
}
749765
skip_optional_kwonly:
750-
return_value = deserialize_impl(self, data, schema);
766+
return_value = deserialize_impl(self, &data, schema);
751767

752768
exit:
769+
/* Cleanup for data */
770+
if (data.obj) {
771+
PyBuffer_Release(&data);
772+
}
773+
753774
return return_value;
754775
}
755776

@@ -825,4 +846,4 @@ pysqlite_connection_exit(pysqlite_Connection *self, PyObject *const *args, Py_ss
825846
#ifndef DESERIALIZE_METHODDEF
826847
#define DESERIALIZE_METHODDEF
827848
#endif /* !defined(DESERIALIZE_METHODDEF) */
828-
/*[clinic end generated code: output=19c5e77935335c4d input=a9049054013a1b77]*/
849+
/*[clinic end generated code: output=b84e5a71dd4a0e61 input=a9049054013a1b77]*/

Modules/_sqlite/connection.c

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1845,48 +1845,34 @@ reset_all_statements(sqlite3 *db)
18451845
_sqlite3.Connection.deserialize as deserialize
18461846
18471847
1848-
data: object
1848+
data: Py_buffer(accept={buffer, str})
18491849
/
18501850
*
18511851
schema: str = "main"
18521852
18531853
[clinic start generated code]*/
18541854

18551855
static PyObject *
1856-
deserialize_impl(pysqlite_Connection *self, PyObject *data,
1856+
deserialize_impl(pysqlite_Connection *self, Py_buffer *data,
18571857
const char *schema)
1858-
/*[clinic end generated code: output=264709775101cd18 input=fc2cc53bd3736b89]*/
1858+
/*[clinic end generated code: output=96b8470aaebf1b25 input=c67fca5dac036eec]*/
18591859
{
1860-
if (PyObject_CheckBuffer(data) < 0) {
1861-
PyErr_SetString(PyExc_ValueError,
1862-
"could not convert 'data' to buffer");
1863-
return NULL;
1864-
}
1865-
1866-
Py_buffer view;
1867-
if (PyObject_GetBuffer(data, &view, PyBUF_SIMPLE) < 0) {
1868-
return NULL;
1869-
}
1870-
1871-
if (view.len > 9223372036854775807) {
1860+
if (data->len > 9223372036854775807) {
18721861
PyErr_SetString(PyExc_OverflowError, "'data' is too large");
1873-
PyBuffer_Release(&view);
18741862
return NULL;
18751863
}
18761864

18771865
if (self->db) {
18781866
reset_all_statements(self->db);
18791867
}
18801868

1881-
Py_ssize_t size = view.len;
1882-
unsigned char *buf = view.buf;
1869+
Py_ssize_t size = data->len;
18831870
const unsigned int flags = 0;
18841871
int rc;
18851872
Py_BEGIN_ALLOW_THREADS
1886-
rc = sqlite3_deserialize(self->db, schema, buf, size, size, flags);
1873+
rc = sqlite3_deserialize(self->db, schema, data->buf, size, size, flags);
18871874
Py_END_ALLOW_THREADS
18881875

1889-
PyBuffer_Release(&view);
18901876
if (rc != SQLITE_OK) {
18911877
_pysqlite_seterror(self->db);
18921878
return NULL;

0 commit comments

Comments
 (0)