Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit107f3cc

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 commita231428)
1 parent7c622be commit107f3cc

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+
deftest_replace_int_error(self):
340+
self.assertRaises(TypeError,self.type2test(b'a b').replace,32,b'')
341+
339342
deftest_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+
deftest_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
deftest_split_unicodewhitespace(self):
343351
forbin (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-
deftest_rsplit_string_error(self):
350-
self.assertRaises(TypeError,self.type2test(b'a b').rsplit,u' ')
351-
352357
deftest_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+
deftest_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+
deftest_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
deftest_pickling(self):
368381
forprotoinrange(pickle.HIGHEST_PROTOCOL+1):
369382
forbinb"",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
deftest_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+
deftest_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+
deftest_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
deftest_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+
staticPyObject*
168+
_PyByteArray_FromBufferObject(PyObject*obj)
169+
{
170+
PyObject*result;
171+
Py_bufferview;
172+
173+
if (PyObject_GetBuffer(obj,&view,PyBUF_FULL_RO)<0) {
174+
returnNULL;
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+
returnresult;
185+
}
186+
167187
PyObject*
168188
PyByteArray_FromStringAndSize(constchar*bytes,Py_ssize_tsize)
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
interr;
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
returnNULL;
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
returnNULL;
21322153

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2026 Movatter.jp