File tree 1 file changed +20
-0
lines changed 1 file changed +20
-0
lines changed Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ pub fn init(context: &PyContext) {
14
14
str_type. set_attr ( "__mul__" , context. new_rustfunc ( str_mul) ) ;
15
15
str_type. set_attr ( "__new__" , context. new_rustfunc ( str_new) ) ;
16
16
str_type. set_attr ( "__str__" , context. new_rustfunc ( str_str) ) ;
17
+ str_type. set_attr ( "__repr__" , context. new_rustfunc ( str_repr) ) ;
17
18
}
18
19
19
20
pub fn get_value ( obj : & PyObjectRef ) -> String {
@@ -44,6 +45,25 @@ fn str_str(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
44
45
Ok ( s. clone ( ) )
45
46
}
46
47
48
+ fn str_repr ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
49
+ arg_check ! ( vm, args, required = [ ( s, Some ( vm. ctx. str_type( ) ) ) ] ) ;
50
+ let value = get_value ( s) ;
51
+ let mut formatted = String :: from ( "'" ) ;
52
+ for c in value. chars ( ) {
53
+ match c {
54
+ '\'' | '\\' => {
55
+ formatted. push ( '\\' ) ;
56
+ formatted. push ( c) ;
57
+ }
58
+ _ => {
59
+ formatted. push ( c) ;
60
+ }
61
+ }
62
+ }
63
+ formatted. push ( '\'' ) ;
64
+ Ok ( vm. ctx . new_str ( formatted) )
65
+ }
66
+
47
67
fn str_add ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
48
68
arg_check ! (
49
69
vm,
You can’t perform that action at this time.
0 commit comments