8000 [2.7] bpo-20047: Make bytearray methods partition() and rpartition() … · python/cpython@107f3cc · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 107f3cc

Browse files
[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 a231428)
1 parent 7c622be commit 107f3cc

File tree

4 files changed

+3488
-9
lines changed

4 files changed

+3488
-9
lines changed

Lib/test/test_bytes.py

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,16 @@ def test_replace(self):
336336
self.assertEqual(b.replace(b'i', b'a'), b'massassappa')
337337
self.assertEqual(b.replace(b'ss', b'x'), b'mixixippi')
338338

339+
def test_replace_int_error(self):
340+
self.assertRaises(TypeError, self.type2test(b'a b').replace, 32, b'')
341+
339342
def test_split_string_error(self):
340343
self.assertRaises(TypeError, self.type2test(b'a b').split, u' ')
344+
self.assertRaises(TypeError, self.type2test(b'a b').rsplit, u' ')
345+
346+
def test_split_int_error(self):
347+
self.assertRaises(TypeError, self.type2test(b'a b').split, 32)
348+
self.assertRaises(TypeError, self.type2test(b'a b').rsplit, 32)
341349

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

349-
def test_rsplit_string_error(self):
350-
self.assertRaises(TypeError, self.type2test(b'a b').rsplit, u' ')
351-
352357
def test_rsplit_unicodewhitespace(self):
353358
b = self.type2test(b"\x09\x0A\x0B\x0C\x0D\x1C\x1D\x1E\x1F")
354359
self.assertEqual(b.rsplit(), [b'\x1c\x1d\x1e\x1f'])
@@ -364,6 +369,14 @@ def test_rpartition(self):
364369
self.assertEqual(b.rpartition(b'i'), (b'mississipp', b'i', b''))
365370
self.assertEqual(b.rpartition(b'w'), (b'', b'', b'mississippi'))
366371

372+
def test_partition_string_error(self):
373+
self.assertRaises(TypeError, self.type2test(b'a b').partition, u' ')
374+
self.assertRaises(TypeError, self.type2test(b'a b').rpartition, u' ')
375+
376+
def test_partition_int_error(self):
377+
self.assertRaises(TypeError, self.type2test(b'a b').partition, 32)
378+
self.assertRaises(TypeError, self.type2test(b'a b').rpartition, 32)
379+
367380
def test_pickling(self):
368381
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
369382
for b in b"", b"a", b"abc", b"\xffab\x80", b"\0\0\377\0\0":
@@ -378,9 +391,19 @@ def test_strip_bytearray(self):
378391
self.assertEqual(self.type2test(b'abc').rstrip(memoryview(b'ac')), b'ab')
379392

380393
def test_strip_string_error(self):
381-
self.assertRaises(TypeError, self.type2test(b'abc').strip, u'b')
382-
self.assertRaises(TypeError, self.type2test(b'abc').lstrip, u'b')
383-
self.assertRaises(TypeError, self.type2test(b'abc').rstrip, u'b')
394+
self.assertRaises(TypeError, self.type2test(b'abc').strip, u'ac')
395+
self.assertRaises(TypeError, self.type2test(b'abc').lstrip, u'ac')
396+
self.assertRaises(TypeError, self.type2test(b'abc').rstrip, u'ac')
397+
398+
def test_strip_int_error(self):
399+
self.assertRaises(TypeError, self.type2test(b' abc ').strip, 32)
400+
self.assertRaises(TypeError, self.type2test(b' abc ').lstrip, 32)
401+
self.assertRaises(TypeError, self.type2test(b' abc ').rstrip, 32)
402+
403+
def test_xjust_int_error(self):
404+
self.assertRaises(TypeError, self.type2test(b'abc').center, 7, 32)
405+
self.assertRaises(TypeError, self.type2test(b'abc').ljust, 7, 32)
406+
self.assertRaises(TypeError, self.type2test(b'abc').rjust, 7, 32)
384407

385408
def test_ord(self):
386409
b = self.type2test(b'\0A\x7f\x80\xff')
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Bytearray methods partition() and rpartition() now accept only bytes-like
2+
objects as separator, as documented. In particular they now raise TypeError
3+
rather of returning a bogus result when an integer is passed as a separator.

Objects/bytearrayobject.c

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,26 @@ PyByteArray_FromObject(PyObject *input)
164164
input, NULL);
165165
}
166166

167+
static PyObject *
168+
_PyByteArray_FromBufferObject(PyObject *obj)
169+
{
170+
PyObject *result;
171+
Py_buffer view;
172+
173+
if (PyObject_GetBuffer(obj, &view, PyBUF_FULL_RO) < 0) {
174+
return NULL;
175+
}
176+
result = PyByteArray_FromStringAndSize(NULL, view.len);
177+
if (result != NULL &&
178+
PyBuffer_ToContiguous(PyByteArray_AS_STRING(result),
179+
&view, view.len, 'C') < 0)
180+
{
181+
Py_CLEAR(result);
182+
}
183+
PyBuffer_Release(&view);
184+
return result;
185+
}
186+
167187
PyObject *
168188
PyByteArray_FromStringAndSize(const char *bytes, Py_ssize_t size)
169189
{
@@ -483,7 +503,8 @@ bytearray_setslice(PyByteArrayObject *self, Py_ssize_t lo, Py_ssize_t hi,
483503
if (values == (PyObject *)self) {
484504
/* Make a copy and call this function recursively */
485505
int err;
486-
values = PyByteArray_FromObject(values);
506+
values = PyByteArray_FromStringAndSize(PyByteArray_AS_STRING(values),
507+
PyByteArray_GET_SIZE(values));
487508
if (values == NULL)
488509
return -1;
489510
err = bytearray_setslice(self, lo, hi, values);
@@ -2098,7 +2119,7 @@ bytearray_partition(PyByteArrayObject *self, PyObject *sep_obj)
20982119
{
20992120
PyObject *bytesep, *result;
21002121

2101-
bytesep = PyByteArray_FromObject(sep_obj);
2122+
bytesep = _PyByteArray_FromBufferObject(sep_obj);
21022123
if (! bytesep)
21032124
return NULL;
21042125

@@ -2126,7 +2147,7 @@ bytearray_rpartition(PyByteArrayObject *self, PyObject *sep_obj)
21262147
{
21272148
PyObject *bytesep, *result;
21282149

2129-
bytesep = PyByteArray_FromObject(sep_obj);
2150+
bytesep = _PyByteArray_FromBufferObject(sep_obj);
21302151
if (! bytesep)
21312152
return NULL;
21322153

0 commit comments

Comments
 (0)
0