@@ -3098,7 +3098,6 @@ mod _io {
30983098 }
30993099 }
31003100
3101- #[ pyclass( flags( BASETYPE , HAS_DICT ) , with( PyRef , Constructor ) ) ]
31023101 impl StringIO {
31033102 fn buffer ( & self , vm : & VirtualMachine ) -> PyResult < PyRwLockWriteGuard < ' _ , BufferedIO > > {
31043103 if !self . closed . load ( ) {
@@ -3107,7 +3106,10 @@ mod _io {
31073106 Err ( io_closed_error ( vm) )
31083107 }
31093108 }
3109+ }
31103110
3111+ #[ pyclass( flags( BASETYPE , HAS_DICT ) , with( Constructor ) ) ]
3112+ impl StringIO {
31113113 #[ pymethod]
31123114 fn readable ( & self ) -> bool {
31133115 true
@@ -3130,34 +3132,28 @@ mod _io {
31303132 fn close ( & self ) {
31313133 self . closed . store ( true ) ;
31323134 }
3133- }
31343135
3135- #[ pyclass]
3136- impl PyRef < StringIO > {
3137- //write string to underlying vector
3136+ // write string to underlying vector
31383137 #[ pymethod]
3139- fn write ( self , data : PyStrRef , vm : & VirtualMachine ) -> PyResult {
3138+ fn write ( & self , data : PyStrRef , vm : & VirtualMachine ) -> PyResult < u64 > {
31403139 let bytes = data. as_str ( ) . as_bytes ( ) ;
3141-
3142- match self . buffer ( vm) ?. write ( bytes) {
3143- Some ( value) => Ok ( vm. ctx . new_int ( value) . into ( ) ) ,
3144- None => Err ( vm. new_type_error ( "Error Writing String" . to_owned ( ) ) ) ,
3145- }
3140+ self . buffer ( vm) ?
3141+ . write ( bytes)
3142+ . ok_or_else ( || vm. new_type_error ( "Error Writing String" . to_owned ( ) ) )
31463143 }
31473144
3148- //return the entire contents of the underlying
3145+ // return the entire contents of the underlying
31493146 #[ pymethod]
3150- fn getvalue ( self , vm : & VirtualMachine ) -> PyResult {
3151- match String :: from_utf8 ( self . buffer ( vm) ?. getvalue ( ) ) {
3152- Ok ( result) => Ok ( vm. ctx . new_str ( result) . into ( ) ) ,
3153- Err ( _) => Err ( vm. new_value_error ( "Error Retrieving Value" . to_owned ( ) ) ) ,
3154- }
3147+ fn getvalue ( & self , vm : & VirtualMachine ) -> PyResult < String > {
3148+ let bytes = self . buffer ( vm) ?. getvalue ( ) ;
3149+ String :: from_utf8 ( bytes)
3150+ . map_err ( |_| vm. new_value_error ( "Error Retrieving Value" . to_owned ( ) ) )
31553151 }
31563152
3157- //skip to the jth position
3153+ // skip to the jth position
31583154 #[ pymethod]
31593155 fn seek (
3160- self ,
3156+ & self ,
31613157 offset : PyObjectRef ,
31623158 how : OptionalArg < i32 > ,
31633159 vm : & VirtualMachine ,
@@ -3167,38 +3163,34 @@ mod _io {
31673163 . map_err ( |err| os_err ( vm, err) )
31683164 }
31693165
3170- //Read k bytes from the object and return.
3171- //If k is undefined || k == -1, then we read all bytes until the end of the file.
3172- //This also increments the stream position by the value of k
3166+ // Read k bytes from the object and return.
3167+ // If k is undefined || k == -1, then we read all bytes until the end of the file.
3168+ // This also increments the stream position by the value of k
31733169 #[ pymethod]
3174- fn read ( self , size : OptionalSize , vm : & VirtualMachine ) -> PyResult {
3175- let data = match self . buffer ( vm) ?. read ( size. to_usize ( ) ) {
3176- Some ( value) => value,
3177- None => Vec :: new ( ) ,
3178- } ;
3170+ fn read ( & self , size : OptionalSize , vm : & VirtualMachine ) -> PyResult < String > {
3171+ let data = self . buffer ( vm) ?. read ( size. to_usize ( ) ) . unwrap_or_default ( ) ;
31793172
31803173 let value = String :: from_utf8 ( data)
31813174 . map_err ( |_| vm. new_value_error ( "Error Retrieving Value" . to_owned ( ) ) ) ?;
3182- Ok ( vm . ctx . new_str ( value) . into ( ) )
3175+ Ok ( value)
31833176 }
31843177
31853178 #[ pymethod]
3186- fn tell ( self , vm : & VirtualMachine ) -> PyResult < u64 > {
3179+ fn tell ( & self , vm : & VirtualMachine ) -> PyResult < u64 > {
31873180 Ok ( self . buffer ( vm) ?. tell ( ) )
31883181 }
31893182
31903183 #[ pymethod]
3191- fn readline ( self , size : OptionalSize , vm : & VirtualMachine ) -> PyResult < String > {
3184+ fn readline ( & self , size : OptionalSize , vm : & VirtualMachine ) -> PyResult < String > {
31923185 // TODO size should correspond to the number of characters, at the moments its the number of
31933186 // bytes.
3194- match String :: from_utf8 ( self . buffer ( vm) ?. readline ( size. to_usize ( ) , vm) ?) {
3195- Ok ( value) => Ok ( value) ,
3196- Err ( _) => Err ( vm. new_value_error ( "Error Retrieving Value" . to_owned ( ) ) ) ,
3197- }
3187+ let input = self . buffer ( vm) ?. readline ( size. to_usize ( ) , vm) ?;
3188+ String :: from_utf8 ( input)
3189+ . map_err ( |_| vm. new_value_error ( "Error Retrieving Value" . to_owned ( ) ) )
31983190 }
31993191
32003192 #[ pymethod]
3201- fn truncate ( self , pos : OptionalSize , vm : & VirtualMachine ) -> PyResult < usize > {
3193+ fn truncate ( & self , pos : OptionalSize , vm : & VirtualMachine ) -> PyResult < usize > {
32023194 let mut buffer = self . buffer ( vm) ?;
32033195 let pos = pos. try_usize ( vm) ?;
32043196 Ok ( buffer. truncate ( pos) )
@@ -3232,7 +3224,6 @@ mod _io {
32323224 }
32333225 }
32343226
3235- #[ pyclass( flags( BASETYPE , HAS_DICT ) , with( PyRef , Constructor ) ) ]
32363227 impl BytesIO {
32373228 fn buffer ( & self , vm : & VirtualMachine ) -> PyResult < PyRwLockWriteGuard < ' _ , BufferedIO > > {
32383229 if !self . closed . load ( ) {
@@ -3241,7 +3232,10 @@ mod _io {
32413232 Err ( io_closed_error ( vm) )
32423233 }
32433234 }
3235+ }
32443236
3237+ #[ pyclass( flags( BASETYPE , HAS_DICT ) , with( PyRef , Constructor ) ) ]
3238+ impl BytesIO {
32453239 #[ pymethod]
32463240 fn readable ( & self ) -> bool {
32473241 true
@@ -3254,37 +3248,33 @@ mod _io {
32543248 fn seekable ( & self ) -> bool {
32553249 true
32563250 }
3257- }
32583251
3259- #[ pyclass]
3260- impl PyRef < BytesIO > {
32613252 #[ pymethod]
3262- fn write ( self , data : ArgBytesLike , vm : & VirtualMachine ) -> PyResult < u64 > {
3253+ fn write ( & self , data : ArgBytesLike , vm : & VirtualMachine ) -> PyResult < u64 > {
32633254 let mut buffer = self . try_resizable ( vm) ?;
3264- match data. with_ref ( |b| buffer. write ( b) ) {
3265- Some ( value) => Ok ( value) ,
3266- None => Err ( vm. new_type_error ( "Error Writing Bytes" . to_owned ( ) ) ) ,
3267- }
3255+ data. with_ref ( |b| buffer. write ( b) )
3256+ . ok_or_else ( || vm. new_type_error ( "Error Writing Bytes" . to_owned ( ) ) )
32683257 }
32693258
3270- //Retrieves the entire bytes object value from the underlying buffer
3259+ // Retrieves the entire bytes object value from the underlying buffer
32713260 #[ pymethod]
3272- fn getvalue ( self , vm : & VirtualMachine ) -> PyResult < PyBytesRef > {
3273- Ok ( vm. ctx . new_bytes ( self . buffer ( vm) ?. getvalue ( ) ) )
3261+ fn getvalue ( & self , vm : & VirtualMachine ) -> PyResult < PyBytesRef > {
3262+ let bytes = self . buffer ( vm) ?. getvalue ( ) ;
3263+ Ok ( vm. ctx . new_bytes ( bytes) )
32743264 }
32753265
3276- //Takes an integer k (bytes) and returns them from the underlying buffer
3277- //If k is undefined || k == -1, then we read all bytes until the end of the file.
3278- //This also increments the stream position by the value of k
3266+ // Takes an integer k (bytes) and returns them from the underlying buffer
3267+ // If k is undefined || k == -1, then we read all bytes until the end of the file.
3268+ // This also increments the stream position by the value of k
32793269 #[ pymethod]
32803270 #[ pymethod( name = "read1" ) ]
3281- fn read ( self , size : OptionalSize , vm : & VirtualMachine ) -> PyResult < Vec < u8 > > {
3271+ fn read ( & self , size : OptionalSize , vm : & VirtualMachine ) -> PyResult < Vec < u8 > > {
32823272 let buf = self . buffer ( vm) ?. read ( size. to_usize ( ) ) . unwrap_or_default ( ) ;
32833273 Ok ( buf)
32843274 }
32853275
32863276 #[ pymethod]
3287- fn readinto ( self , obj : ArgMemoryBuffer , vm : & VirtualMachine ) -> PyResult < usize > {
3277+ fn readinto ( & self , obj : ArgMemoryBuffer , vm : & VirtualMachine ) -> PyResult < usize > {
32883278 let mut buf = self . buffer ( vm) ?;
32893279 let ret = buf
32903280 . cursor
@@ -3297,7 +3287,7 @@ mod _io {
32973287 //skip to the jth position
32983288 #[ pymethod]
32993289 fn seek (
3300- self ,
3290+ & self ,
33013291 offset : PyObjectRef ,
33023292 how : OptionalArg < i32 > ,
33033293 vm : & VirtualMachine ,
@@ -3308,22 +3298,17 @@ mod _io {
33083298 }
33093299
33103300 #[ pymethod]
3311- fn seekable ( self ) -> bool {
3312- true
3313- }
3314-
3315- #[ pymethod]
3316- fn tell ( self , vm : & VirtualMachine ) -> PyResult < u64 > {
3301+ fn tell ( & self , vm : & VirtualMachine ) -> PyResult < u64 > {
33173302 Ok ( self . buffer ( vm) ?. tell ( ) )
33183303 }
33193304
33203305 #[ pymethod]
3321- fn readline ( self , size : OptionalSize , vm : & VirtualMachine ) -> PyResult < Vec < u8 > > {
3306+ fn readline ( & self , size : OptionalSize , vm : & VirtualMachine ) -> PyResult < Vec < u8 > > {
33223307 self . buffer ( vm) ?. readline ( size. to_usize ( ) , vm)
33233308 }
33243309
33253310 #[ pymethod]
3326- fn truncate ( self , pos : OptionalSize , vm : & VirtualMachine ) -> PyResult < usize > {
3311+ fn truncate ( & self , pos : OptionalSize , vm : & VirtualMachine ) -> PyResult < usize > {
33273312 if self . closed . load ( ) {
33283313 return Err ( io_closed_error ( vm) ) ;
33293314 }
@@ -3333,17 +3318,20 @@ mod _io {
33333318 }
33343319
33353320 #[ pygetset]
3336- fn closed ( self ) -> bool {
3321+ fn closed ( & self ) -> bool {
33373322 self . closed . load ( )
33383323 }
33393324
33403325 #[ pymethod]
3341- fn close ( self , vm : & VirtualMachine ) -> PyResult < ( ) > {
3326+ fn close ( & self , vm : & VirtualMachine ) -> PyResult < ( ) > {
33423327 drop ( self . try_resizable ( vm) ?) ;
33433328 self . closed . store ( true ) ;
33443329 Ok ( ( ) )
33453330 }
3331+ }
33463332
3333+ #[ pyclass]
3334+ impl PyRef < BytesIO > {
33473335 #[ pymethod]
33483336 fn getbuffer ( self , vm : & VirtualMachine ) -> PyResult < PyMemoryView > {
33493337 let len = self . buffer . read ( ) . cursor . get_ref ( ) . len ( ) ;
0 commit comments