Skip to content

Commit 6b848f5

Browse files
committed
Fix str.__str__
1 parent eee6e78 commit 6b848f5

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

vm/src/builtins/str.rs

+12-5
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ impl PyStr {
364364
#[pyclass(
365365
flags(BASETYPE),
366366
with(
367+
PyRef,
367368
AsMapping,
368369
AsNumber,
369370
AsSequence,
@@ -481,11 +482,6 @@ impl PyStr {
481482
Self::repeat(zelf, value.into(), vm)
482483
}
483484

484-
#[pymethod(magic)]
485-
fn str(zelf: PyRef<Self>) -> PyStrRef {
486-
zelf
487-
}
488-
489485
#[inline]
490486
pub(crate) fn repr(&self, vm: &VirtualMachine) -> PyResult<String> {
491487
rustpython_common::str::repr(self.as_str())
@@ -1270,6 +1266,17 @@ impl PyStr {
12701266
}
12711267
}
12721268

1269+
#[pyclass]
1270+
impl PyRef<PyStr> {
1271+
#[pymethod(magic)]
1272+
fn str(self, vm: &VirtualMachine) -> PyRefExact<PyStr> {
1273+
self.into_exact_or(&vm.ctx, |zelf| unsafe {
1274+
// Creating a copy with same kind is safe
1275+
PyStr::new_str_unchecked(zelf.bytes.to_vec(), zelf.kind.kind()).into_exact_ref(&vm.ctx)
1276+
})
1277+
}
1278+
}
1279+
12731280
impl PyStrRef {
12741281
pub fn concat_in_place(&mut self, other: &str, vm: &VirtualMachine) {
12751282
// TODO: call [A]Rc::get_mut on the str to try to mutate the data in place

0 commit comments

Comments
 (0)