Skip to content

Commit 2516f69

Browse files
committed
Fix bytes.split
1 parent 14b4fdc commit 2516f69

File tree

5 files changed

+130
-252
lines changed

5 files changed

+130
-252
lines changed

Lib/test/string_tests.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,6 @@ def test_expandtabs(self):
360360
self.checkraises(OverflowError,
361361
'\ta\n\tb', 'expandtabs', sys.maxsize)
362362

363-
@unittest.skip("TODO: RUSTPYTHON test_bytes")
364363
def test_split(self):
365364
# by a char
366365
self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|')
@@ -431,7 +430,6 @@ def test_split(self):
431430
self.checkraises(ValueError, 'hello', 'split', '')
432431
self.checkraises(ValueError, 'hello', 'split', '', 0)
433432

434-
@unittest.skip("TODO: RUSTPYTHON test_bytes")
435433
def test_rsplit(self):
436434
# by a char
437435
self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|')
@@ -697,8 +695,6 @@ def test_capitalize(self):
697695

698696
self.checkraises(TypeError, 'hello', 'capitalize', 42)
699697

700-
# TODO: RUSTPYTHON
701-
@unittest.expectedFailure
702698
def test_additional_split(self):
703699
self.checkequal(['this', 'is', 'the', 'split', 'function'],
704700
'this is the split function', 'split')
@@ -735,8 +731,6 @@ def test_additional_split(self):
735731
self.checkequal(['arf', 'barf'], b, 'split', None)
736732
self.checkequal(['arf', 'barf'], b, 'split', None, 2)
737733

738-
# TODO: RUSTPYTHON
739-
@unittest.expectedFailure
740734
def test_additional_rsplit(self):
741735
self.checkequal(['this', 'is', 'the', 'rsplit', 'function'],
742736
'this is the rsplit function', 'rsplit')

Lib/test/test_bytes.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -722,17 +722,13 @@ def test_split_int_error(self):
722722
self.assertRaises(TypeError, self.type2test(b'a b').split, 32)
723723
self.assertRaises(TypeError, self.type2test(b'a b').rsplit, 32)
724724

725-
# TODO: RUSTPYTHON
726-
@unittest.expectedFailure
727725
def test_split_unicodewhitespace(self):
728726
for b in (b'a\x1Cb', b'a\x1Db', b'a\x1Eb', b'a\x1Fb'):
729727
b = self.type2test(b)
730728
self.assertEqual(b.split(), [b])
731729
b = self.type2test(b"\x09\x0A\x0B\x0C\x0D\x1C\x1D\x1E\x1F")
732730
self.assertEqual(b.split(), [b'\x1c\x1d\x1e\x1f'])
733731

734-
# TODO: RUSTPYTHON
735-
@unittest.expectedFailure
736732
def test_rsplit_unicodewhitespace(self):
737733
b = self.type2test(b"\x09\x0A\x0B\x0C\x0D\x1C\x1D\x1E\x1F")
738734
self.assertEqual(b.rsplit(), [b'\x1c\x1d\x1e\x1f'])

vm/src/obj/objbytearray.rs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ impl PyByteArray {
6969
}
7070

7171
impl From<Vec<u8>> for PyByteArray {
72-
fn from(elements: Vec<u8>) -> PyByteArray {
73-
PyByteArray::new(elements)
72+
fn from(elements: Vec<u8>) -> Self {
73+
Self::new(elements)
7474
}
7575
}
7676

@@ -389,24 +389,14 @@ impl PyByteArray {
389389

390390
#[pymethod(name = "split")]
391391
fn split(&self, options: ByteInnerSplitOptions, vm: &VirtualMachine) -> PyResult {
392-
let as_bytes = self
393-
.borrow_value()
394-
.split(options, false)?
395-
.iter()
396-
.map(|x| vm.ctx.new_bytearray(x.to_vec()))
397-
.collect::<Vec<PyObjectRef>>();
398-
Ok(vm.ctx.new_list(as_bytes))
392+
self.borrow_value()
393+
.split(options, |s, vm| vm.ctx.new_bytearray(s.to_vec()), vm)
399394
}
400395

401396
#[pymethod(name = "rsplit")]
402397
fn rsplit(&self, options: ByteInnerSplitOptions, vm: &VirtualMachine) -> PyResult {
403-
let as_bytes = self
404-
.borrow_value()
405-
.split(options, true)?
406-
.iter()
407-
.map(|x| vm.ctx.new_bytearray(x.to_vec()))
408-
.collect::<Vec<PyObjectRef>>();
409-
Ok(vm.ctx.new_list(as_bytes))
398+
self.borrow_value()
399+
.rsplit(options, |s, vm| vm.ctx.new_bytearray(s.to_vec()), vm)
410400
}
411401

412402
#[pymethod(name = "partition")]

0 commit comments

Comments
 (0)