@@ -7,15 +7,15 @@ use unicode_segmentation::UnicodeSegmentation;
7
7
8
8
use crate :: format:: { FormatParseError , FormatPart , FormatString } ;
9
9
use crate :: pyobject:: {
10
- IntoPyObject , OptionalArg , PyContext , PyFuncArgs , PyIterable , PyObjectRef , PyRef , PyResult ,
11
- PyValue , TypeProtocol ,
10
+ IdProtocol , IntoPyObject , OptionalArg , PyContext , PyFuncArgs , PyIterable , PyObjectRef , PyRef ,
11
+ PyResult , PyValue , TryFromObject , TypeProtocol ,
12
12
} ;
13
13
use crate :: vm:: VirtualMachine ;
14
14
15
15
use super :: objint;
16
16
use super :: objsequence:: PySliceableSequence ;
17
17
use super :: objslice:: PySlice ;
18
- use super :: objtype;
18
+ use super :: objtype:: { self , PyClassRef } ;
19
19
20
20
#[ derive( Clone , Debug ) ]
21
21
pub struct PyString {
@@ -788,16 +788,21 @@ fn perform_format(
788
788
// TODO: should with following format
789
789
// class str(object='')
790
790
// class str(object=b'', encoding='utf-8', errors='strict')
791
- fn str_new ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
792
- if args. args . len ( ) == 1 {
793
- return Ok ( vm. new_str ( "" . to_string ( ) ) ) ;
794
- }
795
-
796
- if args. args . len ( ) > 2 {
797
- panic ! ( "str expects exactly one parameter" ) ;
791
+ fn str_new (
792
+ cls : PyClassRef ,
793
+ object : OptionalArg < PyObjectRef > ,
794
+ vm : & mut VirtualMachine ,
795
+ ) -> PyResult < PyStringRef > {
796
+ let string = match object {
797
+ OptionalArg :: Present ( ref input) => vm. to_str ( input) ?,
798
+ OptionalArg :: Missing => vm. new_str ( "" . to_string ( ) ) ,
798
799
} ;
799
-
800
- vm. to_str ( & args. args [ 1 ] )
800
+ if string. typ ( ) . is ( & cls) {
801
+ TryFromObject :: try_from_object ( vm, string)
802
+ } else {
803
+ let payload = string. payload :: < PyString > ( ) . unwrap ( ) ;
804
+ PyRef :: new_with_type ( vm, payload. clone ( ) , cls)
805
+ }
801
806
}
802
807
803
808
impl PySliceableSequence for String {
0 commit comments