Skip to content

Fix str.split bytes.split #1861

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions Lib/test/string_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,6 @@ def test_expandtabs(self):
self.checkraises(OverflowError,
'\ta\n\tb', 'expandtabs', sys.maxsize)

@unittest.skip("TODO: RUSTPYTHON test_bytes")
def test_split(self):
# by a char
self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|')
Expand Down Expand Up @@ -431,7 +430,6 @@ def test_split(self):
self.checkraises(ValueError, 'hello', 'split', '')
self.checkraises(ValueError, 'hello', 'split', '', 0)

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

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

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_additional_split(self):
self.checkequal(['this', 'is', 'the', 'split', 'function'],
'this is the split function', 'split')
Expand Down Expand Up @@ -735,8 +731,6 @@ def test_additional_split(self):
self.checkequal(['arf', 'barf'], b, 'split', None)
self.checkequal(['arf', 'barf'], b, 'split', None, 2)

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_additional_rsplit(self):
self.checkequal(['this', 'is', 'the', 'rsplit', 'function'],
'this is the rsplit function', 'rsplit')
Expand Down
6 changes: 0 additions & 6 deletions Lib/test/test_bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -722,17 +722,13 @@ 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)

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

# TODO: RUSTPYTHON
@unittest.expectedFailure
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'])
Expand Down Expand Up @@ -1841,8 +1837,6 @@ class BytearrayPEP3137Test(unittest.TestCase):
def marshal(self, x):
return bytearray(x)

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_returns_new_copy(self):
val = self.marshal(b'1234')
# On immutable types these MAY return a reference to themselves
Expand Down
1 change: 1 addition & 0 deletions vm/src/obj/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ pub mod objtype;
pub mod objweakproxy;
pub mod objweakref;
pub mod objzip;
mod pystr;
22 changes: 6 additions & 16 deletions vm/src/obj/objbytearray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ impl PyByteArray {
}

impl From<Vec<u8>> for PyByteArray {
fn from(elements: Vec<u8>) -> PyByteArray {
PyByteArray::new(elements)
fn from(elements: Vec<u8>) -> Self {
Self::new(elements)
}
}

Expand Down Expand Up @@ -389,24 +389,14 @@ impl PyByteArray {

#[pymethod(name = "split")]
fn split(&self, options: ByteInnerSplitOptions, vm: &VirtualMachine) -> PyResult {
let as_bytes = self
.borrow_value()
.split(options, false)?
.iter()
.map(|x| vm.ctx.new_bytearray(x.to_vec()))
.collect::<Vec<PyObjectRef>>();
Ok(vm.ctx.new_list(as_bytes))
self.borrow_value()
.split(options, |s, vm| vm.ctx.new_bytearray(s.to_vec()), vm)
}

#[pymethod(name = "rsplit")]
fn rsplit(&self, options: ByteInnerSplitOptions, vm: &VirtualMachine) -> PyResult {
let as_bytes = self
.borrow_value()
.split(options, true)?
.iter()
.map(|x| vm.ctx.new_bytearray(x.to_vec()))
.collect::<Vec<PyObjectRef>>();
Ok(vm.ctx.new_list(as_bytes))
self.borrow_value()
.rsplit(options, |s, vm| vm.ctx.new_bytearray(s.to_vec()), vm)
}

#[pymethod(name = "partition")]
Expand Down
Loading