@@ -11,6 +11,7 @@ use num_traits::ToPrimitive;
1111use super :: os;
1212use crate :: function:: { OptionalArg , OptionalOption , PyFuncArgs } ;
1313use crate :: obj:: objbytearray:: PyByteArray ;
14+ use crate :: obj:: objbyteinner:: PyBytesLike ;
1415use crate :: obj:: objbytes;
1516use crate :: obj:: objbytes:: PyBytes ;
1617use crate :: obj:: objint:: { self , PyIntRef } ;
@@ -81,6 +82,19 @@ impl BufferedIO {
8182
8283 Some ( buffer)
8384 }
85+
86+ fn tell ( & self ) -> u64 {
87+ self . cursor . position ( )
88+ }
89+
90+ fn readline ( & mut self ) -> Option < String > {
91+ let mut buf = String :: new ( ) ;
92+
93+ match self . cursor . read_line ( & mut buf) {
94+ Ok ( _) => Some ( buf) ,
95+ Err ( _) => None ,
96+ }
97+ }
8498}
8599
86100#[ derive( Debug ) ]
@@ -141,6 +155,17 @@ impl PyStringIORef {
141155 Err ( _) => Err ( vm. new_value_error ( "Error Retrieving Value" . to_string ( ) ) ) ,
142156 }
143157 }
158+
159+ fn tell ( self , _vm : & VirtualMachine ) -> u64 {
160+ self . buffer . borrow ( ) . tell ( )
161+ }
162+
163+ fn readline ( self , vm : & VirtualMachine ) -> PyResult < String > {
164+ match self . buffer . borrow_mut ( ) . readline ( ) {
165+ Some ( line) => Ok ( line) ,
166+ None => Err ( vm. new_value_error ( "Error Performing Operation" . to_string ( ) ) ) ,
167+ }
168+ }
144169}
145170
146171fn string_io_new (
@@ -173,11 +198,11 @@ impl PyValue for PyBytesIO {
173198}
174199
175200impl PyBytesIORef {
176- fn write ( self , data : objbytes :: PyBytesRef , vm : & VirtualMachine ) -> PyResult {
177- let bytes = data. get_value ( ) ;
201+ fn write ( self , data : PyBytesLike , vm : & VirtualMachine ) -> PyResult < u64 > {
202+ let bytes = data. to_cow ( ) ;
178203
179- match self . buffer . borrow_mut ( ) . write ( bytes) {
180- Some ( value) => Ok ( vm . ctx . new_int ( value) ) ,
204+ match self . buffer . borrow_mut ( ) . write ( & bytes) {
205+ Some ( value) => Ok ( value) ,
181206 None => Err ( vm. new_type_error ( "Error Writing Bytes" . to_string ( ) ) ) ,
182207 }
183208 }
@@ -207,6 +232,17 @@ impl PyBytesIORef {
207232 fn seekable ( self , _vm : & VirtualMachine ) -> bool {
208233 true
209234 }
235+
236+ fn tell ( self , _vm : & VirtualMachine ) -> u64 {
237+ self . buffer . borrow ( ) . tell ( )
238+ }
239+
240+ fn readline ( self , vm : & VirtualMachine ) -> PyResult < Vec < u8 > > {
241+ match self . buffer . borrow_mut ( ) . readline ( ) {
242+ Some ( line) => Ok ( line. as_bytes ( ) . to_vec ( ) ) ,
243+ None => Err ( vm. new_value_error ( "Error Performing Operation" . to_string ( ) ) ) ,
244+ }
245+ }
210246}
211247
212248fn bytes_io_new (
@@ -720,7 +756,9 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
720756 "seekable" => ctx. new_rustfunc( PyStringIORef :: seekable) ,
721757 "read" => ctx. new_rustfunc( PyStringIORef :: read) ,
722758 "write" => ctx. new_rustfunc( PyStringIORef :: write) ,
723- "getvalue" => ctx. new_rustfunc( PyStringIORef :: getvalue)
759+ "getvalue" => ctx. new_rustfunc( PyStringIORef :: getvalue) ,
760+ "tell" => ctx. new_rustfunc( PyStringIORef :: tell) ,
761+ "readline" => ctx. new_rustfunc( PyStringIORef :: readline) ,
724762 } ) ;
725763
726764 //BytesIO: in-memory bytes
@@ -731,7 +769,9 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
731769 "seek" => ctx. new_rustfunc( PyBytesIORef :: seek) ,
732770 "seekable" => ctx. new_rustfunc( PyBytesIORef :: seekable) ,
733771 "write" => ctx. new_rustfunc( PyBytesIORef :: write) ,
734- "getvalue" => ctx. new_rustfunc( PyBytesIORef :: getvalue)
772+ "getvalue" => ctx. new_rustfunc( PyBytesIORef :: getvalue) ,
773+ "tell" => ctx. new_rustfunc( PyBytesIORef :: tell) ,
774+ "readline" => ctx. new_rustfunc( PyBytesIORef :: readline) ,
735775 } ) ;
736776
737777 py_module ! ( vm, "_io" , {
0 commit comments