[2.7] bpo-20047: Make bytearray methods partition() and rpartition() rejecting (GH-4158) (#4163)
separators that are not bytes-like objects..
(cherry picked from commit a2314283ff87c65e1745a42c2f2b716b1a209128)
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 @@
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 @@
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 @@
{
PyObject *bytesep, *result;
- bytesep = PyByteArray_FromObject(sep_obj);
+ bytesep = _PyByteArray_FromBufferObject(sep_obj);
if (! bytesep)
return NULL;
@@ -2126,7 +2147,7 @@
{
PyObject *bytesep, *result;
- bytesep = PyByteArray_FromObject(sep_obj);
+ bytesep = _PyByteArray_FromBufferObject(sep_obj);
if (! bytesep)
return NULL;