2014-05-03 18:27:38 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the Micro Python project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, 2014 Damien P. George
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2014-07-24 09:21:37 -04:00
|
|
|
#include <stdint.h>
|
2013-12-17 13:27:24 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
2014-05-02 10:47:01 -04:00
|
|
|
#include "mpconfig.h"
|
2013-12-17 13:27:24 -05:00
|
|
|
#include "nlr.h"
|
|
|
|
#include "misc.h"
|
2014-01-21 16:40:13 -05:00
|
|
|
#include "qstr.h"
|
2013-12-21 13:17:45 -05:00
|
|
|
#include "obj.h"
|
2014-11-03 11:09:39 -05:00
|
|
|
#include "objtype.h"
|
2014-07-24 09:21:37 -04:00
|
|
|
#include "mpz.h"
|
|
|
|
#include "objint.h"
|
2013-12-21 13:17:45 -05:00
|
|
|
#include "runtime0.h"
|
2013-12-17 13:27:24 -05:00
|
|
|
#include "runtime.h"
|
2014-06-27 19:25:04 -04:00
|
|
|
#include "stackctrl.h"
|
2013-12-17 13:27:24 -05:00
|
|
|
|
2014-05-10 19:26:42 -04:00
|
|
|
mp_obj_type_t *mp_obj_get_type(mp_const_obj_t o_in) {
|
2014-01-08 06:47:55 -05:00
|
|
|
if (MP_OBJ_IS_SMALL_INT(o_in)) {
|
2014-03-29 09:43:38 -04:00
|
|
|
return (mp_obj_t)&mp_type_int;
|
2014-01-08 12:33:12 -05:00
|
|
|
} else if (MP_OBJ_IS_QSTR(o_in)) {
|
2014-03-29 09:43:38 -04:00
|
|
|
return (mp_obj_t)&mp_type_str;
|
2014-01-08 06:47:55 -05:00
|
|
|
} else {
|
2014-05-10 19:26:42 -04:00
|
|
|
const mp_obj_base_t *o = o_in;
|
2014-01-08 06:47:55 -05:00
|
|
|
return (mp_obj_t)o->type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-10 19:26:42 -04:00
|
|
|
const char *mp_obj_get_type_str(mp_const_obj_t o_in) {
|
2014-02-15 06:34:50 -05:00
|
|
|
return qstr_str(mp_obj_get_type(o_in)->name);
|
2013-12-17 13:27:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void printf_wrapper(void *env, const char *fmt, ...) {
|
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
|
|
|
vprintf(fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
2014-01-13 12:19:16 -05:00
|
|
|
void mp_obj_print_helper(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
|
2014-06-27 19:25:04 -04:00
|
|
|
// There can be data structures nested too deep, or just recursive
|
2014-06-30 19:13:42 -04:00
|
|
|
MP_STACK_CHECK();
|
2014-05-10 14:05:45 -04:00
|
|
|
#if !NDEBUG
|
|
|
|
if (o_in == NULL) {
|
|
|
|
print(env, "(nil)");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2014-01-22 09:35:10 -05:00
|
|
|
mp_obj_type_t *type = mp_obj_get_type(o_in);
|
|
|
|
if (type->print != NULL) {
|
|
|
|
type->print(print, env, o_in, kind);
|
2013-12-17 13:27:24 -05:00
|
|
|
} else {
|
2014-02-15 06:34:50 -05:00
|
|
|
print(env, "<%s>", qstr_str(type->name));
|
2013-12-17 13:27:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-13 12:19:16 -05:00
|
|
|
void mp_obj_print(mp_obj_t o_in, mp_print_kind_t kind) {
|
|
|
|
mp_obj_print_helper(printf_wrapper, NULL, o_in, kind);
|
2013-12-17 13:27:24 -05:00
|
|
|
}
|
|
|
|
|
2014-01-19 07:38:49 -05:00
|
|
|
// helper function to print an exception with traceback
|
|
|
|
void mp_obj_print_exception(mp_obj_t exc) {
|
2014-02-15 11:10:44 -05:00
|
|
|
if (mp_obj_is_exception_instance(exc)) {
|
2014-07-03 08:25:24 -04:00
|
|
|
mp_uint_t n, *values;
|
2014-01-19 07:38:49 -05:00
|
|
|
mp_obj_exception_get_traceback(exc, &n, &values);
|
|
|
|
if (n > 0) {
|
2014-03-29 20:08:36 -04:00
|
|
|
assert(n % 3 == 0);
|
2014-01-19 07:38:49 -05:00
|
|
|
printf("Traceback (most recent call last):\n");
|
|
|
|
for (int i = n - 3; i >= 0; i -= 3) {
|
2014-01-29 16:51:51 -05:00
|
|
|
#if MICROPY_ENABLE_SOURCE_LINE
|
2014-04-13 10:01:28 -04:00
|
|
|
printf(" File \"%s\", line %d", qstr_str(values[i]), (int)values[i + 1]);
|
2014-01-29 16:51:51 -05:00
|
|
|
#else
|
2014-04-13 10:01:28 -04:00
|
|
|
printf(" File \"%s\"", qstr_str(values[i]));
|
2014-01-29 16:51:51 -05:00
|
|
|
#endif
|
2014-04-13 10:01:28 -04:00
|
|
|
// the block name can be NULL if it's unknown
|
|
|
|
qstr block = values[i + 2];
|
|
|
|
if (block == MP_QSTR_NULL) {
|
|
|
|
printf("\n");
|
|
|
|
} else {
|
|
|
|
printf(", in %s\n", qstr_str(block));
|
|
|
|
}
|
2014-01-19 07:38:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-03-30 18:10:10 -04:00
|
|
|
mp_obj_print(exc, PRINT_EXC);
|
2014-01-19 07:38:49 -05:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2014-08-30 09:28:06 -04:00
|
|
|
bool mp_obj_is_true(mp_obj_t arg) {
|
2014-03-30 08:35:08 -04:00
|
|
|
if (arg == mp_const_false) {
|
|
|
|
return 0;
|
|
|
|
} else if (arg == mp_const_true) {
|
|
|
|
return 1;
|
|
|
|
} else if (arg == mp_const_none) {
|
|
|
|
return 0;
|
|
|
|
} else if (MP_OBJ_IS_SMALL_INT(arg)) {
|
|
|
|
if (MP_OBJ_SMALL_INT_VALUE(arg) == 0) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
mp_obj_type_t *type = mp_obj_get_type(arg);
|
|
|
|
if (type->unary_op != NULL) {
|
|
|
|
mp_obj_t result = type->unary_op(MP_UNARY_OP_BOOL, arg);
|
2014-05-21 14:42:43 -04:00
|
|
|
if (result != MP_OBJ_NULL) {
|
2014-03-30 08:35:08 -04:00
|
|
|
return result == mp_const_true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t len = mp_obj_len_maybe(arg);
|
|
|
|
if (len != MP_OBJ_NULL) {
|
|
|
|
// obj has a length, truth determined if len != 0
|
|
|
|
return len != MP_OBJ_NEW_SMALL_INT(0);
|
|
|
|
} else {
|
|
|
|
// any other obj is true per Python semantics
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-21 13:17:45 -05:00
|
|
|
bool mp_obj_is_callable(mp_obj_t o_in) {
|
2014-11-03 11:09:39 -05:00
|
|
|
mp_call_fun_t call = mp_obj_get_type(o_in)->call;
|
|
|
|
if (call != mp_obj_instance_call) {
|
|
|
|
return call != NULL;
|
|
|
|
}
|
|
|
|
return mp_obj_instance_is_callable(o_in);
|
2013-12-17 13:27:24 -05:00
|
|
|
}
|
|
|
|
|
2014-07-03 08:25:24 -04:00
|
|
|
mp_int_t mp_obj_hash(mp_obj_t o_in) {
|
2013-12-21 13:17:45 -05:00
|
|
|
if (o_in == mp_const_false) {
|
2013-12-17 13:27:24 -05:00
|
|
|
return 0; // needs to hash to same as the integer 0, since False==0
|
2013-12-21 13:17:45 -05:00
|
|
|
} else if (o_in == mp_const_true) {
|
2013-12-17 13:27:24 -05:00
|
|
|
return 1; // needs to hash to same as the integer 1, since True==1
|
2013-12-21 13:17:45 -05:00
|
|
|
} else if (MP_OBJ_IS_SMALL_INT(o_in)) {
|
|
|
|
return MP_OBJ_SMALL_INT_VALUE(o_in);
|
2014-07-24 09:21:37 -04:00
|
|
|
} else if (MP_OBJ_IS_TYPE(o_in, &mp_type_int)) {
|
|
|
|
return mp_obj_int_hash(o_in);
|
2014-04-12 22:41:00 -04:00
|
|
|
} else if (MP_OBJ_IS_STR(o_in) || MP_OBJ_IS_TYPE(o_in, &mp_type_bytes)) {
|
2014-01-22 09:35:10 -05:00
|
|
|
return mp_obj_str_get_hash(o_in);
|
2014-03-29 09:15:08 -04:00
|
|
|
} else if (MP_OBJ_IS_TYPE(o_in, &mp_type_NoneType)) {
|
2014-07-03 08:25:24 -04:00
|
|
|
return (mp_int_t)o_in;
|
2014-08-24 11:28:17 -04:00
|
|
|
} else if (MP_OBJ_IS_FUN(o_in)) {
|
2014-07-03 08:25:24 -04:00
|
|
|
return (mp_int_t)o_in;
|
2014-03-29 09:15:08 -04:00
|
|
|
} else if (MP_OBJ_IS_TYPE(o_in, &mp_type_tuple)) {
|
2014-03-20 15:20:59 -04:00
|
|
|
return mp_obj_tuple_hash(o_in);
|
2014-04-05 05:50:43 -04:00
|
|
|
} else if (MP_OBJ_IS_TYPE(o_in, &mp_type_type)) {
|
2014-07-03 08:25:24 -04:00
|
|
|
return (mp_int_t)o_in;
|
2014-11-12 08:57:34 -05:00
|
|
|
} else if (MP_OBJ_IS_OBJ(o_in)) {
|
|
|
|
// if a valid __hash__ method exists, use it
|
|
|
|
mp_obj_t hash_method[2];
|
|
|
|
mp_load_method_maybe(o_in, MP_QSTR___hash__, hash_method);
|
|
|
|
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)) {
|
2014-12-05 18:13:52 -05:00
|
|
|
return mp_obj_int_get_truncated(hash_val);
|
2014-11-12 08:57:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-03-20 15:20:59 -04:00
|
|
|
|
2014-11-12 08:57:34 -05:00
|
|
|
// TODO hash class and instances - in CPython by default user created classes' __hash__ resolves to their id
|
2014-03-20 15:20:59 -04:00
|
|
|
|
2014-11-12 08:57:34 -05:00
|
|
|
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
|
|
|
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "unhashable type"));
|
2013-12-17 13:27:24 -05:00
|
|
|
} else {
|
2014-11-12 08:57:34 -05:00
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
|
|
|
|
"unhashable type: '%s'", mp_obj_get_type_str(o_in)));
|
2013-12-17 13:27:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// this function implements the '==' operator (and so the inverse of '!=')
|
|
|
|
// from the python language reference:
|
|
|
|
// "The objects need not have the same type. If both are numbers, they are converted
|
|
|
|
// to a common type. Otherwise, the == and != operators always consider objects of
|
|
|
|
// different types to be unequal."
|
|
|
|
// note also that False==0 and True==1 are true expressions
|
2013-12-21 13:17:45 -05:00
|
|
|
bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2) {
|
2013-12-17 13:27:24 -05:00
|
|
|
if (o1 == o2) {
|
|
|
|
return true;
|
2014-04-11 06:52:06 -04:00
|
|
|
}
|
|
|
|
if (o1 == mp_const_none || o2 == mp_const_none) {
|
2014-03-30 18:16:42 -04:00
|
|
|
return false;
|
2014-04-11 06:52:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// fast path for small ints
|
|
|
|
if (MP_OBJ_IS_SMALL_INT(o1)) {
|
|
|
|
if (MP_OBJ_IS_SMALL_INT(o2)) {
|
|
|
|
// both SMALL_INT, and not equal if we get here
|
2013-12-17 13:27:24 -05:00
|
|
|
return false;
|
|
|
|
} else {
|
2014-04-11 06:52:06 -04:00
|
|
|
mp_obj_t temp = o2; o2 = o1; o1 = temp;
|
|
|
|
// o2 is now the SMALL_INT, o1 is not
|
2014-04-11 05:10:37 -04:00
|
|
|
// fall through to generic op
|
2013-12-17 13:27:24 -05:00
|
|
|
}
|
2014-04-11 06:52:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// fast path for strings
|
|
|
|
if (MP_OBJ_IS_STR(o1)) {
|
|
|
|
if (MP_OBJ_IS_STR(o2)) {
|
|
|
|
// both strings, use special function
|
|
|
|
return mp_obj_str_equal(o1, o2);
|
|
|
|
} else {
|
|
|
|
// a string is never equal to anything else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if (MP_OBJ_IS_STR(o2)) {
|
|
|
|
// o1 is not a string (else caught above), so the objects are not equal
|
|
|
|
return false;
|
2014-04-11 05:10:37 -04:00
|
|
|
}
|
2014-02-10 12:43:41 -05:00
|
|
|
|
2014-04-11 05:10:37 -04:00
|
|
|
// generic type, call binary_op(MP_BINARY_OP_EQUAL)
|
|
|
|
mp_obj_type_t *type = mp_obj_get_type(o1);
|
|
|
|
if (type->binary_op != NULL) {
|
|
|
|
mp_obj_t r = type->binary_op(MP_BINARY_OP_EQUAL, o1, o2);
|
2014-05-21 14:42:43 -04:00
|
|
|
if (r != MP_OBJ_NULL) {
|
2014-04-11 05:10:37 -04:00
|
|
|
return r == mp_const_true ? true : false;
|
|
|
|
}
|
2013-12-17 16:35:38 -05:00
|
|
|
}
|
2014-04-11 05:10:37 -04:00
|
|
|
|
2014-11-06 12:36:16 -05:00
|
|
|
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
|
|
|
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_NotImplementedError,
|
|
|
|
"equality for given types not yet implemented"));
|
|
|
|
} else {
|
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_NotImplementedError,
|
|
|
|
"equality for '%s' and '%s' types not yet implemented",
|
|
|
|
mp_obj_get_type_str(o1), mp_obj_get_type_str(o2)));
|
|
|
|
}
|
2013-12-17 16:35:38 -05:00
|
|
|
}
|
|
|
|
|
2014-07-03 08:25:24 -04:00
|
|
|
mp_int_t mp_obj_get_int(mp_const_obj_t arg) {
|
2014-04-05 10:46:47 -04:00
|
|
|
// This function essentially performs implicit type conversion to int
|
|
|
|
// Note that Python does NOT provide implicit type conversion from
|
|
|
|
// float to int in the core expression language, try some_list[1.0].
|
2013-12-21 13:17:45 -05:00
|
|
|
if (arg == mp_const_false) {
|
2013-12-17 13:27:24 -05:00
|
|
|
return 0;
|
2013-12-21 13:17:45 -05:00
|
|
|
} else if (arg == mp_const_true) {
|
2013-12-17 13:27:24 -05:00
|
|
|
return 1;
|
2013-12-21 13:17:45 -05:00
|
|
|
} else if (MP_OBJ_IS_SMALL_INT(arg)) {
|
|
|
|
return MP_OBJ_SMALL_INT_VALUE(arg);
|
2014-03-29 09:43:38 -04:00
|
|
|
} else if (MP_OBJ_IS_TYPE(arg, &mp_type_int)) {
|
2014-01-18 09:07:16 -05:00
|
|
|
return mp_obj_int_get_checked(arg);
|
2013-12-17 13:27:24 -05:00
|
|
|
} else {
|
2014-11-06 12:36:16 -05:00
|
|
|
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
|
|
|
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
|
|
|
|
"can't convert to int"));
|
|
|
|
} else {
|
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
|
|
|
|
"can't convert %s to int", mp_obj_get_type_str(arg)));
|
|
|
|
}
|
2013-12-17 13:27:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-03 07:00:54 -04:00
|
|
|
// returns false if arg is not of integral type
|
|
|
|
// returns true and sets *value if it is of integral type
|
2014-07-03 08:25:24 -04:00
|
|
|
// can throw OverflowError if arg is of integral type, but doesn't fit in a mp_int_t
|
|
|
|
bool mp_obj_get_int_maybe(mp_const_obj_t arg, mp_int_t *value) {
|
2014-04-03 07:00:54 -04:00
|
|
|
if (arg == mp_const_false) {
|
|
|
|
*value = 0;
|
|
|
|
} else if (arg == mp_const_true) {
|
|
|
|
*value = 1;
|
|
|
|
} else if (MP_OBJ_IS_SMALL_INT(arg)) {
|
|
|
|
*value = MP_OBJ_SMALL_INT_VALUE(arg);
|
|
|
|
} else if (MP_OBJ_IS_TYPE(arg, &mp_type_int)) {
|
|
|
|
*value = mp_obj_int_get_checked(arg);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-06-01 08:32:54 -04:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
2014-03-08 10:24:39 -05:00
|
|
|
mp_float_t mp_obj_get_float(mp_obj_t arg) {
|
2013-12-21 13:17:45 -05:00
|
|
|
if (arg == mp_const_false) {
|
2013-12-17 13:27:24 -05:00
|
|
|
return 0;
|
2013-12-21 13:17:45 -05:00
|
|
|
} else if (arg == mp_const_true) {
|
2013-12-17 13:27:24 -05:00
|
|
|
return 1;
|
2013-12-21 13:17:45 -05:00
|
|
|
} else if (MP_OBJ_IS_SMALL_INT(arg)) {
|
|
|
|
return MP_OBJ_SMALL_INT_VALUE(arg);
|
2014-03-29 09:43:38 -04:00
|
|
|
} else if (MP_OBJ_IS_TYPE(arg, &mp_type_int)) {
|
2014-03-22 16:54:01 -04:00
|
|
|
return mp_obj_int_as_float(arg);
|
2014-03-08 10:24:39 -05:00
|
|
|
} else if (MP_OBJ_IS_TYPE(arg, &mp_type_float)) {
|
2013-12-21 13:17:45 -05:00
|
|
|
return mp_obj_float_get(arg);
|
2013-12-17 13:27:24 -05:00
|
|
|
} else {
|
2014-11-06 12:36:16 -05:00
|
|
|
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
|
|
|
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
|
|
|
|
"can't convert to float"));
|
|
|
|
} else {
|
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
|
|
|
|
"can't convert %s to float", mp_obj_get_type_str(arg)));
|
|
|
|
}
|
2013-12-17 13:27:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-19 18:48:35 -04:00
|
|
|
#if MICROPY_PY_BUILTINS_COMPLEX
|
2013-12-21 13:17:45 -05:00
|
|
|
void mp_obj_get_complex(mp_obj_t arg, mp_float_t *real, mp_float_t *imag) {
|
|
|
|
if (arg == mp_const_false) {
|
2013-12-17 13:27:24 -05:00
|
|
|
*real = 0;
|
|
|
|
*imag = 0;
|
2013-12-21 13:17:45 -05:00
|
|
|
} else if (arg == mp_const_true) {
|
2013-12-17 13:27:24 -05:00
|
|
|
*real = 1;
|
|
|
|
*imag = 0;
|
2013-12-21 13:17:45 -05:00
|
|
|
} else if (MP_OBJ_IS_SMALL_INT(arg)) {
|
|
|
|
*real = MP_OBJ_SMALL_INT_VALUE(arg);
|
2013-12-17 13:27:24 -05:00
|
|
|
*imag = 0;
|
2014-03-29 13:28:20 -04:00
|
|
|
} else if (MP_OBJ_IS_TYPE(arg, &mp_type_int)) {
|
|
|
|
*real = mp_obj_int_as_float(arg);
|
|
|
|
*imag = 0;
|
2014-03-08 10:24:39 -05:00
|
|
|
} else if (MP_OBJ_IS_TYPE(arg, &mp_type_float)) {
|
2013-12-21 13:17:45 -05:00
|
|
|
*real = mp_obj_float_get(arg);
|
2013-12-17 13:27:24 -05:00
|
|
|
*imag = 0;
|
2014-03-08 10:24:39 -05:00
|
|
|
} else if (MP_OBJ_IS_TYPE(arg, &mp_type_complex)) {
|
2013-12-21 13:17:45 -05:00
|
|
|
mp_obj_complex_get(arg, real, imag);
|
2013-12-17 13:27:24 -05:00
|
|
|
} else {
|
2014-11-06 12:36:16 -05:00
|
|
|
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
|
|
|
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
|
|
|
|
"can't convert to complex"));
|
|
|
|
} else {
|
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
|
|
|
|
"can't convert %s to complex", mp_obj_get_type_str(arg)));
|
|
|
|
}
|
2013-12-17 13:27:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2014-06-19 18:48:35 -04:00
|
|
|
#endif
|
2013-12-17 13:27:24 -05:00
|
|
|
|
2014-08-30 09:04:14 -04:00
|
|
|
void mp_obj_get_array(mp_obj_t o, mp_uint_t *len, mp_obj_t **items) {
|
2014-03-29 09:15:08 -04:00
|
|
|
if (MP_OBJ_IS_TYPE(o, &mp_type_tuple)) {
|
2014-03-24 06:47:13 -04:00
|
|
|
mp_obj_tuple_get(o, len, items);
|
2014-03-29 09:43:38 -04:00
|
|
|
} else if (MP_OBJ_IS_TYPE(o, &mp_type_list)) {
|
2014-03-24 06:47:13 -04:00
|
|
|
mp_obj_list_get(o, len, items);
|
|
|
|
} else {
|
2014-11-06 12:36:16 -05:00
|
|
|
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
|
|
|
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
|
|
|
|
"expected tuple/list"));
|
|
|
|
} else {
|
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
|
|
|
|
"object '%s' is not a tuple or list", mp_obj_get_type_str(o)));
|
|
|
|
}
|
2014-03-24 06:47:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-30 09:04:14 -04:00
|
|
|
void mp_obj_get_array_fixed_n(mp_obj_t o, mp_uint_t len, mp_obj_t **items) {
|
2014-08-30 10:17:47 -04:00
|
|
|
mp_uint_t seq_len;
|
|
|
|
mp_obj_get_array(o, &seq_len, items);
|
|
|
|
if (seq_len != len) {
|
2014-11-06 12:36:16 -05:00
|
|
|
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
|
|
|
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
|
|
|
|
"tuple/list has wrong length"));
|
|
|
|
} else {
|
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
|
|
|
|
"requested length %d but object has length %d", len, seq_len));
|
|
|
|
}
|
2013-12-17 13:27:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-13 01:57:16 -04:00
|
|
|
// is_slice determines whether the index is a slice index
|
2014-08-30 09:04:14 -04:00
|
|
|
mp_uint_t mp_get_index(const mp_obj_type_t *type, mp_uint_t len, mp_obj_t index, bool is_slice) {
|
2014-07-03 08:25:24 -04:00
|
|
|
mp_int_t i;
|
2014-04-11 06:40:38 -04:00
|
|
|
if (MP_OBJ_IS_SMALL_INT(index)) {
|
|
|
|
i = MP_OBJ_SMALL_INT_VALUE(index);
|
|
|
|
} else if (!mp_obj_get_int_maybe(index, &i)) {
|
2014-11-06 12:36:16 -05:00
|
|
|
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
|
|
|
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
|
|
|
|
"indices must be integers"));
|
|
|
|
} else {
|
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
|
|
|
|
"%s indices must be integers, not %s",
|
|
|
|
qstr_str(type->name), mp_obj_get_type_str(index)));
|
|
|
|
}
|
2013-12-17 13:27:24 -05:00
|
|
|
}
|
2014-03-13 01:57:16 -04:00
|
|
|
|
|
|
|
if (i < 0) {
|
2014-03-13 03:29:15 -04:00
|
|
|
i += len;
|
2014-03-13 01:57:16 -04:00
|
|
|
}
|
|
|
|
if (is_slice) {
|
2014-03-13 03:29:15 -04:00
|
|
|
if (i < 0) {
|
|
|
|
i = 0;
|
|
|
|
} else if (i > len) {
|
|
|
|
i = len;
|
|
|
|
}
|
2014-03-13 01:57:16 -04:00
|
|
|
} else {
|
2014-03-13 03:29:15 -04:00
|
|
|
if (i < 0 || i >= len) {
|
2014-11-06 12:36:16 -05:00
|
|
|
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
|
|
|
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_IndexError, "index out of range"));
|
|
|
|
} else {
|
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_IndexError,
|
|
|
|
"%s index out of range", qstr_str(type->name)));
|
|
|
|
}
|
2014-03-13 03:29:15 -04:00
|
|
|
}
|
2014-03-13 01:57:16 -04:00
|
|
|
}
|
|
|
|
return i;
|
2013-12-17 13:27:24 -05:00
|
|
|
}
|
2014-01-10 06:25:03 -05:00
|
|
|
|
2014-08-22 16:48:30 -04:00
|
|
|
mp_obj_t mp_obj_id(mp_obj_t o_in) {
|
|
|
|
mp_int_t id = (mp_int_t)o_in;
|
|
|
|
if (!MP_OBJ_IS_OBJ(o_in)) {
|
|
|
|
return mp_obj_new_int(id);
|
|
|
|
} else if (id >= 0) {
|
|
|
|
// Many OSes and CPUs have affinity for putting "user" memories
|
|
|
|
// into low half of address space, and "system" into upper half.
|
|
|
|
// We're going to take advantage of that and return small int
|
|
|
|
// (signed) for such "user" addresses.
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(id);
|
|
|
|
} else {
|
|
|
|
// If that didn't work, well, let's return long int, just as
|
|
|
|
// a (big) positve value, so it will never clash with the range
|
|
|
|
// of small int returned in previous case.
|
|
|
|
return mp_obj_new_int_from_uint((mp_uint_t)id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-12 13:33:40 -04:00
|
|
|
// will raise a TypeError if object has no length
|
|
|
|
mp_obj_t mp_obj_len(mp_obj_t o_in) {
|
|
|
|
mp_obj_t len = mp_obj_len_maybe(o_in);
|
|
|
|
if (len == MP_OBJ_NULL) {
|
2014-11-06 12:36:16 -05:00
|
|
|
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
|
|
|
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
|
|
|
|
"object has no len"));
|
|
|
|
} else {
|
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
|
|
|
|
"object of type '%s' has no len()", mp_obj_get_type_str(o_in)));
|
|
|
|
}
|
2014-08-12 13:33:40 -04:00
|
|
|
} else {
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-11 14:22:29 -05:00
|
|
|
// may return MP_OBJ_NULL
|
2014-01-10 06:25:03 -05:00
|
|
|
mp_obj_t mp_obj_len_maybe(mp_obj_t o_in) {
|
2014-06-13 16:37:18 -04:00
|
|
|
if (
|
|
|
|
#if !MICROPY_PY_BUILTINS_STR_UNICODE
|
|
|
|
// It's simple - unicode is slow, non-unicode is fast
|
|
|
|
MP_OBJ_IS_STR(o_in) ||
|
|
|
|
#endif
|
|
|
|
MP_OBJ_IS_TYPE(o_in, &mp_type_bytes)) {
|
2014-07-31 05:49:14 -04:00
|
|
|
return MP_OBJ_NEW_SMALL_INT(mp_obj_str_get_len(o_in));
|
2014-01-10 06:25:03 -05:00
|
|
|
} else {
|
2014-01-29 21:37:19 -05:00
|
|
|
mp_obj_type_t *type = mp_obj_get_type(o_in);
|
|
|
|
if (type->unary_op != NULL) {
|
2014-05-21 14:42:43 -04:00
|
|
|
return type->unary_op(MP_UNARY_OP_LEN, o_in);
|
2014-01-30 05:05:33 -05:00
|
|
|
} else {
|
|
|
|
return MP_OBJ_NULL;
|
2014-01-29 21:37:19 -05:00
|
|
|
}
|
2014-01-10 06:25:03 -05:00
|
|
|
}
|
|
|
|
}
|
2014-01-20 11:37:30 -05:00
|
|
|
|
2014-04-17 17:10:53 -04:00
|
|
|
mp_obj_t mp_obj_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t value) {
|
|
|
|
mp_obj_type_t *type = mp_obj_get_type(base);
|
|
|
|
if (type->subscr != NULL) {
|
|
|
|
mp_obj_t ret = type->subscr(base, index, value);
|
2014-05-21 14:42:43 -04:00
|
|
|
if (ret != MP_OBJ_NULL) {
|
2014-04-17 17:10:53 -04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
// TODO: call base classes here?
|
|
|
|
}
|
|
|
|
if (value == MP_OBJ_NULL) {
|
2014-11-06 12:36:16 -05:00
|
|
|
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
|
|
|
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
|
|
|
|
"object does not support item deletion"));
|
|
|
|
} else {
|
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
|
|
|
|
"'%s' object does not support item deletion", mp_obj_get_type_str(base)));
|
|
|
|
}
|
2014-04-17 17:10:53 -04:00
|
|
|
} else if (value == MP_OBJ_SENTINEL) {
|
2014-11-06 12:36:16 -05:00
|
|
|
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
|
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
|
|
|
|
"object is not subscriptable"));
|
|
|
|
} else {
|
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
|
|
|
|
"'%s' object is not subscriptable", mp_obj_get_type_str(base)));
|
|
|
|
}
|
2014-04-17 17:10:53 -04:00
|
|
|
} else {
|
2014-11-06 12:36:16 -05:00
|
|
|
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
|
|
|
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
|
|
|
|
"object does not support item assignment"));
|
|
|
|
} else {
|
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
|
|
|
|
"'%s' object does not support item assignment", mp_obj_get_type_str(base)));
|
|
|
|
}
|
2014-04-17 17:10:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-20 11:37:30 -05:00
|
|
|
// Return input argument. Useful as .getiter for objects which are
|
|
|
|
// their own iterators, etc.
|
|
|
|
mp_obj_t mp_identity(mp_obj_t self) {
|
|
|
|
return self;
|
|
|
|
}
|
2014-02-08 14:57:19 -05:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(mp_identity_obj, mp_identity);
|
2014-04-08 17:25:28 -04:00
|
|
|
|
2014-08-30 09:28:06 -04:00
|
|
|
bool mp_get_buffer(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
|
2014-04-13 07:08:52 -04:00
|
|
|
mp_obj_type_t *type = mp_obj_get_type(obj);
|
|
|
|
if (type->buffer_p.get_buffer == NULL) {
|
2014-04-08 17:25:28 -04:00
|
|
|
return false;
|
|
|
|
}
|
2014-04-18 17:59:24 -04:00
|
|
|
int ret = type->buffer_p.get_buffer(obj, bufinfo, flags);
|
2014-08-10 04:46:10 -04:00
|
|
|
if (ret != 0) {
|
2014-04-08 17:25:28 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-08-30 09:28:06 -04:00
|
|
|
void mp_get_buffer_raise(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
|
2014-04-18 17:59:24 -04:00
|
|
|
if (!mp_get_buffer(obj, bufinfo, flags)) {
|
2014-04-18 17:29:21 -04:00
|
|
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "object with buffer protocol required"));
|
2014-04-08 17:25:28 -04:00
|
|
|
}
|
|
|
|
}
|