diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2017-10-28 23:11:54 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-10-28 23:11:54 (GMT) |
commit | a2314283ff87c65e1745a42c2f2b716b1a209128 (patch) | |
tree | b1b1287aeea750c8694673939ec72b469c19cc59 /Objects/bytearrayobject.c | |
parent | 5a4bbcd479ce86f68bbe12bc8c16e3447f32e13a (diff) | |
download | cpython-a2314283ff87c65e1745a42c2f2b716b1a209128.zip cpython-a2314283ff87c65e1745a42c2f2b716b1a209128.tar.gz cpython-a2314283ff87c65e1745a42c2f2b716b1a209128.tar.bz2 |
bpo-20047: Make bytearray methods partition() and rpartition() rejecting (#4158)
separators that are not bytes-like objects.
Diffstat (limited to 'Objects/bytearrayobject.c')
-rw-r--r-- | Objects/bytearrayobject.c | 46 |
1 files changed, 34 insertions, 12 deletions
diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c index 840d5b0..c92cfc0 100644 --- a/Objects/bytearrayobject.c +++ b/Objects/bytearrayobject.c @@ -104,6 +104,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) { @@ -536,7 +556,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); @@ -1387,19 +1408,19 @@ Partition the bytearray into three parts using the given separator. This will search for the separator sep in the bytearray. If the separator is found, returns a 3-tuple containing the part before the separator, the -separator itself, and the part after it. +separator itself, and the part after it as new bytearray objects. -If the separator is not found, returns a 3-tuple containing the original -bytearray object and two empty bytearray objects. +If the separator is not found, returns a 3-tuple containing the copy of the +original bytearray object and two empty bytearray objects. [clinic start generated code]*/ static PyObject * bytearray_partition(PyByteArrayObject *self, PyObject *sep) -/*[clinic end generated code: output=45d2525ddd35f957 input=86f89223892b70b5]*/ +/*[clinic end generated code: output=45d2525ddd35f957 input=8f644749ee4fc83a]*/ { PyObject *bytesep, *result; - bytesep = PyByteArray_FromObject(sep); + bytesep = _PyByteArray_FromBufferObject(sep); if (! bytesep) return NULL; @@ -1420,23 +1441,24 @@ bytearray.rpartition sep: object / -Partition the bytes into three parts using the given separator. +Partition the bytearray into three parts using the given separator. -This will search for the separator sep in the bytearray, starting and the end. +This will search for the separator sep in the bytearray, starting at the end. If the separator is found, returns a 3-tuple containing the part before the -separator, the separator itself, and the part after it. +separator, the separator itself, and the part after it as new bytearray +objects. If the separator is not found, returns a 3-tuple containing two empty bytearray -objects and the original bytearray object. +objects and the copy of the original bytearray object. [clinic start generated code]*/ static PyObject * bytearray_rpartition(PyByteArrayObject *self, PyObject *sep) -/*[clinic end generated code: output=440de3c9426115e8 input=5f4094f2de87c8f3]*/ +/*[clinic end generated code: output=440de3c9426115e8 input=7e3df3e6cb8fa0ac]*/ { PyObject *bytesep, *result; - bytesep = PyByteArray_FromObject(sep); + bytesep = _PyByteArray_FromBufferObject(sep); if (! bytesep) return NULL; |