Merge pull request #1857 from youknowone/str-split

Fix str.split to raise empty seprator error
This commit is contained in:
Jeong YunWon
2020-04-11 23:38:24 +09:00
committed by GitHub
2 changed files with 21 additions and 10 deletions

View File

@@ -386,8 +386,6 @@ class UnicodeTest(string_tests.CommonTest,
self.assertRaises(TypeError, 'hello'.translate)
self.assertRaises(TypeError, 'abababc'.translate, 'abc', 'xyz')
# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_split(self):
string_tests.CommonTest.test_split(self)
@@ -405,8 +403,6 @@ class UnicodeTest(string_tests.CommonTest,
self.checkequal([left, right],
left + delim * 2 + right, 'split', delim *2)
# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_rsplit(self):
string_tests.CommonTest.test_rsplit(self)
# test mixed kinds

View File

@@ -474,9 +474,9 @@ impl PyString {
}
#[pymethod]
fn split(&self, args: SplitArgs, vm: &VirtualMachine) -> PyObjectRef {
fn split(&self, args: SplitArgs, vm: &VirtualMachine) -> PyResult {
let value = &self.value;
let pattern = args.sep.as_ref().map(|s| s.as_str());
let pattern = args.non_empty_sep(vm)?;
let num_splits = args.maxsplit;
let elements: Vec<_> = match (pattern, num_splits.is_negative()) {
(Some(pattern), true) => value
@@ -500,13 +500,13 @@ impl PyString {
.map(|o| vm.ctx.new_str(o.to_owned()))
.collect(),
};
vm.ctx.new_list(elements)
Ok(vm.ctx.new_list(elements))
}
#[pymethod]
fn rsplit(&self, args: SplitArgs, vm: &VirtualMachine) -> PyObjectRef {
fn rsplit(&self, args: SplitArgs, vm: &VirtualMachine) -> PyResult {
let value = &self.value;
let pattern = args.sep.as_ref().map(|s| s.as_str());
let pattern = args.non_empty_sep(vm)?;
let num_splits = args.maxsplit;
let mut elements: Vec<_> = match (pattern, num_splits.is_negative()) {
(Some(pattern), true) => value
@@ -533,7 +533,7 @@ impl PyString {
// Unlike Python rsplit, Rust rsplitn returns an iterator that
// starts from the end of the string.
elements.reverse();
vm.ctx.new_list(elements)
Ok(vm.ctx.new_list(elements))
}
#[pymethod]
@@ -1378,6 +1378,21 @@ struct SplitArgs {
maxsplit: isize,
}
impl SplitArgs {
fn non_empty_sep<'a>(&'a self, vm: &VirtualMachine) -> PyResult<Option<&'a str>> {
let sep = if let Some(s) = self.sep.as_ref() {
let sep = s.as_str();
if sep.is_empty() {
return Err(vm.new_value_error("empty separator".to_owned()));
}
Some(sep)
} else {
None
};
Ok(sep)
}
}
pub fn init(ctx: &PyContext) {
PyString::extend_class(ctx, &ctx.types.str_type);