Skip to content

[2.7] bpo-20047: Make bytearray methods partition() and rpartition() rejecting (GH-4158) #4163

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions Lib/test/test_bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,16 @@ def test_replace(self):
self.assertEqual(b.replace(b'i', b'a'), b'massassappa')
self.assertEqual(b.replace(b'ss', b'x'), b'mixixippi')

def test_replace_int_error(self):
self.assertRaises(TypeError, self.type2test(b'a b').replace, 32, b'')

def test_split_string_error(self):
self.assertRaises(TypeError, self.type2test(b'a b').split, u' ')
self.assertRaises(TypeError, self.type2test(b'a b').rsplit, u' ')

def test_split_int_error(self):
self.assertRaises(TypeError, self.type2test(b'a b').split, 32)
self.assertRaises(TypeError, self.type2test(b'a b').rsplit, 32)

def test_split_unicodewhitespace(self):
for b in (b'a\x1Cb', b'a\x1Db', b'a\x1Eb', b'a\x1Fb'):
Expand All @@ -346,9 +354,6 @@ def test_split_unicodewhitespace(self):
b = self.type2test(b"\x09\x0A\x0B\x0C\x0D\x1C\x1D\x1E\x1F")
self.assertEqual(b.split(), [b'\x1c\x1d\x1e\x1f'])

def test_rsplit_string_error(self):
self.assertRaises(TypeError, self.type2test(b'a b').rsplit, u' ')

def test_rsplit_unicodewhitespace(self):
b = self.type2test(b"\x09\x0A\x0B\x0C\x0D\x1C\x1D\x1E\x1F")
self.assertEqual(b.rsplit(), [b'\x1c\x1d\x1e\x1f'])
Expand All @@ -364,6 +369,14 @@ def test_rpartition(self):
self.assertEqual(b.rpartition(b'i'), (b'mississipp', b'i', b''))
self.assertEqual(b.rpartition(b'w'), (b'', b'', b'mississippi'))

def test_partition_string_error(self):
self.assertRaises(TypeError, self.type2test(b'a b').partition, u' ')
self.assertRaises(TypeError, self.type2test(b'a b').rpartition, u' ')

def test_partition_int_error(self):
self.assertRaises(TypeError, self.type2test(b'a b').partition, 32)
self.assertRaises(TypeError, self.type2test(b'a b').rpartition, 32)

def test_pickling(self):
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
for b in b"", b"a", b"abc", b"\xffab\x80", b"\0\0\377\0\0":
Expand All @@ -378,9 +391,19 @@ def test_strip_bytearray(self):
self.assertEqual(self.type2test(b'abc').rstrip(memoryview(b'ac')), b'ab')

def test_strip_string_error(self):
self.assertRaises(TypeError, self.type2test(b'abc').strip, u'b')
self.assertRaises(TypeError, self.type2test(b'abc').lstrip, u'b')
self.assertRaises(TypeError, self.type2test(b'abc').rstrip, u'b')
self.assertRaises(TypeError, self.type2test(b'abc').strip, u'ac')
self.assertRaises(TypeError, self.type2test(b'abc').lstrip, u'ac')
self.assertRaises(TypeError, self.type2test(b'abc').rstrip, u'ac')

def test_strip_int_error(self):
self.assertRaises(TypeError, self.type2test(b' abc ').strip, 32)
self.assertRaises(TypeError, self.type2test(b' abc ').lstrip, 32)
self.assertRaises(TypeError, self.type2test(b' abc ').rstrip, 32)

def test_xjust_int_error(self):
self.assertRaises(TypeError, self.type2test(b'abc').center, 7, 32)
self.assertRaises(TypeError, self.type2test(b'abc').ljust, 7, 32)
self.assertRaises(TypeError, self.type2test(b'abc').rjust, 7, 32)

def test_ord(self):
b = self.type2test(b'\0A\x7f\x80\xff')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Bytearray methods partition() and rpartition() now accept only bytes-like
objects as separator, as documented. In particular they now raise TypeError
rather of returning a bogus result when an integer is passed as a separator.
27 changes: 24 additions & 3 deletions Objects/bytearrayobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down
Loading