@@ -58,7 +58,7 @@ STATIC mp_obj_t stream_readall(mp_obj_t self_in);
5858#define is_nonblocking_error (errno ) (0)
5959#endif
6060
61- #define STREAM_CONTENT_TYPE (stream ) (((stream)->is_bytes ) ? &mp_type_bytes : &mp_type_str )
61+ #define STREAM_CONTENT_TYPE (stream ) (((stream)->is_text ) ? &mp_type_str : &mp_type_bytes )
6262
6363STATIC mp_obj_t stream_read (uint n_args , const mp_obj_t * args ) {
6464 struct _mp_obj_base_t * o = (struct _mp_obj_base_t * )args [0 ];
@@ -76,7 +76,7 @@ STATIC mp_obj_t stream_read(uint n_args, const mp_obj_t *args) {
7676 }
7777
7878 #if MICROPY_PY_BUILTINS_STR_UNICODE
79- if (! o -> type -> stream_p -> is_bytes ) {
79+ if (o -> type -> stream_p -> is_text ) {
8080 // We need to read sz number of unicode characters. Because we don't have any
8181 // buffering, and because the stream API can only read bytes, we must read here
8282 // in units of bytes and must never over read. If we want sz chars, then reading
@@ -96,8 +96,8 @@ STATIC mp_obj_t stream_read(uint n_args, const mp_obj_t *args) {
9696 nlr_raise (mp_obj_new_exception_msg_varg (& mp_type_MemoryError , "out of memory" ));
9797 }
9898 int error ;
99- mp_int_t out_sz = o -> type -> stream_p -> read (o , p , more_bytes , & error );
100- if (out_sz == -1 ) {
99+ mp_uint_t out_sz = o -> type -> stream_p -> read (o , p , more_bytes , & error );
100+ if (out_sz == MP_STREAM_ERROR ) {
101101 vstr_cut_tail_bytes (& vstr , more_bytes );
102102 if (is_nonblocking_error (error )) {
103103 // With non-blocking streams, we read as much as we can.
@@ -113,7 +113,7 @@ STATIC mp_obj_t stream_read(uint n_args, const mp_obj_t *args) {
113113 nlr_raise (mp_obj_new_exception_msg_varg (& mp_type_OSError , "[Errno %d]" , error ));
114114 }
115115
116- if (( mp_uint_t ) out_sz < more_bytes ) {
116+ if (out_sz < more_bytes ) {
117117 // Finish reading.
118118 // TODO what if we have read only half a non-ASCII char?
119119 vstr_cut_tail_bytes (& vstr , more_bytes - out_sz );
@@ -168,8 +168,8 @@ STATIC mp_obj_t stream_read(uint n_args, const mp_obj_t *args) {
168168
169169 byte * buf = m_new (byte , sz );
170170 int error ;
171- mp_int_t out_sz = o -> type -> stream_p -> read (o , buf , sz , & error );
172- if (out_sz == -1 ) {
171+ mp_uint_t out_sz = o -> type -> stream_p -> read (o , buf , sz , & error );
172+ if (out_sz == MP_STREAM_ERROR ) {
173173 if (is_nonblocking_error (error )) {
174174 // https://docs.python.org/3.4/library/io.html#io.RawIOBase.read
175175 // "If the object is in non-blocking mode and no bytes are available,
@@ -194,8 +194,8 @@ mp_obj_t mp_stream_write(mp_obj_t self_in, const void *buf, mp_uint_t len) {
194194 }
195195
196196 int error ;
197- mp_int_t out_sz = o -> type -> stream_p -> write (self_in , buf , len , & error );
198- if (out_sz == -1 ) {
197+ mp_uint_t out_sz = o -> type -> stream_p -> write (self_in , buf , len , & error );
198+ if (out_sz == MP_STREAM_ERROR ) {
199199 if (is_nonblocking_error (error )) {
200200 // http://docs.python.org/3/library/io.html#io.RawIOBase.write
201201 // "None is returned if the raw stream is set not to block and
@@ -230,8 +230,8 @@ STATIC mp_obj_t stream_readall(mp_obj_t self_in) {
230230 int error ;
231231 int current_read = DEFAULT_BUFFER_SIZE ;
232232 while (true) {
233- mp_int_t out_sz = o -> type -> stream_p -> read (self_in , p , current_read , & error );
234- if (out_sz == -1 ) {
233+ mp_uint_t out_sz = o -> type -> stream_p -> read (self_in , p , current_read , & error );
234+ if (out_sz == MP_STREAM_ERROR ) {
235235 if (is_nonblocking_error (error )) {
236236 // With non-blocking streams, we read as much as we can.
237237 // If we read nothing, return None, just like read().
@@ -292,16 +292,15 @@ STATIC mp_obj_t stream_unbuffered_readline(uint n_args, const mp_obj_t *args) {
292292 nlr_raise (mp_obj_new_exception_msg_varg (& mp_type_MemoryError , "out of memory" ));
293293 }
294294
295- mp_int_t out_sz = o -> type -> stream_p -> read (o , p , 1 , & error );
296- if (out_sz == -1 ) {
295+ mp_uint_t out_sz = o -> type -> stream_p -> read (o , p , 1 , & error );
296+ if (out_sz == MP_STREAM_ERROR ) {
297297 nlr_raise (mp_obj_new_exception_msg_varg (& mp_type_OSError , "[Errno %d]" , error ));
298298 }
299299 if (out_sz == 0 ) {
300300 // Back out previously added byte
301- // TODO: This is a bit hacky, does it supported by vstr API contract?
302301 // Consider, what's better - read a char and get OutOfMemory (so read
303302 // char is lost), or allocate first as we do.
304- vstr_add_len (vstr , - 1 );
303+ vstr_cut_tail_bytes (vstr , 1 );
305304 break ;
306305 }
307306 if (* p == '\n' ) {
0 commit comments