summaryrefslogtreecommitdiffstatshomepage
path: root/py/stream.c
diff options
context:
space:
mode:
authorPaul Sokolovsky <pfalcon@users.sourceforge.net>2014-05-15 07:28:19 +0300
committerPaul Sokolovsky <pfalcon@users.sourceforge.net>2014-05-15 07:28:19 +0300
commita47b64ae2dd81e45da082c6337aaf5fcef4da75c (patch)
tree48912484589ae106bedbcd17f463fdc802cc8744 /py/stream.c
parent0c124c3123a6c7607375e2df0c49edef577a8b8d (diff)
downloadmicropython-a47b64ae2dd81e45da082c6337aaf5fcef4da75c.tar.gz
micropython-a47b64ae2dd81e45da082c6337aaf5fcef4da75c.zip
objstringio: Implement io.BytesIO.
Done in generalized manner, allowing any stream class to be specified as working with bytes.
Diffstat (limited to 'py/stream.c')
-rw-r--r--py/stream.c9
1 files changed, 6 insertions, 3 deletions
diff --git a/py/stream.c b/py/stream.c
index 599582b510..b1a64cdcf5 100644
--- a/py/stream.c
+++ b/py/stream.c
@@ -32,6 +32,7 @@
#include "misc.h"
#include "qstr.h"
#include "obj.h"
+#include "objstr.h"
#include "stream.h"
#if MICROPY_STREAMS_NON_BLOCK
#include <errno.h>
@@ -53,6 +54,8 @@ STATIC mp_obj_t stream_readall(mp_obj_t self_in);
#define is_nonblocking_error(errno) (0)
#endif
+#define STREAM_CONTENT_TYPE(stream) (((stream)->is_bytes) ? &mp_type_bytes : &mp_type_str)
+
STATIC mp_obj_t stream_read(uint n_args, const mp_obj_t *args) {
struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)args[0];
if (o->type->stream_p == NULL || o->type->stream_p->read == NULL) {
@@ -78,7 +81,7 @@ STATIC mp_obj_t stream_read(uint n_args, const mp_obj_t *args) {
}
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "[Errno %d]", error));
} else {
- mp_obj_t s = mp_obj_new_str(buf, out_sz, false); // will reallocate to use exact size
+ mp_obj_t s = str_new(STREAM_CONTENT_TYPE(o->type->stream_p), buf, out_sz); // will reallocate to use exact size
m_free(buf, sz);
return s;
}
@@ -155,7 +158,7 @@ STATIC mp_obj_t stream_readall(mp_obj_t self_in) {
}
}
- mp_obj_t s = mp_obj_new_str((byte*)vstr->buf, total_size, false);
+ mp_obj_t s = str_new(STREAM_CONTENT_TYPE(o->type->stream_p), (byte*)vstr->buf, total_size);
vstr_free(vstr);
return s;
}
@@ -204,7 +207,7 @@ STATIC mp_obj_t stream_unbuffered_readline(uint n_args, const mp_obj_t *args) {
}
}
// TODO need a string creation API that doesn't copy the given data
- mp_obj_t ret = mp_obj_new_str((byte*)vstr->buf, vstr->len, false);
+ mp_obj_t ret = str_new(STREAM_CONTENT_TYPE(o->type->stream_p), (byte*)vstr->buf, vstr->len);
vstr_free(vstr);
return ret;
}