Skip to content

Commit 5c43cab

Browse files
committed
Fix str.join with str subclass
1 parent 4438350 commit 5c43cab

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

Lib/test/string_tests.py

-2
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

+17-6
Original file line numberDiff line numberDiff line change
@@ -860,13 +860,24 @@ impl PyStr {
860860
}
861861

862862
#[pymethod]
863-
fn join(&self, iterable: ArgIterable<PyStrRef>, vm: &VirtualMachine) -> PyResult<PyStrRef> {
863+
fn join(
864+
zelf: PyRef<Self>,
865+
iterable: ArgIterable<PyStrRef>,
866+
vm: &VirtualMachine,
867+
) -> PyResult<PyStrRef> {
864868
let iter = iterable.iter(vm)?;
865-
866-
match iter.exactly_one() {
867-
Ok(first) => first,
868-
Err(iter) => Ok(vm.ctx.new_str(self.as_str().py_join(iter)?)),
869-
}
869+
let joined = match iter.exactly_one() {
870+
Ok(first) => {
871+
let first = first?;
872+
if first.as_object().class().is(vm.ctx.types.str_type) {
873+
return Ok(first);
874+
} else {
875+
first.as_str().to_owned()
876+
}
877+
}
878+
Err(iter) => zelf.as_str().py_join(iter)?,
879+
};
880+
Ok(joined.into_pystr_ref(vm))
870881
}
871882

872883
// FIXME: two traversals of str is expensive

0 commit comments

Comments
 (0)