aboutsummaryrefslogtreecommitdiff
path: root/py
diff options
context:
space:
mode:
authorDamien George2014-12-05 23:13:52 +0000
committerDamien George2014-12-05 23:13:52 +0000
commitbe6d8be91e133e98117025062df0e63aaf87efd2 (patch)
tree692495154f547612c148312b4abc0afc3f4a50d6 /py
parent451a0870753be89f5a284fd39727705a3ad2109b (diff)
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.
Diffstat (limited to 'py')
-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
7 files changed, 16 insertions, 11 deletions
diff --git a/py/binary.c b/py/binary.c
index 46a4eb694..e4dac31e9 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 e58149588..c869a6546 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 febff67c2..3721bea92 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 8e048ddec..7f0e2b250 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 8d47308b0..ec55c7784 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 7eff54c53..5480bd385 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 810e9b349..db7e5657f 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;
}