summaryrefslogtreecommitdiffstatshomepage
path: root/unix/modsocket.c
diff options
context:
space:
mode:
authorPaul Sokolovsky <pfalcon@users.sourceforge.net>2014-05-24 21:24:37 +0300
committerPaul Sokolovsky <pfalcon@users.sourceforge.net>2014-05-24 21:24:37 +0300
commit561789d7182e8354761e4dc9f40ed7dd1a72686a (patch)
tree15ae3ce33b449c43a24383ca8981767c6bd28b77 /unix/modsocket.c
parent806ea1f6ca2b1b50bb4634be6c39ad83d8af7e89 (diff)
downloadmicropython-561789d7182e8354761e4dc9f40ed7dd1a72686a.tar.gz
micropython-561789d7182e8354761e4dc9f40ed7dd1a72686a.zip
unix modsocket: Make .makefile() method more compliant.
.makefile() should allow to specify which stream time to create - byte or text.
Diffstat (limited to 'unix/modsocket.c')
-rw-r--r--unix/modsocket.c16
1 files changed, 15 insertions, 1 deletions
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 <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
+#include <alloca.h>
#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},