summaryrefslogtreecommitdiffstats
path: root/Objects/bytearrayobject.c
diff options
context:
space:
mode:
Diffstat (limited to 'Objects/bytearrayobject.c')
-rw-r--r--Objects/bytearrayobject.c27
1 files changed, 24 insertions, 3 deletions
diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c
index 72f2ad8..04c2506 100644
--- a/Objects/bytearrayobject.c
+++ b/Objects/bytearrayobject.c
@@ -164,6 +164,26 @@ PyByteArray_FromObject(PyObject *input)
input, NULL);
}
+static PyObject *
+_PyByteArray_FromBufferObject(PyObject *obj)
+{
+ PyObject *result;
+ Py_buffer view;
+
+ if (PyObject_GetBuffer(obj, &view, PyBUF_FULL_RO) < 0) {
+ return NULL;
+ }
+ result = PyByteArray_FromStringAndSize(NULL, view.len);
+ if (result != NULL &&
+ PyBuffer_ToContiguous(PyByteArray_AS_STRING(result),
+ &view, view.len, 'C') < 0)
+ {
+ Py_CLEAR(result);
+ }
+ PyBuffer_Release(&view);
+ return result;
+}
+
PyObject *
PyByteArray_FromStringAndSize(const char *bytes, Py_ssize_t size)
{
@@ -483,7 +503,8 @@ bytearray_setslice(PyByteArrayObject *self, Py_ssize_t lo, Py_ssize_t hi,
if (values == (PyObject *)self) {
/* Make a copy and call this function recursively */
int err;
- values = PyByteArray_FromObject(values);
+ values = PyByteArray_FromStringAndSize(PyByteArray_AS_STRING(values),
+ PyByteArray_GET_SIZE(values));
if (values == NULL)
return -1;
err = bytearray_setslice(self, lo, hi, values);
@@ -2098,7 +2119,7 @@ bytearray_partition(PyByteArrayObject *self, PyObject *sep_obj)
{
PyObject *bytesep, *result;
- bytesep = PyByteArray_FromObject(sep_obj);
+ bytesep = _PyByteArray_FromBufferObject(sep_obj);
if (! bytesep)
return NULL;
@@ -2126,7 +2147,7 @@ bytearray_rpartition(PyByteArrayObject *self, PyObject *sep_obj)
{
PyObject *bytesep, *result;
- bytesep = PyByteArray_FromObject(sep_obj);
+ bytesep = _PyByteArray_FromBufferObject(sep_obj);
if (! bytesep)
return NULL;