8000 streams: Make .write() support arbitrary objects with buffer interface. · lurch/micropython@45fb143 · GitHub
[go: up one dir, main page]

Skip to content

Commit 45fb143

Browse files
committed
streams: Make .write() support arbitrary objects with buffer interface.
This in particular fixes writing str vs bytes.
1 parent 1463c1f commit 45fb143

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

py/stream.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,11 @@ STATIC mp_obj_t stream_write(mp_obj_t self_in, mp_obj_t arg) {
4242
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Operation not supported"));
4343
}
4444

45-
uint sz;
46-
const char *buf = mp_obj_str_get_data(arg, &sz);
45+
mp_buffer_info_t bufinfo;
46+
mp_get_buffer_raise(arg, &bufinfo, MP_BUFFER_READ);
47+
4748
int error;
48-
machine_int_t out_sz = o->type->stream_p->write(self_in, buf, sz, &error);
49+
machine_int_t out_sz = o->type->stream_p->write(self_in, bufinfo.buf, bufinfo.len, &error);
4950
if (out_sz == -1) {
5051
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "[Errno %d]", error));
5152
} else {

0 commit comments

Comments
 (0)
0