Skip to content

Commit 0852fa6

Browse files
Copilotyouknowone
andcommitted
Preserve str subclass returned by __repr__
Co-authored-by: youknowone <69878+youknowone@users.noreply.github.com>
1 parent a5b33a0 commit 0852fa6

File tree

2 files changed

+2
-4
lines changed

2 files changed

+2
-4
lines changed

Lib/test/test_str.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2415,7 +2415,6 @@ def test_ucs4(self):
24152415
else:
24162416
self.fail("Should have raised UnicodeDecodeError")
24172417

2418-
@unittest.expectedFailure # TODO: RUSTPYTHON; AssertionError: <class 'str'> is not <class 'test.test_str.StrSubclass'>
24192418
def test_conversion(self):
24202419
# Make sure __str__() works properly
24212420
class StrWithStr(str):

crates/vm/src/builtins/str.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -394,13 +394,12 @@ impl Constructor for PyStr {
394394
type Args = StrArgs;
395395

396396
fn slot_new(cls: PyTypeRef, func_args: FuncArgs, vm: &VirtualMachine) -> PyResult {
397-
// Optimization: return exact str as-is (only when no encoding/errors provided)
397+
// Optimization: for exact str, return PyObject_Str result as-is
398398
if cls.is(vm.ctx.types.str_type)
399399
&& func_args.args.len() == 1
400400
&& func_args.kwargs.is_empty()
401-
&& func_args.args[0].class().is(vm.ctx.types.str_type)
402401
{
403-
return Ok(func_args.args[0].clone());
402+
return func_args.args[0].str(vm).map(Into::into);
404403
}
405404

406405
let args: Self::Args = func_args.bind(vm)?;

0 commit comments

Comments
 (0)