blob: 7b8be5889fb3259929c89384b85a5c57104c368b [file] [log] [blame]
Damien660365e2013-12-17 18:27:24 +00001#include <stdio.h>
2#include <stdarg.h>
3#include <assert.h>
4
5#include "nlr.h"
6#include "misc.h"
Damiend99b0522013-12-21 18:17:45 +00007#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +00008#include "qstr.h"
Damien660365e2013-12-17 18:27:24 +00009#include "obj.h"
Damiend99b0522013-12-21 18:17:45 +000010#include "runtime0.h"
11#include "runtime.h"
Damien660365e2013-12-17 18:27:24 +000012
Damien George5fa93b62014-01-22 14:35:10 +000013mp_obj_type_t *mp_obj_get_type(mp_obj_t o_in) {
Damien Georgeb97669a2014-01-08 11:47:55 +000014 if (MP_OBJ_IS_SMALL_INT(o_in)) {
Damien George3e1a5c12014-03-29 13:43:38 +000015 return (mp_obj_t)&mp_type_int;
Damien George38a2da62014-01-08 17:33:12 +000016 } else if (MP_OBJ_IS_QSTR(o_in)) {
Damien George3e1a5c12014-03-29 13:43:38 +000017 return (mp_obj_t)&mp_type_str;
Damien Georgeb97669a2014-01-08 11:47:55 +000018 } else {
19 mp_obj_base_t *o = o_in;
20 return (mp_obj_t)o->type;
21 }
22}
23
Damiend99b0522013-12-21 18:17:45 +000024const char *mp_obj_get_type_str(mp_obj_t o_in) {
Damien Georgea71c83a2014-02-15 11:34:50 +000025 return qstr_str(mp_obj_get_type(o_in)->name);
Damien660365e2013-12-17 18:27:24 +000026}
27
28void printf_wrapper(void *env, const char *fmt, ...) {
29 va_list args;
30 va_start(args, fmt);
31 vprintf(fmt, args);
32 va_end(args);
33}
34
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020035void mp_obj_print_helper(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
Damien George5fa93b62014-01-22 14:35:10 +000036 mp_obj_type_t *type = mp_obj_get_type(o_in);
37 if (type->print != NULL) {
38 type->print(print, env, o_in, kind);
Damien660365e2013-12-17 18:27:24 +000039 } else {
Damien Georgea71c83a2014-02-15 11:34:50 +000040 print(env, "<%s>", qstr_str(type->name));
Damien660365e2013-12-17 18:27:24 +000041 }
42}
43
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020044void mp_obj_print(mp_obj_t o_in, mp_print_kind_t kind) {
45 mp_obj_print_helper(printf_wrapper, NULL, o_in, kind);
Damien660365e2013-12-17 18:27:24 +000046}
47
Damien George136b1492014-01-19 12:38:49 +000048// helper function to print an exception with traceback
49void mp_obj_print_exception(mp_obj_t exc) {
Damien Georgec5966122014-02-15 16:10:44 +000050 if (mp_obj_is_exception_instance(exc)) {
Damien George136b1492014-01-19 12:38:49 +000051 machine_uint_t n, *values;
52 mp_obj_exception_get_traceback(exc, &n, &values);
53 if (n > 0) {
Paul Sokolovsky0ae518f2014-03-30 02:08:36 +020054 assert(n % 3 == 0);
Damien George136b1492014-01-19 12:38:49 +000055 printf("Traceback (most recent call last):\n");
56 for (int i = n - 3; i >= 0; i -= 3) {
Damien George62ad1892014-01-29 21:51:51 +000057#if MICROPY_ENABLE_SOURCE_LINE
Damien George136b1492014-01-19 12:38:49 +000058 printf(" File \"%s\", line %d, in %s\n", qstr_str(values[i]), (int)values[i + 1], qstr_str(values[i + 2]));
Damien George62ad1892014-01-29 21:51:51 +000059#else
60 printf(" File \"%s\", in %s\n", qstr_str(values[i]), qstr_str(values[i + 2]));
61#endif
Damien George136b1492014-01-19 12:38:49 +000062 }
63 }
64 }
Paul Sokolovskya96d3d02014-03-31 01:10:10 +030065 mp_obj_print(exc, PRINT_EXC);
Damien George136b1492014-01-19 12:38:49 +000066 printf("\n");
67}
68
Damien Georged17926d2014-03-30 13:35:08 +010069int mp_obj_is_true(mp_obj_t arg) {
70 if (arg == mp_const_false) {
71 return 0;
72 } else if (arg == mp_const_true) {
73 return 1;
74 } else if (arg == mp_const_none) {
75 return 0;
76 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
77 if (MP_OBJ_SMALL_INT_VALUE(arg) == 0) {
78 return 0;
79 } else {
80 return 1;
81 }
82 } else {
83 mp_obj_type_t *type = mp_obj_get_type(arg);
84 if (type->unary_op != NULL) {
85 mp_obj_t result = type->unary_op(MP_UNARY_OP_BOOL, arg);
86 if (result != MP_OBJ_NULL) {
87 return result == mp_const_true;
88 }
89 }
90
91 mp_obj_t len = mp_obj_len_maybe(arg);
92 if (len != MP_OBJ_NULL) {
93 // obj has a length, truth determined if len != 0
94 return len != MP_OBJ_NEW_SMALL_INT(0);
95 } else {
96 // any other obj is true per Python semantics
97 return 1;
98 }
99 }
100}
101
Damien Georgeb5fbd0b2014-04-09 19:55:33 +0100102// returns true if o_in is bool, small int, or long int
103bool mp_obj_is_integer(mp_obj_t o_in) {
104 return MP_OBJ_IS_INT(o_in) || MP_OBJ_IS_TYPE(o_in, &mp_type_bool);
105}
106
Damiend99b0522013-12-21 18:17:45 +0000107bool mp_obj_is_callable(mp_obj_t o_in) {
Damien Georgeb051e7d2014-01-23 18:13:53 +0000108 return mp_obj_get_type(o_in)->call != NULL;
Damien660365e2013-12-17 18:27:24 +0000109}
110
Damiend99b0522013-12-21 18:17:45 +0000111machine_int_t mp_obj_hash(mp_obj_t o_in) {
112 if (o_in == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +0000113 return 0; // needs to hash to same as the integer 0, since False==0
Damiend99b0522013-12-21 18:17:45 +0000114 } else if (o_in == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +0000115 return 1; // needs to hash to same as the integer 1, since True==1
Damiend99b0522013-12-21 18:17:45 +0000116 } else if (MP_OBJ_IS_SMALL_INT(o_in)) {
117 return MP_OBJ_SMALL_INT_VALUE(o_in);
Damien George5fa93b62014-01-22 14:35:10 +0000118 } else if (MP_OBJ_IS_STR(o_in)) {
119 return mp_obj_str_get_hash(o_in);
Damien George07ddab52014-03-29 13:15:08 +0000120 } else if (MP_OBJ_IS_TYPE(o_in, &mp_type_NoneType)) {
Damien660365e2013-12-17 18:27:24 +0000121 return (machine_int_t)o_in;
Damien George3e1a5c12014-03-29 13:43:38 +0000122 } else if (MP_OBJ_IS_TYPE(o_in, &mp_type_fun_native) || MP_OBJ_IS_TYPE(o_in, &mp_type_fun_bc)) {
Damien George7f8be592014-03-20 19:20:59 +0000123 return (machine_int_t)o_in;
Damien George07ddab52014-03-29 13:15:08 +0000124 } else if (MP_OBJ_IS_TYPE(o_in, &mp_type_tuple)) {
Damien George7f8be592014-03-20 19:20:59 +0000125 return mp_obj_tuple_hash(o_in);
Paul Sokolovsky91cbe602014-04-05 12:50:43 +0300126 } else if (MP_OBJ_IS_TYPE(o_in, &mp_type_type)) {
127 return (machine_int_t)o_in;
Damien George7f8be592014-03-20 19:20:59 +0000128
129 // TODO hash class and instances
130 // TODO delegate to __hash__ method if it exists
131
Damien660365e2013-12-17 18:27:24 +0000132 } else {
Damien Georgeea13f402014-04-05 18:32:08 +0100133 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "unhashable type: '%s'", mp_obj_get_type_str(o_in)));
Damien660365e2013-12-17 18:27:24 +0000134 }
135}
136
137// this function implements the '==' operator (and so the inverse of '!=')
138// from the python language reference:
139// "The objects need not have the same type. If both are numbers, they are converted
140// to a common type. Otherwise, the == and != operators always consider objects of
141// different types to be unequal."
142// note also that False==0 and True==1 are true expressions
Damiend99b0522013-12-21 18:17:45 +0000143bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2) {
Damien660365e2013-12-17 18:27:24 +0000144 if (o1 == o2) {
145 return true;
Damien Georgee0f29792014-03-30 23:16:42 +0100146 } else if (o1 == mp_const_none || o2 == mp_const_none) {
147 return false;
Damiend99b0522013-12-21 18:17:45 +0000148 } else if (MP_OBJ_IS_SMALL_INT(o1) || MP_OBJ_IS_SMALL_INT(o2)) {
149 if (MP_OBJ_IS_SMALL_INT(o1) && MP_OBJ_IS_SMALL_INT(o2)) {
Damien660365e2013-12-17 18:27:24 +0000150 return false;
151 } else {
Damien Georgeb8a053a2014-04-11 10:10:37 +0100152 if (MP_OBJ_IS_SMALL_INT(o1)) {
153 mp_obj_t temp = o2; o2 = o1; o1 = temp;
Damien660365e2013-12-17 18:27:24 +0000154 }
Damien Georgeb8a053a2014-04-11 10:10:37 +0100155 // o2 is the SMALL_INT, o1 is not
156 // fall through to generic op
Damien660365e2013-12-17 18:27:24 +0000157 }
Damien George5fa93b62014-01-22 14:35:10 +0000158 } else if (MP_OBJ_IS_STR(o1) && MP_OBJ_IS_STR(o2)) {
159 return mp_obj_str_equal(o1, o2);
Damien660365e2013-12-17 18:27:24 +0000160 }
Damien Georgeb8a053a2014-04-11 10:10:37 +0100161
162 // generic type, call binary_op(MP_BINARY_OP_EQUAL)
163 mp_obj_type_t *type = mp_obj_get_type(o1);
164 if (type->binary_op != NULL) {
165 mp_obj_t r = type->binary_op(MP_BINARY_OP_EQUAL, o1, o2);
166 if (r != MP_OBJ_NULL) {
167 return r == mp_const_true ? true : false;
168 }
169 }
170
171 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_NotImplementedError,
172 "Equality for '%s' and '%s' types not yet implemented", mp_obj_get_type_str(o1), mp_obj_get_type_str(o2)));
173 return false;
Damien660365e2013-12-17 18:27:24 +0000174}
175
Damiend99b0522013-12-21 18:17:45 +0000176machine_int_t mp_obj_get_int(mp_obj_t arg) {
Paul Sokolovskye99841b2014-04-05 17:46:47 +0300177 // This function essentially performs implicit type conversion to int
178 // Note that Python does NOT provide implicit type conversion from
179 // float to int in the core expression language, try some_list[1.0].
Damiend99b0522013-12-21 18:17:45 +0000180 if (arg == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +0000181 return 0;
Damiend99b0522013-12-21 18:17:45 +0000182 } else if (arg == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +0000183 return 1;
Damiend99b0522013-12-21 18:17:45 +0000184 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
185 return MP_OBJ_SMALL_INT_VALUE(arg);
Damien George3e1a5c12014-03-29 13:43:38 +0000186 } else if (MP_OBJ_IS_TYPE(arg, &mp_type_int)) {
Paul Sokolovskyd26b3792014-01-18 16:07:16 +0200187 return mp_obj_int_get_checked(arg);
Damien660365e2013-12-17 18:27:24 +0000188 } else {
Damien Georgeea13f402014-04-05 18:32:08 +0100189 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "can't convert %s to int", mp_obj_get_type_str(arg)));
Damien660365e2013-12-17 18:27:24 +0000190 }
191}
192
Damien George8270e382014-04-03 11:00:54 +0000193// returns false if arg is not of integral type
194// returns true and sets *value if it is of integral type
195// can throw OverflowError if arg is of integral type, but doesn't fit in a machine_int_t
196bool mp_obj_get_int_maybe(mp_obj_t arg, machine_int_t *value) {
197 if (arg == mp_const_false) {
198 *value = 0;
199 } else if (arg == mp_const_true) {
200 *value = 1;
201 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
202 *value = MP_OBJ_SMALL_INT_VALUE(arg);
203 } else if (MP_OBJ_IS_TYPE(arg, &mp_type_int)) {
204 *value = mp_obj_int_get_checked(arg);
205 } else {
206 return false;
207 }
208 return true;
209}
210
Damien660365e2013-12-17 18:27:24 +0000211#if MICROPY_ENABLE_FLOAT
Damien George0c36da02014-03-08 15:24:39 +0000212mp_float_t mp_obj_get_float(mp_obj_t arg) {
Damiend99b0522013-12-21 18:17:45 +0000213 if (arg == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +0000214 return 0;
Damiend99b0522013-12-21 18:17:45 +0000215 } else if (arg == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +0000216 return 1;
Damiend99b0522013-12-21 18:17:45 +0000217 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
218 return MP_OBJ_SMALL_INT_VALUE(arg);
Damien George3e1a5c12014-03-29 13:43:38 +0000219 } else if (MP_OBJ_IS_TYPE(arg, &mp_type_int)) {
Damien Georgeeabdf672014-03-22 20:54:01 +0000220 return mp_obj_int_as_float(arg);
Damien George0c36da02014-03-08 15:24:39 +0000221 } else if (MP_OBJ_IS_TYPE(arg, &mp_type_float)) {
Damiend99b0522013-12-21 18:17:45 +0000222 return mp_obj_float_get(arg);
Damien660365e2013-12-17 18:27:24 +0000223 } else {
Damien Georgeea13f402014-04-05 18:32:08 +0100224 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "can't convert %s to float", mp_obj_get_type_str(arg)));
Damien660365e2013-12-17 18:27:24 +0000225 }
226}
227
Damiend99b0522013-12-21 18:17:45 +0000228void mp_obj_get_complex(mp_obj_t arg, mp_float_t *real, mp_float_t *imag) {
229 if (arg == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +0000230 *real = 0;
231 *imag = 0;
Damiend99b0522013-12-21 18:17:45 +0000232 } else if (arg == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +0000233 *real = 1;
234 *imag = 0;
Damiend99b0522013-12-21 18:17:45 +0000235 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
236 *real = MP_OBJ_SMALL_INT_VALUE(arg);
Damien660365e2013-12-17 18:27:24 +0000237 *imag = 0;
Damien George0aa5d512014-03-29 17:28:20 +0000238 } else if (MP_OBJ_IS_TYPE(arg, &mp_type_int)) {
239 *real = mp_obj_int_as_float(arg);
240 *imag = 0;
Damien George0c36da02014-03-08 15:24:39 +0000241 } else if (MP_OBJ_IS_TYPE(arg, &mp_type_float)) {
Damiend99b0522013-12-21 18:17:45 +0000242 *real = mp_obj_float_get(arg);
Damien660365e2013-12-17 18:27:24 +0000243 *imag = 0;
Damien George0c36da02014-03-08 15:24:39 +0000244 } else if (MP_OBJ_IS_TYPE(arg, &mp_type_complex)) {
Damiend99b0522013-12-21 18:17:45 +0000245 mp_obj_complex_get(arg, real, imag);
Damien660365e2013-12-17 18:27:24 +0000246 } else {
Damien Georgeea13f402014-04-05 18:32:08 +0100247 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "can't convert %s to complex", mp_obj_get_type_str(arg)));
Damien660365e2013-12-17 18:27:24 +0000248 }
249}
250#endif
251
Damien George24ff0632014-03-24 10:47:13 +0000252void mp_obj_get_array(mp_obj_t o, uint *len, mp_obj_t **items) {
Damien George07ddab52014-03-29 13:15:08 +0000253 if (MP_OBJ_IS_TYPE(o, &mp_type_tuple)) {
Damien George24ff0632014-03-24 10:47:13 +0000254 mp_obj_tuple_get(o, len, items);
Damien George3e1a5c12014-03-29 13:43:38 +0000255 } else if (MP_OBJ_IS_TYPE(o, &mp_type_list)) {
Damien George24ff0632014-03-24 10:47:13 +0000256 mp_obj_list_get(o, len, items);
Damien660365e2013-12-17 18:27:24 +0000257 } else {
Damien Georgeea13f402014-04-05 18:32:08 +0100258 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)));
Damien George24ff0632014-03-24 10:47:13 +0000259 }
260}
261
262void mp_obj_get_array_fixed_n(mp_obj_t o, uint len, mp_obj_t **items) {
Damien George3e1a5c12014-03-29 13:43:38 +0000263 if (MP_OBJ_IS_TYPE(o, &mp_type_tuple) || MP_OBJ_IS_TYPE(o, &mp_type_list)) {
Damien George24ff0632014-03-24 10:47:13 +0000264 uint seq_len;
Damien George07ddab52014-03-29 13:15:08 +0000265 if (MP_OBJ_IS_TYPE(o, &mp_type_tuple)) {
Damien George24ff0632014-03-24 10:47:13 +0000266 mp_obj_tuple_get(o, &seq_len, items);
267 } else {
268 mp_obj_list_get(o, &seq_len, items);
269 }
270 if (seq_len != len) {
Damien George686afc52014-04-11 09:13:30 +0100271 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "requested length %d but object has length %d", len, seq_len));
Damien George24ff0632014-03-24 10:47:13 +0000272 }
273 } else {
Damien Georgeea13f402014-04-05 18:32:08 +0100274 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)));
Damien660365e2013-12-17 18:27:24 +0000275 }
276}
277
xbe9e1e8cd2014-03-12 22:57:16 -0700278// is_slice determines whether the index is a slice index
279uint mp_get_index(const mp_obj_type_t *type, machine_uint_t len, mp_obj_t index, bool is_slice) {
280 int i;
Damien Georgeb5fbd0b2014-04-09 19:55:33 +0100281 if (MP_OBJ_IS_INT(index)) {
282 i = mp_obj_int_get_checked(index);
Damien George07ddab52014-03-29 13:15:08 +0000283 } else if (MP_OBJ_IS_TYPE(index, &mp_type_bool)) {
xbec5d70ba2014-03-13 00:29:15 -0700284 i = (index == mp_const_true ? 1 : 0);
Damiend99b0522013-12-21 18:17:45 +0000285 } else {
Damien Georgeea13f402014-04-05 18:32:08 +0100286 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)));
Damien660365e2013-12-17 18:27:24 +0000287 }
xbe9e1e8cd2014-03-12 22:57:16 -0700288
289 if (i < 0) {
xbec5d70ba2014-03-13 00:29:15 -0700290 i += len;
xbe9e1e8cd2014-03-12 22:57:16 -0700291 }
292 if (is_slice) {
xbec5d70ba2014-03-13 00:29:15 -0700293 if (i < 0) {
294 i = 0;
295 } else if (i > len) {
296 i = len;
297 }
xbe9e1e8cd2014-03-12 22:57:16 -0700298 } else {
xbec5d70ba2014-03-13 00:29:15 -0700299 if (i < 0 || i >= len) {
Damien Georgeea13f402014-04-05 18:32:08 +0100300 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_IndexError, "%s index out of range", qstr_str(type->name)));
xbec5d70ba2014-03-13 00:29:15 -0700301 }
xbe9e1e8cd2014-03-12 22:57:16 -0700302 }
303 return i;
Damien660365e2013-12-17 18:27:24 +0000304}
John R. Lenton4bee76e2014-01-10 11:25:03 +0000305
Damien Georgeeae16442014-01-11 19:22:29 +0000306// may return MP_OBJ_NULL
John R. Lenton4bee76e2014-01-10 11:25:03 +0000307mp_obj_t mp_obj_len_maybe(mp_obj_t o_in) {
Damien George5fa93b62014-01-22 14:35:10 +0000308 if (MP_OBJ_IS_STR(o_in)) {
Damien George09a0c642014-01-30 10:05:33 +0000309 return MP_OBJ_NEW_SMALL_INT((machine_int_t)mp_obj_str_get_len(o_in));
John R. Lenton4bee76e2014-01-10 11:25:03 +0000310 } else {
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200311 mp_obj_type_t *type = mp_obj_get_type(o_in);
312 if (type->unary_op != NULL) {
Damien Georged17926d2014-03-30 13:35:08 +0100313 return type->unary_op(MP_UNARY_OP_LEN, o_in);
Damien George09a0c642014-01-30 10:05:33 +0000314 } else {
315 return MP_OBJ_NULL;
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200316 }
John R. Lenton4bee76e2014-01-10 11:25:03 +0000317 }
John R. Lenton4bee76e2014-01-10 11:25:03 +0000318}
Paul Sokolovskydff3f892014-01-20 18:37:30 +0200319
320// Return input argument. Useful as .getiter for objects which are
321// their own iterators, etc.
322mp_obj_t mp_identity(mp_obj_t self) {
323 return self;
324}
Paul Sokolovsky557c9d52014-02-08 21:57:19 +0200325MP_DEFINE_CONST_FUN_OBJ_1(mp_identity_obj, mp_identity);
Paul Sokolovsky3aa8ee72014-04-09 00:25:28 +0300326
327bool mp_get_buffer(mp_obj_t obj, buffer_info_t *bufinfo) {
328 mp_obj_base_t *o = (mp_obj_base_t *)obj;
329 if (o->type->buffer_p.get_buffer == NULL) {
330 return false;
331 }
332 o->type->buffer_p.get_buffer(o, bufinfo, BUFFER_READ);
333 if (bufinfo->buf == NULL) {
334 return false;
335 }
336 return true;
337}
338
339void mp_get_buffer_raise(mp_obj_t obj, buffer_info_t *bufinfo) {
340 if (!mp_get_buffer(obj, bufinfo)) {
341 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "Object with buffer protocol required"));
342 }
343}