44 * The MIT License (MIT)
55 *
66 * Copyright (c) 2013, 2014 Damien P. George
7+ * Copyright (c) 2014 Paul Sokolovsky
78 *
89 * Permission is hereby granted, free of charge, to any person obtaining a copy
910 * of this software and associated documentation files (the "Software"), to deal
3435#include "obj.h"
3536#include "runtime.h"
3637#include "stream.h"
38+ #include "objstr.h"
3739
3840#if MICROPY_ENABLE_MOD_IO
3941
@@ -46,7 +48,7 @@ typedef struct _mp_obj_stringio_t {
4648
4749STATIC void stringio_print (void (* print )(void * env , const char * fmt , ...), void * env , mp_obj_t self_in , mp_print_kind_t kind ) {
4850 mp_obj_stringio_t * self = self_in ;
49- print (env , "<io.StringIO 0x%x>" , self -> vstr );
51+ print (env , self -> base . type == & mp_type_stringio ? "<io.StringIO 0x%x>" : "<io.BytesIO 0x%x>" , self -> vstr );
5052}
5153
5254STATIC machine_int_t stringio_read (mp_obj_t o_in , void * buf , machine_uint_t size , int * errcode ) {
@@ -77,9 +79,11 @@ STATIC machine_int_t stringio_write(mp_obj_t o_in, const void *buf, machine_uint
7779 return size ;
7880}
7981
82+ #define STREAM_TO_CONTENT_TYPE (o ) (((o)->base.type == &mp_type_stringio) ? &mp_type_str : &mp_type_bytes)
83+
8084STATIC mp_obj_t stringio_getvalue (mp_obj_t self_in ) {
8185 mp_obj_stringio_t * self = self_in ;
82- return mp_obj_new_str (( byte * )self -> vstr -> buf , self -> vstr -> len , false );
86+ return str_new ( STREAM_TO_CONTENT_TYPE ( self ), ( byte * )self -> vstr -> buf , self -> vstr -> len );
8387}
8488STATIC MP_DEFINE_CONST_FUN_OBJ_1 (stringio_getvalue_obj , stringio_getvalue );
8589
@@ -96,16 +100,16 @@ mp_obj_t stringio___exit__(uint n_args, const mp_obj_t *args) {
96100}
97101STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (stringio___exit___obj , 4 , 4 , stringio___exit__ );
98102
99- STATIC mp_obj_stringio_t * stringio_new () {
103+ STATIC mp_obj_stringio_t * stringio_new (mp_obj_t type_in ) {
100104 mp_obj_stringio_t * o = m_new_obj (mp_obj_stringio_t );
101- o -> base .type = & mp_type_stringio ;
105+ o -> base .type = type_in ;
102106 o -> vstr = vstr_new ();
103107 o -> pos = 0 ;
104108 return o ;
105109}
106110
107111STATIC mp_obj_t stringio_make_new (mp_obj_t type_in , uint n_args , uint n_kw , const mp_obj_t * args ) {
108- mp_obj_stringio_t * o = stringio_new ();
112+ mp_obj_stringio_t * o = stringio_new (type_in );
109113
110114 if (n_args > 0 ) {
111115 mp_buffer_info_t bufinfo ;
@@ -135,6 +139,12 @@ STATIC const mp_stream_p_t stringio_stream_p = {
135139 .write = stringio_write ,
136140};
137141
142+ STATIC const mp_stream_p_t bytesio_stream_p = {
143+ .read = stringio_read ,
144+ .write = stringio_write ,
145+ .is_bytes = true,
146+ };
147+
138148const mp_obj_type_t mp_type_stringio = {
139149 { & mp_type_type },
140150 .name = MP_QSTR_StringIO ,
@@ -146,4 +156,17 @@ const mp_obj_type_t mp_type_stringio = {
146156 .locals_dict = (mp_obj_t )& stringio_locals_dict ,
147157};
148158
159+ #if MICROPY_IO_BYTESIO
160+ const mp_obj_type_t mp_type_bytesio = {
161+ { & mp_type_type },
162+ .name = MP_QSTR_BytesIO ,
163+ .print = stringio_print ,
164+ .make_new = stringio_make_new ,
165+ .getiter = mp_identity ,
166+ .iternext = mp_stream_unbuffered_iter ,
167+ .stream_p = & bytesio_stream_p ,
168+ .locals_dict = (mp_obj_t )& stringio_locals_dict ,
169+ };
170+ #endif
171+
149172#endif
0 commit comments