@@ -785,6 +785,22 @@ fn text_io_wrapper_seekable(_self: PyObjectRef) -> bool {
785
785
true
786
786
}
787
787
788
+ fn text_io_wrapper_seek (
789
+ instance : PyObjectRef ,
790
+ offset : PyObjectRef ,
791
+ how : OptionalArg ,
792
+ vm : & VirtualMachine ,
793
+ ) -> PyResult {
794
+ let raw = vm. get_attribute ( instance, "buffer" ) ?;
795
+ let args: Vec < _ > = std:: iter:: once ( offset) . chain ( how. into_option ( ) ) . collect ( ) ;
796
+ vm. invoke ( & vm. get_attribute ( raw, "seek" ) ?, args)
797
+ }
798
+
799
+ fn text_io_wrapper_tell ( instance : PyObjectRef , vm : & VirtualMachine ) -> PyResult {
800
+ let raw = vm. get_attribute ( instance, "buffer" ) ?;
801
+ vm. invoke ( & vm. get_attribute ( raw, "tell" ) ?, vec ! [ ] )
802
+ }
803
+
788
804
fn text_io_wrapper_read (
789
805
instance : PyObjectRef ,
790
806
size : OptionalOption < PyObjectRef > ,
@@ -930,16 +946,21 @@ fn split_mode_string(mode_string: &str) -> Result<(String, String), String> {
930
946
Ok ( ( mode, typ. to_string ( ) ) )
931
947
}
932
948
933
- pub fn io_open ( vm : & VirtualMachine , args : PyFuncArgs ) -> PyResult {
934
- arg_check ! (
935
- vm,
936
- args,
937
- required = [ ( file, None ) ] ,
938
- optional = [ ( mode, Some ( vm. ctx. str_type( ) ) ) ]
939
- ) ;
949
+ fn io_open_wrapper (
950
+ file : PyObjectRef ,
951
+ mode : OptionalArg < PyStringRef > ,
952
+ vm : & VirtualMachine ,
953
+ ) -> PyResult {
954
+ io_open ( file, mode. as_ref ( ) . into_option ( ) . map ( |s| s. as_str ( ) ) , vm)
955
+ }
956
+ fn io_open_code ( file : PyObjectRef , vm : & VirtualMachine ) -> PyResult {
957
+ // TODO: lifecycle hooks or something?
958
+ io_open ( file, Some ( "rb" ) , vm)
959
+ }
940
960
961
+ pub fn io_open ( file : PyObjectRef , mode : Option < & str > , vm : & VirtualMachine ) -> PyResult {
941
962
// mode is optional: 'rt' is the default mode (open from reading text)
942
- let mode_string = mode. map_or ( "rt" , objstr :: borrow_value ) ;
963
+ let mode_string = mode. unwrap_or ( "rt" ) ;
943
964
944
965
let ( mode, typ) = match split_mode_string ( mode_string) {
945
966
Ok ( ( mode, typ) ) => ( mode, typ) ,
@@ -1061,6 +1082,8 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
1061
1082
let text_io_wrapper = py_class ! ( ctx, "TextIOWrapper" , text_io_base. clone( ) , {
1062
1083
"__init__" => ctx. new_method( text_io_wrapper_init) ,
1063
1084
"seekable" => ctx. new_method( text_io_wrapper_seekable) ,
1085
+ "seek" => ctx. new_method( text_io_wrapper_seek) ,
1086
+ "tell" => ctx. new_method( text_io_wrapper_tell) ,
1064
1087
"read" => ctx. new_method( text_io_wrapper_read) ,
1065
1088
"write" => ctx. new_method( text_io_wrapper_write) ,
1066
1089
"readline" => ctx. new_method( text_io_wrapper_readline) ,
@@ -1098,7 +1121,8 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
1098
1121
} ) ;
1099
1122
1100
1123
let module = py_module ! ( vm, "_io" , {
1101
- "open" => ctx. new_function( io_open) ,
1124
+ "open" => ctx. new_function( io_open_wrapper) ,
1125
+ "open_code" => ctx. new_function( io_open_code) ,
1102
1126
"_IOBase" => io_base,
1103
1127
"_RawIOBase" => raw_io_base. clone( ) ,
1104
1128
"_BufferedIOBase" => buffered_io_base,
0 commit comments