Skip to content

Commit 3c4f04f

Browse files
authored
Merge pull request RustPython#4498 from youknowone/str-join
Fix str.join with str subclass
2 parents 1fd557d + 5c43cab commit 3c4f04f

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

Lib/test/string_tests.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1427,8 +1427,6 @@ def test_find_etc_raise_correct_error_messages(self):
14271427
class MixinStrUnicodeTest:
14281428
# Additional tests that only work with str.
14291429

1430-
# TODO: RUSTPYTHON
1431-
@unittest.expectedFailure
14321430
def test_bug1001011(self):
14331431
# Make sure join returns a NEW object for single item sequences
14341432
# involving a subclass.

vm/src/builtins/str.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -876,13 +876,24 @@ impl PyStr {
876876
}
877877

878878
#[pymethod]
879-
fn join(&self, iterable: ArgIterable<PyStrRef>, vm: &VirtualMachine) -> PyResult<PyStrRef> {
879+
fn join(
880+
zelf: PyRef<Self>,
881+
iterable: ArgIterable<PyStrRef>,
882+
vm: &VirtualMachine,
883+
) -> PyResult<PyStrRef> {
880884
let iter = iterable.iter(vm)?;
881-
882-
match iter.exactly_one() {
883-
Ok(first) => first,
884-
Err(iter) => Ok(vm.ctx.new_str(self.as_str().py_join(iter)?)),
885-
}
885+
let joined = match iter.exactly_one() {
886+
Ok(first) => {
887+
let first = first?;
888+
if first.as_object().class().is(vm.ctx.types.str_type) {
889+
return Ok(first);
890+
} else {
891+
first.as_str().to_owned()
892+
}
893+
}
894+
Err(iter) => zelf.as_str().py_join(iter)?,
895+
};
896+
Ok(joined.into_pystr_ref(vm))
886897
}
887898

888899
// FIXME: two traversals of str is expensive

0 commit comments

Comments
 (0)