mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-02 19:39:49 +09:00
Compare commits
5 Commits
main
...
copilot/fi
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9a11d44a5c | ||
|
|
514053415c | ||
|
|
2ff66e7ed7 | ||
|
|
ed2fd1e16f | ||
|
|
8a56d78bda |
1
Lib/test/test_str.py
vendored
1
Lib/test/test_str.py
vendored
@@ -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):
|
||||
|
||||
@@ -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)?;
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user