From 561789d7182e8354761e4dc9f40ed7dd1a72686a Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sat, 24 May 2014 21:24:37 +0300 Subject: [PATCH] unix modsocket: Make .makefile() method more compliant. .makefile() should allow to specify which stream time to create - byte or text. --- py/builtin.h | 1 + unix/modsocket.c | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/py/builtin.h b/py/builtin.h index 2929c1018a..2e4d2023d0 100644 --- a/py/builtin.h +++ b/py/builtin.h @@ -25,6 +25,7 @@ */ mp_obj_t mp_builtin___import__(uint n_args, mp_obj_t *args); +mp_obj_t mp_builtin_open(uint n_args, const mp_obj_t *args); MP_DECLARE_CONST_FUN_OBJ(mp_builtin___build_class___obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin___import___obj); diff --git a/unix/modsocket.c b/unix/modsocket.c index 5192c02438..6484f563fb 100644 --- a/unix/modsocket.c +++ b/unix/modsocket.c @@ -37,6 +37,7 @@ #include #include #include +#include #include "mpconfig.h" #include "nlr.h" @@ -47,6 +48,7 @@ #include "objarray.h" #include "runtime.h" #include "stream.h" +#include "builtin.h" #define MICROPY_SOCKET_EXTRA (0) @@ -229,6 +231,18 @@ STATIC mp_obj_t socket_setblocking(mp_obj_t self_in, mp_obj_t flag_in) { } STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_setblocking_obj, socket_setblocking); +STATIC mp_obj_t socket_makefile(uint n_args, const mp_obj_t *args) { + // TODO: CPython explicitly says that closing returned object doesn't close + // the original socket (Python2 at all says that fd is dup()ed). But we + // save on the bloat. + mp_obj_socket_t *self = args[0]; + mp_obj_t *new_args = alloca(n_args * sizeof(mp_obj_t)); + memcpy(new_args + 1, args + 1, (n_args - 1) * sizeof(mp_obj_t)); + new_args[0] = MP_OBJ_NEW_SMALL_INT(self->fd); + return mp_builtin_open(n_args, new_args); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(socket_makefile_obj, 1, 3, socket_makefile); + STATIC mp_obj_t socket_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) { int family = AF_INET; int type = SOCK_STREAM; @@ -254,7 +268,7 @@ STATIC mp_obj_t socket_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const STATIC const mp_map_elem_t microsocket_locals_dict_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_fileno), (mp_obj_t)&socket_fileno_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_makefile), (mp_obj_t)&mp_identity_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_makefile), (mp_obj_t)&socket_makefile_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_read), (mp_obj_t)&mp_stream_read_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_readall), (mp_obj_t)&mp_stream_readall_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_readline), (mp_obj_t)&mp_stream_unbuffered_readline_obj},