summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2014-12-05 23:13:52 +0000
committerDamien George <damien.p.george@gmail.com>2014-12-05 23:13:52 +0000
commitbe6d8be91e133e98117025062df0e63aaf87efd2 (patch)
tree692495154f547612c148312b4abc0afc3f4a50d6
parent451a0870753be89f5a284fd39727705a3ad2109b (diff)
downloadmicropython-be6d8be91e133e98117025062df0e63aaf87efd2.tar.gz
micropython-be6d8be91e133e98117025062df0e63aaf87efd2.zip
py: Rename mp_obj_int_get to mp_obj_int_get_truncated; fix struct.pack.
mp_obj_int_get_truncated is used as a "fast path" int accessor that doesn't check for overflow and returns the int truncated to the machine word size, ie mp_int_t. Use mp_obj_int_get_truncated to fix struct.pack when packing maximum word sized values. Addresses issues #779 and #998.
-rw-r--r--extmod/moductypes.c4
-rw-r--r--extmod/modure.c4
-rw-r--r--py/binary.c7
-rw-r--r--py/obj.c2
-rw-r--r--py/obj.h6
-rw-r--r--py/objint.c2
-rw-r--r--py/objint_longlong.c4
-rw-r--r--py/objint_mpz.c4
-rw-r--r--py/stream.c2
-rw-r--r--stmhal/modusocket.c2
-rw-r--r--tests/basics/struct1.py12
-rw-r--r--unix/modffi.c6
-rw-r--r--unix/modsocket.c2
13 files changed, 37 insertions, 20 deletions
diff --git a/extmod/moductypes.c b/extmod/moductypes.c
index 83cfbbcc57..b8f68bf8c1 100644
--- a/extmod/moductypes.c
+++ b/extmod/moductypes.c
@@ -571,7 +571,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(uctypes_struct_addressof_obj, uctypes_struct_addressof
/// captured by reference (and thus memory pointed by bytearray may change
/// or become invalid at later time). Use bytes_at() to capture by value.
mp_obj_t uctypes_struct_bytearray_at(mp_obj_t ptr, mp_obj_t size) {
- return mp_obj_new_bytearray_by_ref(mp_obj_int_get(size), (void*)mp_obj_int_get(ptr));
+ return mp_obj_new_bytearray_by_ref(mp_obj_int_get_truncated(size), (void*)mp_obj_int_get_truncated(ptr));
}
MP_DEFINE_CONST_FUN_OBJ_2(uctypes_struct_bytearray_at_obj, uctypes_struct_bytearray_at);
@@ -580,7 +580,7 @@ MP_DEFINE_CONST_FUN_OBJ_2(uctypes_struct_bytearray_at_obj, uctypes_struct_bytear
/// captured by value, i.e. copied. Use bytearray_at() to capture by reference
/// ("zero copy").
mp_obj_t uctypes_struct_bytes_at(mp_obj_t ptr, mp_obj_t size) {
- return mp_obj_new_bytes((void*)mp_obj_int_get(ptr), mp_obj_int_get(size));
+ return mp_obj_new_bytes((void*)mp_obj_int_get_truncated(ptr), mp_obj_int_get_truncated(size));
}
MP_DEFINE_CONST_FUN_OBJ_2(uctypes_struct_bytes_at_obj, uctypes_struct_bytes_at);
diff --git a/extmod/modure.c b/extmod/modure.c
index eb010faf90..910166249c 100644
--- a/extmod/modure.c
+++ b/extmod/modure.c
@@ -62,7 +62,7 @@ STATIC void match_print(void (*print)(void *env, const char *fmt, ...), void *en
STATIC mp_obj_t match_group(mp_obj_t self_in, mp_obj_t no_in) {
mp_obj_match_t *self = self_in;
- mp_int_t no = mp_obj_int_get(no_in);
+ mp_int_t no = mp_obj_int_get_truncated(no_in);
if (no < 0 || no >= self->num_matches / 2) {
nlr_raise(mp_obj_new_exception_arg1(&mp_type_IndexError, no_in));
}
@@ -130,7 +130,7 @@ STATIC mp_obj_t re_split(uint n_args, const mp_obj_t *args) {
int maxsplit = 0;
if (n_args > 2) {
- maxsplit = mp_obj_int_get(args[2]);
+ maxsplit = mp_obj_int_get_truncated(args[2]);
}
mp_obj_t retval = mp_obj_new_list(0, NULL);
diff --git a/py/binary.c b/py/binary.c
index 46a4eb694f..e4dac31e96 100644
--- a/py/binary.c
+++ b/py/binary.c
@@ -252,7 +252,12 @@ void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte **
val = (mp_uint_t)val_in;
break;
default:
- val = mp_obj_get_int(val_in);
+ // we handle large ints here by calling the truncated accessor
+ if (MP_OBJ_IS_TYPE(val_in, &mp_type_int)) {
+ val = mp_obj_int_get_truncated(val_in);
+ } else {
+ val = mp_obj_get_int(val_in);
+ }
}
mp_binary_set_int(MIN(size, sizeof(val)), struct_type == '>', p, val);
diff --git a/py/obj.c b/py/obj.c
index e581495888..c869a65465 100644
--- a/py/obj.c
+++ b/py/obj.c
@@ -179,7 +179,7 @@ mp_int_t mp_obj_hash(mp_obj_t o_in) {
if (hash_method[0] != MP_OBJ_NULL) {
mp_obj_t hash_val = mp_call_method_n_kw(0, 0, hash_method);
if (MP_OBJ_IS_INT(hash_val)) {
- return mp_obj_int_get(hash_val);
+ return mp_obj_int_get_truncated(hash_val);
}
}
}
diff --git a/py/obj.h b/py/obj.h
index febff67c29..3721bea925 100644
--- a/py/obj.h
+++ b/py/obj.h
@@ -460,12 +460,12 @@ void mp_obj_cell_set(mp_obj_t self_in, mp_obj_t obj);
// int
// For long int, returns value truncated to mp_int_t
-mp_int_t mp_obj_int_get(mp_const_obj_t self_in);
+mp_int_t mp_obj_int_get_truncated(mp_const_obj_t self_in);
+// Will raise exception if value doesn't fit into mp_int_t
+mp_int_t mp_obj_int_get_checked(mp_const_obj_t self_in);
#if MICROPY_PY_BUILTINS_FLOAT
mp_float_t mp_obj_int_as_float(mp_obj_t self_in);
#endif
-// Will raise exception if value doesn't fit into mp_int_t
-mp_int_t mp_obj_int_get_checked(mp_const_obj_t self_in);
// exception
#define mp_obj_is_native_exception_instance(o) (mp_obj_get_type(o)->make_new == mp_obj_exception_make_new)
diff --git a/py/objint.c b/py/objint.c
index 8e048ddec3..7f0e2b2506 100644
--- a/py/objint.c
+++ b/py/objint.c
@@ -270,7 +270,7 @@ mp_obj_t mp_obj_new_int(mp_int_t value) {
return mp_const_none;
}
-mp_int_t mp_obj_int_get(mp_const_obj_t self_in) {
+mp_int_t mp_obj_int_get_truncated(mp_const_obj_t self_in) {
return MP_OBJ_SMALL_INT_VALUE(self_in);
}
diff --git a/py/objint_longlong.c b/py/objint_longlong.c
index 8d47308b0f..ec55c77849 100644
--- a/py/objint_longlong.c
+++ b/py/objint_longlong.c
@@ -198,7 +198,7 @@ mp_obj_t mp_obj_new_int_from_str_len(const char **str, mp_uint_t len, bool neg,
return o;
}
-mp_int_t mp_obj_int_get(mp_const_obj_t self_in) {
+mp_int_t mp_obj_int_get_truncated(mp_const_obj_t self_in) {
if (MP_OBJ_IS_SMALL_INT(self_in)) {
return MP_OBJ_SMALL_INT_VALUE(self_in);
} else {
@@ -209,7 +209,7 @@ mp_int_t mp_obj_int_get(mp_const_obj_t self_in) {
mp_int_t mp_obj_int_get_checked(mp_const_obj_t self_in) {
// TODO: Check overflow
- return mp_obj_int_get(self_in);
+ return mp_obj_int_get_truncated(self_in);
}
#if MICROPY_PY_BUILTINS_FLOAT
diff --git a/py/objint_mpz.c b/py/objint_mpz.c
index 7eff54c53c..5480bd385d 100644
--- a/py/objint_mpz.c
+++ b/py/objint_mpz.c
@@ -305,12 +305,12 @@ mp_obj_t mp_obj_new_int_from_str_len(const char **str, mp_uint_t len, bool neg,
return o;
}
-mp_int_t mp_obj_int_get(mp_const_obj_t self_in) {
+mp_int_t mp_obj_int_get_truncated(mp_const_obj_t self_in) {
if (MP_OBJ_IS_SMALL_INT(self_in)) {
return MP_OBJ_SMALL_INT_VALUE(self_in);
} else {
const mp_obj_int_t *self = self_in;
- // TODO this is a hack until we remove mp_obj_int_get function entirely
+ // hash returns actual int value if it fits in mp_int_t
return mpz_hash(&self->mpz);
}
}
diff --git a/py/stream.c b/py/stream.c
index 810e9b3498..db7e5657fd 100644
--- a/py/stream.c
+++ b/py/stream.c
@@ -229,7 +229,7 @@ STATIC mp_obj_t stream_readinto(mp_uint_t n_args, const mp_obj_t *args) {
// https://docs.python.org/3/library/socket.html#socket.socket.recv_into
mp_uint_t len = bufinfo.len;
if (n_args > 2) {
- len = mp_obj_int_get(args[2]);
+ len = mp_obj_int_get_truncated(args[2]);
if (len > bufinfo.len) {
len = bufinfo.len;
}
diff --git a/stmhal/modusocket.c b/stmhal/modusocket.c
index 1c0b036d34..d705636c1a 100644
--- a/stmhal/modusocket.c
+++ b/stmhal/modusocket.c
@@ -288,7 +288,7 @@ STATIC mp_obj_t socket_setsockopt(mp_uint_t n_args, const mp_obj_t *args) {
const void *optval;
mp_uint_t optlen;
if (mp_obj_is_integer(args[3])) {
- int val = mp_obj_int_get(args[3]);
+ mp_int_t val = mp_obj_int_get_truncated(args[3]);
optval = &val;
optlen = sizeof(val);
} else {
diff --git a/tests/basics/struct1.py b/tests/basics/struct1.py
index c3049c55d6..cebffc1425 100644
--- a/tests/basics/struct1.py
+++ b/tests/basics/struct1.py
@@ -25,3 +25,15 @@ print(struct.pack("<6sH", b"foo", 10000))
s = struct.pack("BHBI", 10, 100, 200, 300)
v = struct.unpack("BHBI", s)
print(v == (10, 100, 200, 300))
+
+# check maximum pack on 32-bit machine
+print(struct.pack("<I", 2**32 - 1))
+print(struct.pack("<I", 0xffffffff))
+
+# fails on 32-bit machine
+#print(struct.pack("<Q", 2**64 - 1))
+#print(struct.pack("<Q", 0xffffffffffffffff))
+
+# check maximum unpack
+print(struct.unpack("<I", b"\xff\xff\xff\xff"))
+print(struct.unpack("<Q", b"\xff\xff\xff\xff\xff\xff\xff\xff"))
diff --git a/unix/modffi.c b/unix/modffi.c
index 331219a14b..6f87789290 100644
--- a/unix/modffi.c
+++ b/unix/modffi.c
@@ -211,7 +211,7 @@ STATIC void call_py_func(ffi_cif *cif, void *ret, void** args, mp_obj_t func) {
}
mp_obj_t res = mp_call_function_n_kw(func, cif->nargs, 0, pyargs);
- *(ffi_arg*)ret = mp_obj_int_get(res);
+ *(ffi_arg*)ret = mp_obj_int_get_truncated(res);
}
STATIC mp_obj_t mod_ffi_callback(mp_obj_t rettype_in, mp_obj_t func_in, mp_obj_t paramtypes_in) {
@@ -313,7 +313,7 @@ mp_obj_t ffifunc_call(mp_obj_t self_in, mp_uint_t n_args, mp_uint_t n_kw, const
if (a == mp_const_none) {
values[i] = 0;
} else if (MP_OBJ_IS_INT(a)) {
- values[i] = mp_obj_int_get(a);
+ values[i] = mp_obj_int_get_truncated(a);
} else if (MP_OBJ_IS_STR(a)) {
const char *s = mp_obj_str_get_str(a);
values[i] = (ffi_arg)s;
@@ -422,7 +422,7 @@ mp_obj_t mod_ffi_open(mp_uint_t n_args, const mp_obj_t *args) {
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_ffi_open_obj, 1, 2, mod_ffi_open);
mp_obj_t mod_ffi_as_bytearray(mp_obj_t ptr, mp_obj_t size) {
- return mp_obj_new_bytearray_by_ref(mp_obj_int_get(size), (void*)mp_obj_int_get(ptr));
+ return mp_obj_new_bytearray_by_ref(mp_obj_int_get_truncated(size), (void*)mp_obj_int_get_truncated(ptr));
}
MP_DEFINE_CONST_FUN_OBJ_2(mod_ffi_as_bytearray_obj, mod_ffi_as_bytearray);
diff --git a/unix/modsocket.c b/unix/modsocket.c
index 749e58328e..f844d3b355 100644
--- a/unix/modsocket.c
+++ b/unix/modsocket.c
@@ -218,7 +218,7 @@ STATIC mp_obj_t socket_setsockopt(mp_uint_t n_args, const mp_obj_t *args) {
const void *optval;
socklen_t optlen;
if (MP_OBJ_IS_INT(args[3])) {
- int val = mp_obj_int_get(args[3]);
+ int val = mp_obj_int_get_truncated(args[3]);
optval = &val;
optlen = sizeof(val);
} else {