Merge pull request #4498 from youknowone/str-join

Fix str.join with str subclass
This commit is contained in:
Jim Fasarakis-Hilliard
2023-02-15 19:07:11 +02:00
committed by GitHub
2 changed files with 17 additions and 8 deletions

View File

@@ -1427,8 +1427,6 @@ class MixinStrUnicodeUserStringTest:
class MixinStrUnicodeTest:
# Additional tests that only work with str.
# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_bug1001011(self):
# Make sure join returns a NEW object for single item sequences
# involving a subclass.

View File

@@ -876,13 +876,24 @@ impl PyStr {
}
#[pymethod]
fn join(&self, iterable: ArgIterable<PyStrRef>, vm: &VirtualMachine) -> PyResult<PyStrRef> {
fn join(
zelf: PyRef<Self>,
iterable: ArgIterable<PyStrRef>,
vm: &VirtualMachine,
) -> PyResult<PyStrRef> {
let iter = iterable.iter(vm)?;
match iter.exactly_one() {
Ok(first) => first,
Err(iter) => Ok(vm.ctx.new_str(self.as_str().py_join(iter)?)),
}
let joined = match iter.exactly_one() {
Ok(first) => {
let first = first?;
if first.as_object().class().is(vm.ctx.types.str_type) {
return Ok(first);
} else {
first.as_str().to_owned()
}
}
Err(iter) => zelf.as_str().py_join(iter)?,
};
Ok(joined.into_pystr_ref(vm))
}
// FIXME: two traversals of str is expensive