Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
a2314283
Commit
a2314283
authored
Oct 29, 2017
by
Serhiy Storchaka
Committed by
GitHub
Oct 29, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-20047: Make bytearray methods partition() and rpartition() rejecting (#4158)
separators that are not bytes-like objects.
parent
5a4bbcd4
Changes
9
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
89 additions
and
38 deletions
+89
-38
Doc/library/stdtypes.rst
Doc/library/stdtypes.rst
+6
-4
Lib/test/test_bytes.py
Lib/test/test_bytes.py
+29
-6
Misc/NEWS.d/next/Core and Builtins/2017-10-28-19-11-05.bpo-20047.GuNAto.rst
...ore and Builtins/2017-10-28-19-11-05.bpo-20047.GuNAto.rst
+3
-0
Objects/bytearrayobject.c
Objects/bytearrayobject.c
+34
-12
Objects/bytesobject.c
Objects/bytesobject.c
+2
-2
Objects/clinic/bytearrayobject.c.h
Objects/clinic/bytearrayobject.c.h
+9
-8
Objects/clinic/bytesobject.c.h
Objects/clinic/bytesobject.c.h
+2
-2
Objects/clinic/unicodeobject.c.h
Objects/clinic/unicodeobject.c.h
+2
-2
Objects/unicodeobject.c
Objects/unicodeobject.c
+2
-2
No files found.
Doc/library/stdtypes.rst
View file @
a2314283
...
...
@@ -2573,8 +2573,9 @@ arbitrary binary data.
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.
...
...
@@ -2629,8 +2630,9 @@ arbitrary binary data.
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.
...
...
Lib/test/test_bytes.py
View file @
a2314283
...
...
@@ -548,8 +548,16 @@ class BaseBytesTest:
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
'):
...
...
@@ -558,9 +566,6 @@ class BaseBytesTest:
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
'])
...
...
@@ -576,6 +581,14 @@ class BaseBytesTest:
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"
\
xff
ab
\
x80
", b"
\
0
\
0
\
377
\
0
\
0
":
...
...
@@ -608,9 +621,14 @@ class BaseBytesTest:
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)
...
...
@@ -633,6 +651,11 @@ class BaseBytesTest:
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'
\
0
A
\
x7f
\
x80
\
xff
')
self.assertEqual([ord(b[i:i+1]) for i in range(len(b))],
...
...
Misc/NEWS.d/next/Core and Builtins/2017-10-28-19-11-05.bpo-20047.GuNAto.rst
0 → 100644
View file @
a2314283
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.
Objects/bytearrayobject.c
View file @
a2314283
...
...
@@ -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=8
6f89223892b70b5
]*/
/*[clinic end generated code: output=45d2525ddd35f957 input=8
f644749ee4fc83a
]*/
{
PyObject
*
bytesep
,
*
result
;
bytesep
=
PyByteArray_From
Object
(
sep
);
bytesep
=
_PyByteArray_FromBuffer
Object
(
sep
);
if
(
!
bytesep
)
return
NULL
;
...
...
@@ -1420,23 +1441,24 @@ bytearray.rpartition
sep: object
/
Partition the byte
s
into three parts using the given separator.
Partition the byte
array
into three parts using the given separator.
This will search for the separator sep in the bytearray, starting a
nd
the end.
This will search for the separator sep in the bytearray, starting a
t
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_From
Object
(
sep
);
bytesep
=
_PyByteArray_FromBuffer
Object
(
sep
);
if
(
!
bytesep
)
return
NULL
;
...
...
Objects/bytesobject.c
View file @
a2314283
...
...
@@ -1834,7 +1834,7 @@ bytes.rpartition
Partition the bytes into three parts using the given separator.
This will search for the separator sep in the bytes, starting a
nd
the end. If
This will search for the separator sep in the bytes, starting a
t
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.
...
...
@@ -1844,7 +1844,7 @@ objects and the original bytes object.
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
,
...
...
Objects/clinic/bytearrayobject.c.h
View file @
a2314283
...
...
@@ -214,10 +214,10 @@ PyDoc_STRVAR(bytearray_partition__doc__,
"
\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__},
...
...
@@ -226,14 +226,15 @@ PyDoc_STRVAR(bytearray_rpartition__doc__,
"rpartition($self, sep, /)
\n
"
"--
\n
"
"
\n
"
"Partition the byte
s
into three parts using the given separator.
\n
"
"Partition the byte
array
into three parts using the given separator.
\n
"
"
\n
"
"This will search for the separator sep in the bytearray, starting a
nd
the end.
\n
"
"This will search for the separator sep in the bytearray, starting a
t
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__},
...
...
@@ -711,4 +712,4 @@ bytearray_sizeof(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored))
{
return
bytearray_sizeof_impl
(
self
);
}
/*[clinic end generated code: output=
e53f10084457a46b
input=a9049054013a1b77]*/
/*[clinic end generated code: output=
c2804d009182328c
input=a9049054013a1b77]*/
Objects/clinic/bytesobject.c.h
View file @
a2314283
...
...
@@ -86,7 +86,7 @@ PyDoc_STRVAR(bytes_rpartition__doc__,
"
\n
"
"Partition the bytes into three parts using the given separator.
\n
"
"
\n
"
"This will search for the separator sep in the bytes, starting a
nd
the end. If
\n
"
"This will search for the separator sep in the bytes, starting a
t
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
"
...
...
@@ -499,4 +499,4 @@ bytes_fromhex(PyTypeObject *type, PyObject *arg)
exit:
return
return_value
;
}
/*[clinic end generated code: output=
9e3374bd7d04c163
input=a9049054013a1b77]*/
/*[clinic end generated code: output=
fc9e02359cc56d36
input=a9049054013a1b77]*/
Objects/clinic/unicodeobject.c.h
View file @
a2314283
...
...
@@ -682,7 +682,7 @@ PyDoc_STRVAR(unicode_rpartition__doc__,
"
\n
"
"Partition the string into three parts using the given separator.
\n
"
"
\n
"
"This will search for the separator in the string, starting a
nd
the end. If
\n
"
"This will search for the separator in the string, starting a
t
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
"
...
...
@@ -930,4 +930,4 @@ unicode_sizeof(PyObject *self, PyObject *Py_UNUSED(ignored))
{
return
unicode_sizeof_impl
(
self
);
}
/*[clinic end generated code: output=8
fd799fd7f2cc724
input=a9049054013a1b77]*/
/*[clinic end generated code: output=8
16292e81a8a732e
input=a9049054013a1b77]*/
Objects/unicodeobject.c
View file @
a2314283
...
...
@@ -13067,7 +13067,7 @@ str.rpartition as unicode_rpartition = str.partition
Partition the string into three parts using the given separator.
This will search for the separator in the string, starting a
nd
the end. If
This will search for the separator in the string, starting a
t
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.
...
...
@@ -13077,7 +13077,7 @@ and the original string.
static
PyObject
*
unicode_rpartition
(
PyObject
*
self
,
PyObject
*
sep
)
/*[clinic end generated code: output=1aa13cf1156572aa input=
e77c7acb69bdfca6
]*/
/*[clinic end generated code: output=1aa13cf1156572aa input=
c4b7db3ef5cf336a
]*/
{
return
PyUnicode_RPartition
(
self
,
sep
);
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment