From 2831a8f8007fc04feb00d9f2970c8e54470ec8c2 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sun, 10 Aug 2014 21:20:40 +0300 Subject: [PATCH 1/3] modsocket: .recv() returns bytes object. --- unix/modsocket.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/unix/modsocket.c b/unix/modsocket.c index d5b8e11c24..7eae1d8bf9 100644 --- a/unix/modsocket.c +++ b/unix/modsocket.c @@ -45,6 +45,7 @@ #include "obj.h" #include "objtuple.h" #include "objarray.h" +#include "objstr.h" #include "runtime.h" #include "stream.h" #include "builtin.h" @@ -179,11 +180,11 @@ STATIC mp_obj_t socket_recv(uint n_args, const mp_obj_t *args) { flags = MP_OBJ_SMALL_INT_VALUE(args[2]); } - char *buf = m_new(char, sz); + byte *buf = m_new(byte, sz); int out_sz = recv(self->fd, buf, sz, flags); RAISE_ERRNO(out_sz, errno); - mp_obj_t ret = MP_OBJ_NEW_QSTR(qstr_from_strn(buf, out_sz)); + mp_obj_t ret = mp_obj_new_str_of_type(&mp_type_bytes, buf, out_sz); m_del(char, buf, sz); return ret; } From ecca53bd34d946f1015d2e8f4474bcd6a950cbee Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sun, 10 Aug 2014 20:16:39 +0300 Subject: [PATCH 2/3] py: binary.c: Properly implement alignment for native unpacked structs. --- py/binary.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/py/binary.c b/py/binary.c index 3f1d7f2867..65688272aa 100644 --- a/py/binary.c +++ b/py/binary.c @@ -26,6 +26,7 @@ #include #include +#include #include #include @@ -37,6 +38,10 @@ // Helpers to work with binary-encoded data +#ifndef alignof +#define alignof(type) offsetof(struct { char c; type t; }, t) +#endif + int mp_binary_get_size(char struct_type, char val_type, uint *palign) { int size = 0; int align = 1; @@ -68,16 +73,20 @@ int mp_binary_get_size(char struct_type, char val_type, uint *palign) { case 'b': case 'B': align = size = 1; break; case 'h': case 'H': - align = size = sizeof(short); break; + align = alignof(short); + size = sizeof(short); break; case 'i': case 'I': - align = size = sizeof(int); break; + align = alignof(int); + size = sizeof(int); break; case 'l': case 'L': - align = size = sizeof(long); break; + align = alignof(long); + size = sizeof(long); break; case 'q': case 'Q': - // TODO: This is for x86 - align = sizeof(int); size = sizeof(long long); break; + align = alignof(long long); + size = sizeof(long long); break; case 'P': case 'O': case 'S': - align = size = sizeof(void*); break; + align = alignof(void*); + size = sizeof(void*); break; } } } From 0c5498540b2005e39422647d2ca9cad1a4ff731b Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sun, 10 Aug 2014 23:14:35 +0300 Subject: [PATCH 3/3] objstr: split(): check arg type consistency (str vs bytes). Similar to other methods and following CPython3 strictness. --- py/objstr.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/py/objstr.c b/py/objstr.c index 6ec997f4bf..9d34609882 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -472,6 +472,9 @@ STATIC mp_obj_t str_split(uint n_args, const mp_obj_t *args) { } else { // sep given + if (mp_obj_get_type(sep) != self_type) { + arg_type_mixup(); + } uint sep_len; const char *sep_str = mp_obj_str_get_data(sep, &sep_len);