stream: Factor out mp_stream_write() method to write a memstring to stream.
This commit is contained in:
parent
122c9db3db
commit
ac736f15c9
15
py/stream.c
15
py/stream.c
@ -98,18 +98,15 @@ STATIC mp_obj_t stream_read(uint n_args, const mp_obj_t *args) {
|
||||
}
|
||||
}
|
||||
|
||||
STATIC mp_obj_t stream_write(mp_obj_t self_in, mp_obj_t arg) {
|
||||
mp_obj_t mp_stream_write(mp_obj_t self_in, const void *buf, mp_uint_t len) {
|
||||
struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)self_in;
|
||||
if (o->type->stream_p == NULL || o->type->stream_p->write == NULL) {
|
||||
// CPython: io.UnsupportedOperation, OSError subclass
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Operation not supported"));
|
||||
}
|
||||
|
||||
mp_buffer_info_t bufinfo;
|
||||
mp_get_buffer_raise(arg, &bufinfo, MP_BUFFER_READ);
|
||||
|
||||
int error;
|
||||
mp_int_t out_sz = o->type->stream_p->write(self_in, bufinfo.buf, bufinfo.len, &error);
|
||||
mp_int_t out_sz = o->type->stream_p->write(self_in, buf, len, &error);
|
||||
if (out_sz == -1) {
|
||||
if (is_nonblocking_error(error)) {
|
||||
// http://docs.python.org/3/library/io.html#io.RawIOBase.write
|
||||
@ -125,6 +122,12 @@ STATIC mp_obj_t stream_write(mp_obj_t self_in, mp_obj_t arg) {
|
||||
}
|
||||
}
|
||||
|
||||
STATIC mp_obj_t stream_write_method(mp_obj_t self_in, mp_obj_t arg) {
|
||||
mp_buffer_info_t bufinfo;
|
||||
mp_get_buffer_raise(arg, &bufinfo, MP_BUFFER_READ);
|
||||
return mp_stream_write(self_in, bufinfo.buf, bufinfo.len);
|
||||
}
|
||||
|
||||
STATIC mp_obj_t stream_readall(mp_obj_t self_in) {
|
||||
struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)self_in;
|
||||
if (o->type->stream_p == NULL || o->type->stream_p->read == NULL) {
|
||||
@ -248,4 +251,4 @@ mp_obj_t mp_stream_unbuffered_iter(mp_obj_t self) {
|
||||
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_read_obj, 1, 2, stream_read);
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_readall_obj, stream_readall);
|
||||
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_unbuffered_readline_obj, 1, 2, stream_unbuffered_readline);
|
||||
MP_DEFINE_CONST_FUN_OBJ_2(mp_stream_write_obj, stream_write);
|
||||
MP_DEFINE_CONST_FUN_OBJ_2(mp_stream_write_obj, stream_write_method);
|
||||
|
@ -32,3 +32,5 @@ MP_DECLARE_CONST_FUN_OBJ(mp_stream_write_obj);
|
||||
|
||||
// Iterator which uses mp_stream_unbuffered_readline_obj
|
||||
mp_obj_t mp_stream_unbuffered_iter(mp_obj_t self);
|
||||
|
||||
mp_obj_t mp_stream_write(mp_obj_t self_in, const void *buf, mp_uint_t len);
|
||||
|
Loading…
Reference in New Issue
Block a user