bytearray.partition(sep)
Split the sequence at the first occurrence of *sep*, and return a 3-tuple
- containing the part before the separator, the separator, and the part
- after the separator. If the separator is not found, return a 3-tuple
+ containing the part before the separator, the separator itself or its
+ bytearray copy, and the part after the separator.
+ If the separator is not found, return a 3-tuple
containing a copy of the original sequence, followed by two empty bytes or
bytearray objects.
bytearray.rpartition(sep)
Split the sequence at the last occurrence of *sep*, and return a 3-tuple
- containing the part before the separator, the separator, and the part
- after the separator. If the separator is not found, return a 3-tuple
+ containing the part before the separator, the separator itself or its
+ bytearray copy, and the part after the separator.
+ If the separator is not found, return a 3-tuple
containing a copy of the original sequence, followed by two empty bytes or
bytearray objects.
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, ' ')
+ self.assertRaises(TypeError, self.type2test(b'a b').rsplit, ' ')
+
+ 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'):
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, ' ')
-
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'])
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, ' ')
+ self.assertRaises(TypeError, self.type2test(b'a b').rpartition, ' ')
+
+ 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":
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, 'b')
- self.assertRaises(TypeError, self.type2test(b'abc').lstrip, 'b')
- self.assertRaises(TypeError, self.type2test(b'abc').rstrip, 'b')
+ self.assertRaises(TypeError, self.type2test(b'abc').strip, 'ac')
+ self.assertRaises(TypeError, self.type2test(b'abc').lstrip, 'ac')
+ self.assertRaises(TypeError, self.type2test(b'abc').rstrip, '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_center(self):
# Fill character can be either bytes or bytearray (issue 12380)
self.assertEqual(b.rjust(7, fill_type(b'-')),
self.type2test(b'----abc'))
+ 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')
self.assertEqual([ord(b[i:i+1]) for i in range(len(b))],
--- /dev/null
+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.
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)
{
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);
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;
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;
Partition the bytes into three parts using the given separator.
-This will search for the separator sep in the bytes, starting and the end. If
+This will search for the separator sep in the bytes, 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.
static PyObject *
bytes_rpartition_impl(PyBytesObject *self, Py_buffer *sep)
-/*[clinic end generated code: output=191b114cbb028e50 input=67f689e63a62d478]*/
+/*[clinic end generated code: output=191b114cbb028e50 input=d78db010c8cfdbe1]*/
{
return stringlib_rpartition(
(PyObject*) self,
"\n"
"This will search for the separator sep in the bytearray. If the separator is\n"
"found, returns a 3-tuple containing the part before the separator, the\n"
-"separator itself, and the part after it.\n"
+"separator itself, and the part after it as new bytearray objects.\n"
"\n"
-"If the separator is not found, returns a 3-tuple containing the original\n"
-"bytearray object and two empty bytearray objects.");
+"If the separator is not found, returns a 3-tuple containing the copy of the\n"
+"original bytearray object and two empty bytearray objects.");
#define BYTEARRAY_PARTITION_METHODDEF \
{"partition", (PyCFunction)bytearray_partition, METH_O, bytearray_partition__doc__},
"rpartition($self, sep, /)\n"
"--\n"
"\n"
-"Partition the bytes into three parts using the given separator.\n"
+"Partition the bytearray into three parts using the given separator.\n"
"\n"
-"This will search for the separator sep in the bytearray, starting and the end.\n"
+"This will search for the separator sep in the bytearray, starting at the end.\n"
"If the separator is found, returns a 3-tuple containing the part before the\n"
-"separator, the separator itself, and the part after it.\n"
+"separator, the separator itself, and the part after it as new bytearray\n"
+"objects.\n"
"\n"
"If the separator is not found, returns a 3-tuple containing two empty bytearray\n"
-"objects and the original bytearray object.");
+"objects and the copy of the original bytearray object.");
#define BYTEARRAY_RPARTITION_METHODDEF \
{"rpartition", (PyCFunction)bytearray_rpartition, METH_O, bytearray_rpartition__doc__},
{
return bytearray_sizeof_impl(self);
}
-/*[clinic end generated code: output=225342a680391b9c input=a9049054013a1b77]*/
+/*[clinic end generated code: output=8f022100f059226c input=a9049054013a1b77]*/
"\n"
"Partition the bytes into three parts using the given separator.\n"
"\n"
-"This will search for the separator sep in the bytes, starting and the end. If\n"
+"This will search for the separator sep in the bytes, starting at the end. If\n"
"the separator is found, returns a 3-tuple containing the part before the\n"
"separator, the separator itself, and the part after it.\n"
"\n"
exit:
return return_value;
}
-/*[clinic end generated code: output=2dc3c93cfd2dc440 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=4ac7e35150d47467 input=a9049054013a1b77]*/