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.
This commit is contained in:
Damien George 2014-12-05 23:13:52 +00:00
parent 451a087075
commit be6d8be91e
13 changed files with 37 additions and 20 deletions

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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);
}
}
}

View File

@ -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)

View File

@ -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);
}

View File

@ -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

View File

@ -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);
}
}

View File

@ -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;
}

View File

@ -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 {

View File

@ -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"))

View File

@ -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);

View File

@ -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 {