Compare commits

...

5 Commits

Author SHA1 Message Date
copilot-swe-agent[bot]
9a11d44a5c Align test_str with CPython and move local coverage
Co-authored-by: youknowone <69878+youknowone@users.noreply.github.com>
2026-03-29 04:33:46 +09:00
copilot-swe-agent[bot]
514053415c Tighten str subclass conversion regression
Co-authored-by: youknowone <69878+youknowone@users.noreply.github.com>
2026-03-29 04:33:46 +09:00
copilot-swe-agent[bot]
2ff66e7ed7 Format str constructor fast path
Co-authored-by: youknowone <69878+youknowone@users.noreply.github.com>
2026-03-29 04:33:46 +09:00
copilot-swe-agent[bot]
ed2fd1e16f Preserve str subclass returned by __repr__
Co-authored-by: youknowone <69878+youknowone@users.noreply.github.com>
2026-03-29 04:33:46 +09:00
copilot-swe-agent[bot]
8a56d78bda Initial plan 2026-03-29 04:33:46 +09:00
3 changed files with 30 additions and 7 deletions

View File

@@ -2414,7 +2414,6 @@ class StrTest(string_tests.StringLikeTest,
else:
self.fail("Should have raised UnicodeDecodeError")
@unittest.expectedFailure # TODO: RUSTPYTHON; AssertionError: <class 'str'> is not <class 'test.test_str.StrSubclass'>
def test_conversion(self):
# Make sure __str__() works properly
class StrWithStr(str):

View File

@@ -394,13 +394,10 @@ impl Constructor for PyStr {
type Args = StrArgs;
fn slot_new(cls: PyTypeRef, func_args: FuncArgs, vm: &VirtualMachine) -> PyResult {
// Optimization: return exact str as-is (only when no encoding/errors provided)
if cls.is(vm.ctx.types.str_type)
&& func_args.args.len() == 1
&& func_args.kwargs.is_empty()
&& func_args.args[0].class().is(vm.ctx.types.str_type)
// Optimization: for exact str, return PyObject_Str result as-is
if cls.is(vm.ctx.types.str_type) && func_args.args.len() == 1 && func_args.kwargs.is_empty()
{
return Ok(func_args.args[0].clone());
return func_args.args[0].str(vm).map(Into::into);
}
let args: Self::Args = func_args.bind(vm)?;

View File

@@ -19,6 +19,33 @@ assert type(str(y)) is str, "Str of a str-subtype should be a str."
assert y + " other" == "1 other"
assert y.x == "substr"
class ReprStrSubclass(str):
pass
class WithStr:
def __init__(self, value):
self.value = value
def __str__(self):
return self.value
class WithRepr:
def __init__(self, value):
self.value = value
def __repr__(self):
return self.value
str_value = ReprStrSubclass("abc")
assert str(WithStr(str_value)) is str_value
repr_value = ReprStrSubclass("<abc>")
assert str(WithRepr(repr_value)) is repr_value
## Base strings currently get an attribute dict, but shouldn't.
# with assert_raises(AttributeError):
# "hello".x = 5