Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the Micro Python project, http://micropython.org/ |
| 3 | * |
| 4 | * The MIT License (MIT) |
| 5 | * |
| 6 | * Copyright (c) 2013, 2014 Damien P. George |
| 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | * of this software and associated documentation files (the "Software"), to deal |
| 10 | * in the Software without restriction, including without limitation the rights |
| 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | * copies of the Software, and to permit persons to whom the Software is |
| 13 | * furnished to do so, subject to the following conditions: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included in |
| 16 | * all copies or substantial portions of the Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | * THE SOFTWARE. |
| 25 | */ |
| 26 | |
Damien George | ffe911d | 2014-07-24 14:21:37 +0100 | [diff] [blame] | 27 | #include <stdint.h> |
Damien | 660365e | 2013-12-17 18:27:24 +0000 | [diff] [blame] | 28 | #include <stdio.h> |
| 29 | #include <stdarg.h> |
| 30 | #include <assert.h> |
| 31 | |
Damien George | 51dfcb4 | 2015-01-01 20:27:54 +0000 | [diff] [blame] | 32 | #include "py/nlr.h" |
| 33 | #include "py/obj.h" |
| 34 | #include "py/objtype.h" |
| 35 | #include "py/objint.h" |
Damien George | 5169822 | 2015-09-03 23:01:07 +0100 | [diff] [blame] | 36 | #include "py/objstr.h" |
Damien George | 51dfcb4 | 2015-01-01 20:27:54 +0000 | [diff] [blame] | 37 | #include "py/runtime0.h" |
| 38 | #include "py/runtime.h" |
| 39 | #include "py/stackctrl.h" |
Paul Sokolovsky | 1f91e92 | 2015-02-17 00:12:42 +0200 | [diff] [blame] | 40 | #include "py/stream.h" // for mp_obj_print |
Damien | 660365e | 2013-12-17 18:27:24 +0000 | [diff] [blame] | 41 | |
Paul Sokolovsky | 7aca1ca | 2014-05-11 02:26:42 +0300 | [diff] [blame] | 42 | mp_obj_type_t *mp_obj_get_type(mp_const_obj_t o_in) { |
Damien George | b97669a | 2014-01-08 11:47:55 +0000 | [diff] [blame] | 43 | if (MP_OBJ_IS_SMALL_INT(o_in)) { |
Damien George | 3e1a5c1 | 2014-03-29 13:43:38 +0000 | [diff] [blame] | 44 | return (mp_obj_t)&mp_type_int; |
Damien George | 38a2da6 | 2014-01-08 17:33:12 +0000 | [diff] [blame] | 45 | } else if (MP_OBJ_IS_QSTR(o_in)) { |
Damien George | 3e1a5c1 | 2014-03-29 13:43:38 +0000 | [diff] [blame] | 46 | return (mp_obj_t)&mp_type_str; |
Damien George | b97669a | 2014-01-08 11:47:55 +0000 | [diff] [blame] | 47 | } else { |
Paul Sokolovsky | 7aca1ca | 2014-05-11 02:26:42 +0300 | [diff] [blame] | 48 | const mp_obj_base_t *o = o_in; |
Damien George | b97669a | 2014-01-08 11:47:55 +0000 | [diff] [blame] | 49 | return (mp_obj_t)o->type; |
| 50 | } |
| 51 | } |
| 52 | |
Paul Sokolovsky | 7aca1ca | 2014-05-11 02:26:42 +0300 | [diff] [blame] | 53 | const char *mp_obj_get_type_str(mp_const_obj_t o_in) { |
Damien George | a71c83a | 2014-02-15 11:34:50 +0000 | [diff] [blame] | 54 | return qstr_str(mp_obj_get_type(o_in)->name); |
Damien | 660365e | 2013-12-17 18:27:24 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Damien George | 7f9d1d6 | 2015-04-09 23:56:15 +0100 | [diff] [blame] | 57 | void mp_obj_print_helper(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) { |
Paul Sokolovsky | 8993fb6 | 2014-06-28 02:25:04 +0300 | [diff] [blame] | 58 | // There can be data structures nested too deep, or just recursive |
Paul Sokolovsky | caa7334 | 2014-07-01 02:13:42 +0300 | [diff] [blame] | 59 | MP_STACK_CHECK(); |
Damien George | 8788b13 | 2015-01-25 18:35:54 +0000 | [diff] [blame] | 60 | #ifndef NDEBUG |
Paul Sokolovsky | 0bc1594 | 2014-05-10 21:05:45 +0300 | [diff] [blame] | 61 | if (o_in == NULL) { |
Damien George | 7f9d1d6 | 2015-04-09 23:56:15 +0100 | [diff] [blame] | 62 | mp_print_str(print, "(nil)"); |
Paul Sokolovsky | 0bc1594 | 2014-05-10 21:05:45 +0300 | [diff] [blame] | 63 | return; |
| 64 | } |
| 65 | #endif |
Damien George | 5fa93b6 | 2014-01-22 14:35:10 +0000 | [diff] [blame] | 66 | mp_obj_type_t *type = mp_obj_get_type(o_in); |
| 67 | if (type->print != NULL) { |
Damien George | 7f9d1d6 | 2015-04-09 23:56:15 +0100 | [diff] [blame] | 68 | type->print((mp_print_t*)print, o_in, kind); |
Damien | 660365e | 2013-12-17 18:27:24 +0000 | [diff] [blame] | 69 | } else { |
Damien George | 044c473 | 2015-04-11 13:03:37 +0100 | [diff] [blame] | 70 | mp_printf(print, "<%q>", type->name); |
Damien | 660365e | 2013-12-17 18:27:24 +0000 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
Paul Sokolovsky | 76d982e | 2014-01-13 19:19:16 +0200 | [diff] [blame] | 74 | void mp_obj_print(mp_obj_t o_in, mp_print_kind_t kind) { |
Paul Sokolovsky | 8c70523 | 2015-02-17 00:32:18 +0200 | [diff] [blame] | 75 | #if MICROPY_PY_IO |
Damien George | 5ae5ec9 | 2015-04-11 12:01:39 +0100 | [diff] [blame] | 76 | mp_obj_print_helper(&mp_sys_stdout_print, o_in, kind); |
Paul Sokolovsky | 8c70523 | 2015-02-17 00:32:18 +0200 | [diff] [blame] | 77 | #else |
Damien George | 7f9d1d6 | 2015-04-09 23:56:15 +0100 | [diff] [blame] | 78 | mp_obj_print_helper(&mp_plat_print, o_in, kind); |
Paul Sokolovsky | 8c70523 | 2015-02-17 00:32:18 +0200 | [diff] [blame] | 79 | #endif |
Damien | 660365e | 2013-12-17 18:27:24 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Damien George | 136b149 | 2014-01-19 12:38:49 +0000 | [diff] [blame] | 82 | // helper function to print an exception with traceback |
Damien George | 7f9d1d6 | 2015-04-09 23:56:15 +0100 | [diff] [blame] | 83 | void mp_obj_print_exception(const mp_print_t *print, mp_obj_t exc) { |
Damien George | c596612 | 2014-02-15 16:10:44 +0000 | [diff] [blame] | 84 | if (mp_obj_is_exception_instance(exc)) { |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 85 | mp_uint_t n, *values; |
Damien George | 136b149 | 2014-01-19 12:38:49 +0000 | [diff] [blame] | 86 | mp_obj_exception_get_traceback(exc, &n, &values); |
| 87 | if (n > 0) { |
Paul Sokolovsky | 0ae518f | 2014-03-30 02:08:36 +0200 | [diff] [blame] | 88 | assert(n % 3 == 0); |
Damien George | 7f9d1d6 | 2015-04-09 23:56:15 +0100 | [diff] [blame] | 89 | mp_print_str(print, "Traceback (most recent call last):\n"); |
Damien George | 136b149 | 2014-01-19 12:38:49 +0000 | [diff] [blame] | 90 | for (int i = n - 3; i >= 0; i -= 3) { |
Damien George | 62ad189 | 2014-01-29 21:51:51 +0000 | [diff] [blame] | 91 | #if MICROPY_ENABLE_SOURCE_LINE |
Damien George | 044c473 | 2015-04-11 13:03:37 +0100 | [diff] [blame] | 92 | mp_printf(print, " File \"%q\", line %d", values[i], (int)values[i + 1]); |
Damien George | 62ad189 | 2014-01-29 21:51:51 +0000 | [diff] [blame] | 93 | #else |
Damien George | 044c473 | 2015-04-11 13:03:37 +0100 | [diff] [blame] | 94 | mp_printf(print, " File \"%q\"", values[i]); |
Damien George | 62ad189 | 2014-01-29 21:51:51 +0000 | [diff] [blame] | 95 | #endif |
Damien George | 0e4ba25 | 2014-04-13 15:01:28 +0100 | [diff] [blame] | 96 | // the block name can be NULL if it's unknown |
| 97 | qstr block = values[i + 2]; |
| 98 | if (block == MP_QSTR_NULL) { |
Damien George | 7f9d1d6 | 2015-04-09 23:56:15 +0100 | [diff] [blame] | 99 | mp_print_str(print, "\n"); |
Damien George | 0e4ba25 | 2014-04-13 15:01:28 +0100 | [diff] [blame] | 100 | } else { |
Damien George | 044c473 | 2015-04-11 13:03:37 +0100 | [diff] [blame] | 101 | mp_printf(print, ", in %q\n", block); |
Damien George | 0e4ba25 | 2014-04-13 15:01:28 +0100 | [diff] [blame] | 102 | } |
Damien George | 136b149 | 2014-01-19 12:38:49 +0000 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | } |
Damien George | 7f9d1d6 | 2015-04-09 23:56:15 +0100 | [diff] [blame] | 106 | mp_obj_print_helper(print, exc, PRINT_EXC); |
| 107 | mp_print_str(print, "\n"); |
Damien George | 136b149 | 2014-01-19 12:38:49 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Damien George | 4d91723 | 2014-08-30 14:28:06 +0100 | [diff] [blame] | 110 | bool mp_obj_is_true(mp_obj_t arg) { |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 111 | if (arg == mp_const_false) { |
| 112 | return 0; |
| 113 | } else if (arg == mp_const_true) { |
| 114 | return 1; |
| 115 | } else if (arg == mp_const_none) { |
| 116 | return 0; |
| 117 | } else if (MP_OBJ_IS_SMALL_INT(arg)) { |
| 118 | if (MP_OBJ_SMALL_INT_VALUE(arg) == 0) { |
| 119 | return 0; |
| 120 | } else { |
| 121 | return 1; |
| 122 | } |
| 123 | } else { |
| 124 | mp_obj_type_t *type = mp_obj_get_type(arg); |
| 125 | if (type->unary_op != NULL) { |
| 126 | mp_obj_t result = type->unary_op(MP_UNARY_OP_BOOL, arg); |
Damien George | 6ac5dce | 2014-05-21 19:42:43 +0100 | [diff] [blame] | 127 | if (result != MP_OBJ_NULL) { |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 128 | return result == mp_const_true; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | mp_obj_t len = mp_obj_len_maybe(arg); |
| 133 | if (len != MP_OBJ_NULL) { |
| 134 | // obj has a length, truth determined if len != 0 |
| 135 | return len != MP_OBJ_NEW_SMALL_INT(0); |
| 136 | } else { |
| 137 | // any other obj is true per Python semantics |
| 138 | return 1; |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 143 | bool mp_obj_is_callable(mp_obj_t o_in) { |
Damien George | 0344fa1 | 2014-11-03 16:09:39 +0000 | [diff] [blame] | 144 | mp_call_fun_t call = mp_obj_get_type(o_in)->call; |
| 145 | if (call != mp_obj_instance_call) { |
| 146 | return call != NULL; |
| 147 | } |
| 148 | return mp_obj_instance_is_callable(o_in); |
Damien | 660365e | 2013-12-17 18:27:24 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Damien George | c38dc3c | 2015-01-11 15:13:18 +0000 | [diff] [blame] | 151 | // This function implements the '==' operator (and so the inverse of '!='). |
| 152 | // |
| 153 | // From the Python language reference: |
| 154 | // (https://docs.python.org/3/reference/expressions.html#not-in) |
Damien | 660365e | 2013-12-17 18:27:24 +0000 | [diff] [blame] | 155 | // "The objects need not have the same type. If both are numbers, they are converted |
| 156 | // to a common type. Otherwise, the == and != operators always consider objects of |
| 157 | // different types to be unequal." |
Damien George | c38dc3c | 2015-01-11 15:13:18 +0000 | [diff] [blame] | 158 | // |
| 159 | // This means that False==0 and True==1 are true expressions. |
| 160 | // |
| 161 | // Furthermore, from the v3.4.2 code for object.c: "Practical amendments: If rich |
| 162 | // comparison returns NotImplemented, == and != are decided by comparing the object |
| 163 | // pointer." |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 164 | bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2) { |
Damien | 660365e | 2013-12-17 18:27:24 +0000 | [diff] [blame] | 165 | if (o1 == o2) { |
| 166 | return true; |
Damien George | e22d76e | 2014-04-11 10:52:06 +0000 | [diff] [blame] | 167 | } |
| 168 | if (o1 == mp_const_none || o2 == mp_const_none) { |
Damien George | e0f2979 | 2014-03-30 23:16:42 +0100 | [diff] [blame] | 169 | return false; |
Damien George | e22d76e | 2014-04-11 10:52:06 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | // fast path for small ints |
| 173 | if (MP_OBJ_IS_SMALL_INT(o1)) { |
| 174 | if (MP_OBJ_IS_SMALL_INT(o2)) { |
| 175 | // both SMALL_INT, and not equal if we get here |
Damien | 660365e | 2013-12-17 18:27:24 +0000 | [diff] [blame] | 176 | return false; |
| 177 | } else { |
Damien George | e22d76e | 2014-04-11 10:52:06 +0000 | [diff] [blame] | 178 | mp_obj_t temp = o2; o2 = o1; o1 = temp; |
| 179 | // o2 is now the SMALL_INT, o1 is not |
Damien George | b8a053a | 2014-04-11 10:10:37 +0100 | [diff] [blame] | 180 | // fall through to generic op |
Damien | 660365e | 2013-12-17 18:27:24 +0000 | [diff] [blame] | 181 | } |
Damien George | e22d76e | 2014-04-11 10:52:06 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | // fast path for strings |
| 185 | if (MP_OBJ_IS_STR(o1)) { |
| 186 | if (MP_OBJ_IS_STR(o2)) { |
| 187 | // both strings, use special function |
| 188 | return mp_obj_str_equal(o1, o2); |
| 189 | } else { |
| 190 | // a string is never equal to anything else |
| 191 | return false; |
| 192 | } |
| 193 | } else if (MP_OBJ_IS_STR(o2)) { |
| 194 | // o1 is not a string (else caught above), so the objects are not equal |
| 195 | return false; |
Damien | 660365e | 2013-12-17 18:27:24 +0000 | [diff] [blame] | 196 | } |
Damien George | b8a053a | 2014-04-11 10:10:37 +0100 | [diff] [blame] | 197 | |
| 198 | // generic type, call binary_op(MP_BINARY_OP_EQUAL) |
| 199 | mp_obj_type_t *type = mp_obj_get_type(o1); |
| 200 | if (type->binary_op != NULL) { |
| 201 | mp_obj_t r = type->binary_op(MP_BINARY_OP_EQUAL, o1, o2); |
Damien George | 6ac5dce | 2014-05-21 19:42:43 +0100 | [diff] [blame] | 202 | if (r != MP_OBJ_NULL) { |
Damien George | b8a053a | 2014-04-11 10:10:37 +0100 | [diff] [blame] | 203 | return r == mp_const_true ? true : false; |
| 204 | } |
| 205 | } |
| 206 | |
Damien George | c38dc3c | 2015-01-11 15:13:18 +0000 | [diff] [blame] | 207 | // equality not implemented, and objects are not the same object, so |
| 208 | // they are defined as not equal |
| 209 | return false; |
Damien | 660365e | 2013-12-17 18:27:24 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 212 | mp_int_t mp_obj_get_int(mp_const_obj_t arg) { |
Paul Sokolovsky | e99841b | 2014-04-05 17:46:47 +0300 | [diff] [blame] | 213 | // This function essentially performs implicit type conversion to int |
| 214 | // Note that Python does NOT provide implicit type conversion from |
| 215 | // float to int in the core expression language, try some_list[1.0]. |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 216 | if (arg == mp_const_false) { |
Damien | 660365e | 2013-12-17 18:27:24 +0000 | [diff] [blame] | 217 | return 0; |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 218 | } else if (arg == mp_const_true) { |
Damien | 660365e | 2013-12-17 18:27:24 +0000 | [diff] [blame] | 219 | return 1; |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 220 | } else if (MP_OBJ_IS_SMALL_INT(arg)) { |
| 221 | return MP_OBJ_SMALL_INT_VALUE(arg); |
Damien George | 3e1a5c1 | 2014-03-29 13:43:38 +0000 | [diff] [blame] | 222 | } else if (MP_OBJ_IS_TYPE(arg, &mp_type_int)) { |
Paul Sokolovsky | d26b379 | 2014-01-18 16:07:16 +0200 | [diff] [blame] | 223 | return mp_obj_int_get_checked(arg); |
Damien | 660365e | 2013-12-17 18:27:24 +0000 | [diff] [blame] | 224 | } else { |
Damien George | 1e9a92f | 2014-11-06 17:36:16 +0000 | [diff] [blame] | 225 | if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { |
| 226 | nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, |
| 227 | "can't convert to int")); |
| 228 | } else { |
| 229 | nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, |
| 230 | "can't convert %s to int", mp_obj_get_type_str(arg))); |
| 231 | } |
Damien | 660365e | 2013-12-17 18:27:24 +0000 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | |
Damien George | c50772d | 2015-05-12 23:05:53 +0100 | [diff] [blame] | 235 | mp_int_t mp_obj_get_int_truncated(mp_const_obj_t arg) { |
| 236 | if (MP_OBJ_IS_INT(arg)) { |
| 237 | return mp_obj_int_get_truncated(arg); |
| 238 | } else { |
| 239 | return mp_obj_get_int(arg); |
| 240 | } |
| 241 | } |
| 242 | |
Damien George | 8270e38 | 2014-04-03 11:00:54 +0000 | [diff] [blame] | 243 | // returns false if arg is not of integral type |
| 244 | // returns true and sets *value if it is of integral type |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 245 | // can throw OverflowError if arg is of integral type, but doesn't fit in a mp_int_t |
| 246 | bool mp_obj_get_int_maybe(mp_const_obj_t arg, mp_int_t *value) { |
Damien George | 8270e38 | 2014-04-03 11:00:54 +0000 | [diff] [blame] | 247 | if (arg == mp_const_false) { |
| 248 | *value = 0; |
| 249 | } else if (arg == mp_const_true) { |
| 250 | *value = 1; |
| 251 | } else if (MP_OBJ_IS_SMALL_INT(arg)) { |
| 252 | *value = MP_OBJ_SMALL_INT_VALUE(arg); |
| 253 | } else if (MP_OBJ_IS_TYPE(arg, &mp_type_int)) { |
| 254 | *value = mp_obj_int_get_checked(arg); |
| 255 | } else { |
| 256 | return false; |
| 257 | } |
| 258 | return true; |
| 259 | } |
| 260 | |
Damien George | fb510b3 | 2014-06-01 13:32:54 +0100 | [diff] [blame] | 261 | #if MICROPY_PY_BUILTINS_FLOAT |
Damien George | 0c36da0 | 2014-03-08 15:24:39 +0000 | [diff] [blame] | 262 | mp_float_t mp_obj_get_float(mp_obj_t arg) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 263 | if (arg == mp_const_false) { |
Damien | 660365e | 2013-12-17 18:27:24 +0000 | [diff] [blame] | 264 | return 0; |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 265 | } else if (arg == mp_const_true) { |
Damien | 660365e | 2013-12-17 18:27:24 +0000 | [diff] [blame] | 266 | return 1; |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 267 | } else if (MP_OBJ_IS_SMALL_INT(arg)) { |
| 268 | return MP_OBJ_SMALL_INT_VALUE(arg); |
Damien George | 3e1a5c1 | 2014-03-29 13:43:38 +0000 | [diff] [blame] | 269 | } else if (MP_OBJ_IS_TYPE(arg, &mp_type_int)) { |
Damien George | eabdf67 | 2014-03-22 20:54:01 +0000 | [diff] [blame] | 270 | return mp_obj_int_as_float(arg); |
Damien George | aaef185 | 2015-08-20 23:30:12 +0100 | [diff] [blame] | 271 | } else if (mp_obj_is_float(arg)) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 272 | return mp_obj_float_get(arg); |
Damien | 660365e | 2013-12-17 18:27:24 +0000 | [diff] [blame] | 273 | } else { |
Damien George | 1e9a92f | 2014-11-06 17:36:16 +0000 | [diff] [blame] | 274 | if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { |
| 275 | nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, |
| 276 | "can't convert to float")); |
| 277 | } else { |
| 278 | nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, |
| 279 | "can't convert %s to float", mp_obj_get_type_str(arg))); |
| 280 | } |
Damien | 660365e | 2013-12-17 18:27:24 +0000 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | |
Paul Sokolovsky | 3b6f7b9 | 2014-06-20 01:48:35 +0300 | [diff] [blame] | 284 | #if MICROPY_PY_BUILTINS_COMPLEX |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 285 | void mp_obj_get_complex(mp_obj_t arg, mp_float_t *real, mp_float_t *imag) { |
| 286 | if (arg == mp_const_false) { |
Damien | 660365e | 2013-12-17 18:27:24 +0000 | [diff] [blame] | 287 | *real = 0; |
| 288 | *imag = 0; |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 289 | } else if (arg == mp_const_true) { |
Damien | 660365e | 2013-12-17 18:27:24 +0000 | [diff] [blame] | 290 | *real = 1; |
| 291 | *imag = 0; |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 292 | } else if (MP_OBJ_IS_SMALL_INT(arg)) { |
| 293 | *real = MP_OBJ_SMALL_INT_VALUE(arg); |
Damien | 660365e | 2013-12-17 18:27:24 +0000 | [diff] [blame] | 294 | *imag = 0; |
Damien George | 0aa5d51 | 2014-03-29 17:28:20 +0000 | [diff] [blame] | 295 | } else if (MP_OBJ_IS_TYPE(arg, &mp_type_int)) { |
| 296 | *real = mp_obj_int_as_float(arg); |
| 297 | *imag = 0; |
Damien George | aaef185 | 2015-08-20 23:30:12 +0100 | [diff] [blame] | 298 | } else if (mp_obj_is_float(arg)) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 299 | *real = mp_obj_float_get(arg); |
Damien | 660365e | 2013-12-17 18:27:24 +0000 | [diff] [blame] | 300 | *imag = 0; |
Damien George | 0c36da0 | 2014-03-08 15:24:39 +0000 | [diff] [blame] | 301 | } else if (MP_OBJ_IS_TYPE(arg, &mp_type_complex)) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 302 | mp_obj_complex_get(arg, real, imag); |
Damien | 660365e | 2013-12-17 18:27:24 +0000 | [diff] [blame] | 303 | } else { |
Damien George | 1e9a92f | 2014-11-06 17:36:16 +0000 | [diff] [blame] | 304 | if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { |
| 305 | nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, |
| 306 | "can't convert to complex")); |
| 307 | } else { |
| 308 | nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, |
| 309 | "can't convert %s to complex", mp_obj_get_type_str(arg))); |
| 310 | } |
Damien | 660365e | 2013-12-17 18:27:24 +0000 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | #endif |
Paul Sokolovsky | 3b6f7b9 | 2014-06-20 01:48:35 +0300 | [diff] [blame] | 314 | #endif |
Damien | 660365e | 2013-12-17 18:27:24 +0000 | [diff] [blame] | 315 | |
Damien George | 9c4cbe2 | 2014-08-30 14:04:14 +0100 | [diff] [blame] | 316 | void mp_obj_get_array(mp_obj_t o, mp_uint_t *len, mp_obj_t **items) { |
Damien George | 07ddab5 | 2014-03-29 13:15:08 +0000 | [diff] [blame] | 317 | if (MP_OBJ_IS_TYPE(o, &mp_type_tuple)) { |
Damien George | 24ff063 | 2014-03-24 10:47:13 +0000 | [diff] [blame] | 318 | mp_obj_tuple_get(o, len, items); |
Damien George | 3e1a5c1 | 2014-03-29 13:43:38 +0000 | [diff] [blame] | 319 | } else if (MP_OBJ_IS_TYPE(o, &mp_type_list)) { |
Damien George | 24ff063 | 2014-03-24 10:47:13 +0000 | [diff] [blame] | 320 | mp_obj_list_get(o, len, items); |
Damien | 660365e | 2013-12-17 18:27:24 +0000 | [diff] [blame] | 321 | } else { |
Damien George | 1e9a92f | 2014-11-06 17:36:16 +0000 | [diff] [blame] | 322 | if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { |
| 323 | nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, |
| 324 | "expected tuple/list")); |
| 325 | } else { |
| 326 | nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, |
| 327 | "object '%s' is not a tuple or list", mp_obj_get_type_str(o))); |
| 328 | } |
Damien George | 24ff063 | 2014-03-24 10:47:13 +0000 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | |
Damien George | 9c4cbe2 | 2014-08-30 14:04:14 +0100 | [diff] [blame] | 332 | void mp_obj_get_array_fixed_n(mp_obj_t o, mp_uint_t len, mp_obj_t **items) { |
Damien George | ca6d75f | 2014-08-30 15:17:47 +0100 | [diff] [blame] | 333 | mp_uint_t seq_len; |
| 334 | mp_obj_get_array(o, &seq_len, items); |
| 335 | if (seq_len != len) { |
Damien George | 1e9a92f | 2014-11-06 17:36:16 +0000 | [diff] [blame] | 336 | if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { |
| 337 | nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, |
| 338 | "tuple/list has wrong length")); |
| 339 | } else { |
| 340 | nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, |
| 341 | "requested length %d but object has length %d", len, seq_len)); |
| 342 | } |
Damien | 660365e | 2013-12-17 18:27:24 +0000 | [diff] [blame] | 343 | } |
| 344 | } |
| 345 | |
xbe | 9e1e8cd | 2014-03-12 22:57:16 -0700 | [diff] [blame] | 346 | // is_slice determines whether the index is a slice index |
Damien George | 9c4cbe2 | 2014-08-30 14:04:14 +0100 | [diff] [blame] | 347 | mp_uint_t mp_get_index(const mp_obj_type_t *type, mp_uint_t len, mp_obj_t index, bool is_slice) { |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 348 | mp_int_t i; |
Damien George | a9ddd6d | 2014-04-11 10:40:38 +0000 | [diff] [blame] | 349 | if (MP_OBJ_IS_SMALL_INT(index)) { |
| 350 | i = MP_OBJ_SMALL_INT_VALUE(index); |
| 351 | } else if (!mp_obj_get_int_maybe(index, &i)) { |
Damien George | 1e9a92f | 2014-11-06 17:36:16 +0000 | [diff] [blame] | 352 | if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { |
| 353 | nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, |
| 354 | "indices must be integers")); |
| 355 | } else { |
| 356 | nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, |
Damien George | 044c473 | 2015-04-11 13:03:37 +0100 | [diff] [blame] | 357 | "%q indices must be integers, not %s", |
| 358 | type->name, mp_obj_get_type_str(index))); |
Damien George | 1e9a92f | 2014-11-06 17:36:16 +0000 | [diff] [blame] | 359 | } |
Damien | 660365e | 2013-12-17 18:27:24 +0000 | [diff] [blame] | 360 | } |
xbe | 9e1e8cd | 2014-03-12 22:57:16 -0700 | [diff] [blame] | 361 | |
| 362 | if (i < 0) { |
xbe | c5d70ba | 2014-03-13 00:29:15 -0700 | [diff] [blame] | 363 | i += len; |
xbe | 9e1e8cd | 2014-03-12 22:57:16 -0700 | [diff] [blame] | 364 | } |
| 365 | if (is_slice) { |
xbe | c5d70ba | 2014-03-13 00:29:15 -0700 | [diff] [blame] | 366 | if (i < 0) { |
| 367 | i = 0; |
Damien George | 963a5a3 | 2015-01-16 17:47:07 +0000 | [diff] [blame] | 368 | } else if ((mp_uint_t)i > len) { |
xbe | c5d70ba | 2014-03-13 00:29:15 -0700 | [diff] [blame] | 369 | i = len; |
| 370 | } |
xbe | 9e1e8cd | 2014-03-12 22:57:16 -0700 | [diff] [blame] | 371 | } else { |
Damien George | 963a5a3 | 2015-01-16 17:47:07 +0000 | [diff] [blame] | 372 | if (i < 0 || (mp_uint_t)i >= len) { |
Damien George | 1e9a92f | 2014-11-06 17:36:16 +0000 | [diff] [blame] | 373 | if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { |
| 374 | nlr_raise(mp_obj_new_exception_msg(&mp_type_IndexError, "index out of range")); |
| 375 | } else { |
| 376 | nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_IndexError, |
Damien George | 044c473 | 2015-04-11 13:03:37 +0100 | [diff] [blame] | 377 | "%q index out of range", type->name)); |
Damien George | 1e9a92f | 2014-11-06 17:36:16 +0000 | [diff] [blame] | 378 | } |
xbe | c5d70ba | 2014-03-13 00:29:15 -0700 | [diff] [blame] | 379 | } |
xbe | 9e1e8cd | 2014-03-12 22:57:16 -0700 | [diff] [blame] | 380 | } |
| 381 | return i; |
Damien | 660365e | 2013-12-17 18:27:24 +0000 | [diff] [blame] | 382 | } |
John R. Lenton | 4bee76e | 2014-01-10 11:25:03 +0000 | [diff] [blame] | 383 | |
Damien George | c7687ad | 2014-08-22 21:48:30 +0100 | [diff] [blame] | 384 | mp_obj_t mp_obj_id(mp_obj_t o_in) { |
| 385 | mp_int_t id = (mp_int_t)o_in; |
| 386 | if (!MP_OBJ_IS_OBJ(o_in)) { |
| 387 | return mp_obj_new_int(id); |
| 388 | } else if (id >= 0) { |
| 389 | // Many OSes and CPUs have affinity for putting "user" memories |
| 390 | // into low half of address space, and "system" into upper half. |
| 391 | // We're going to take advantage of that and return small int |
| 392 | // (signed) for such "user" addresses. |
| 393 | return MP_OBJ_NEW_SMALL_INT(id); |
| 394 | } else { |
| 395 | // If that didn't work, well, let's return long int, just as |
| 396 | // a (big) positve value, so it will never clash with the range |
| 397 | // of small int returned in previous case. |
| 398 | return mp_obj_new_int_from_uint((mp_uint_t)id); |
| 399 | } |
| 400 | } |
| 401 | |
Damien George | 4c03b3a | 2014-08-12 18:33:40 +0100 | [diff] [blame] | 402 | // will raise a TypeError if object has no length |
| 403 | mp_obj_t mp_obj_len(mp_obj_t o_in) { |
| 404 | mp_obj_t len = mp_obj_len_maybe(o_in); |
| 405 | if (len == MP_OBJ_NULL) { |
Damien George | 1e9a92f | 2014-11-06 17:36:16 +0000 | [diff] [blame] | 406 | if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { |
| 407 | nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, |
| 408 | "object has no len")); |
| 409 | } else { |
| 410 | nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, |
| 411 | "object of type '%s' has no len()", mp_obj_get_type_str(o_in))); |
| 412 | } |
Damien George | 4c03b3a | 2014-08-12 18:33:40 +0100 | [diff] [blame] | 413 | } else { |
| 414 | return len; |
| 415 | } |
| 416 | } |
| 417 | |
Damien George | eae1644 | 2014-01-11 19:22:29 +0000 | [diff] [blame] | 418 | // may return MP_OBJ_NULL |
John R. Lenton | 4bee76e | 2014-01-10 11:25:03 +0000 | [diff] [blame] | 419 | mp_obj_t mp_obj_len_maybe(mp_obj_t o_in) { |
Paul Sokolovsky | e7f2b4c | 2014-06-13 23:37:18 +0300 | [diff] [blame] | 420 | if ( |
| 421 | #if !MICROPY_PY_BUILTINS_STR_UNICODE |
| 422 | // It's simple - unicode is slow, non-unicode is fast |
| 423 | MP_OBJ_IS_STR(o_in) || |
| 424 | #endif |
| 425 | MP_OBJ_IS_TYPE(o_in, &mp_type_bytes)) { |
Damien George | 5169822 | 2015-09-03 23:01:07 +0100 | [diff] [blame] | 426 | GET_STR_LEN(o_in, l); |
| 427 | return MP_OBJ_NEW_SMALL_INT(l); |
John R. Lenton | 4bee76e | 2014-01-10 11:25:03 +0000 | [diff] [blame] | 428 | } else { |
Paul Sokolovsky | c1d9bbc | 2014-01-30 04:37:19 +0200 | [diff] [blame] | 429 | mp_obj_type_t *type = mp_obj_get_type(o_in); |
| 430 | if (type->unary_op != NULL) { |
Damien George | 6ac5dce | 2014-05-21 19:42:43 +0100 | [diff] [blame] | 431 | return type->unary_op(MP_UNARY_OP_LEN, o_in); |
Damien George | 09a0c64 | 2014-01-30 10:05:33 +0000 | [diff] [blame] | 432 | } else { |
| 433 | return MP_OBJ_NULL; |
Paul Sokolovsky | c1d9bbc | 2014-01-30 04:37:19 +0200 | [diff] [blame] | 434 | } |
John R. Lenton | 4bee76e | 2014-01-10 11:25:03 +0000 | [diff] [blame] | 435 | } |
John R. Lenton | 4bee76e | 2014-01-10 11:25:03 +0000 | [diff] [blame] | 436 | } |
Paul Sokolovsky | dff3f89 | 2014-01-20 18:37:30 +0200 | [diff] [blame] | 437 | |
Damien George | 729f7b4 | 2014-04-17 22:10:53 +0100 | [diff] [blame] | 438 | mp_obj_t mp_obj_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t value) { |
| 439 | mp_obj_type_t *type = mp_obj_get_type(base); |
| 440 | if (type->subscr != NULL) { |
| 441 | mp_obj_t ret = type->subscr(base, index, value); |
Damien George | 6ac5dce | 2014-05-21 19:42:43 +0100 | [diff] [blame] | 442 | if (ret != MP_OBJ_NULL) { |
Damien George | 729f7b4 | 2014-04-17 22:10:53 +0100 | [diff] [blame] | 443 | return ret; |
| 444 | } |
| 445 | // TODO: call base classes here? |
| 446 | } |
| 447 | if (value == MP_OBJ_NULL) { |
Damien George | 1e9a92f | 2014-11-06 17:36:16 +0000 | [diff] [blame] | 448 | if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { |
| 449 | nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, |
| 450 | "object does not support item deletion")); |
| 451 | } else { |
| 452 | nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, |
| 453 | "'%s' object does not support item deletion", mp_obj_get_type_str(base))); |
| 454 | } |
Damien George | 729f7b4 | 2014-04-17 22:10:53 +0100 | [diff] [blame] | 455 | } else if (value == MP_OBJ_SENTINEL) { |
Damien George | 1e9a92f | 2014-11-06 17:36:16 +0000 | [diff] [blame] | 456 | if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { |
| 457 | nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, |
| 458 | "object is not subscriptable")); |
| 459 | } else { |
| 460 | nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, |
| 461 | "'%s' object is not subscriptable", mp_obj_get_type_str(base))); |
| 462 | } |
Damien George | 729f7b4 | 2014-04-17 22:10:53 +0100 | [diff] [blame] | 463 | } else { |
Damien George | 1e9a92f | 2014-11-06 17:36:16 +0000 | [diff] [blame] | 464 | if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { |
| 465 | nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, |
| 466 | "object does not support item assignment")); |
| 467 | } else { |
| 468 | nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, |
| 469 | "'%s' object does not support item assignment", mp_obj_get_type_str(base))); |
| 470 | } |
Damien George | 729f7b4 | 2014-04-17 22:10:53 +0100 | [diff] [blame] | 471 | } |
| 472 | } |
| 473 | |
Paul Sokolovsky | dff3f89 | 2014-01-20 18:37:30 +0200 | [diff] [blame] | 474 | // Return input argument. Useful as .getiter for objects which are |
| 475 | // their own iterators, etc. |
| 476 | mp_obj_t mp_identity(mp_obj_t self) { |
| 477 | return self; |
| 478 | } |
Paul Sokolovsky | 557c9d5 | 2014-02-08 21:57:19 +0200 | [diff] [blame] | 479 | MP_DEFINE_CONST_FUN_OBJ_1(mp_identity_obj, mp_identity); |
Paul Sokolovsky | 3aa8ee7 | 2014-04-09 00:25:28 +0300 | [diff] [blame] | 480 | |
Damien George | 4d91723 | 2014-08-30 14:28:06 +0100 | [diff] [blame] | 481 | bool mp_get_buffer(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags) { |
Damien George | 8a1cab9 | 2014-04-13 12:08:52 +0100 | [diff] [blame] | 482 | mp_obj_type_t *type = mp_obj_get_type(obj); |
| 483 | if (type->buffer_p.get_buffer == NULL) { |
Paul Sokolovsky | 3aa8ee7 | 2014-04-09 00:25:28 +0300 | [diff] [blame] | 484 | return false; |
| 485 | } |
Damien George | b11b85a | 2014-04-18 22:59:24 +0100 | [diff] [blame] | 486 | int ret = type->buffer_p.get_buffer(obj, bufinfo, flags); |
Paul Sokolovsky | 7133d91 | 2014-08-10 11:46:10 +0300 | [diff] [blame] | 487 | if (ret != 0) { |
Paul Sokolovsky | 3aa8ee7 | 2014-04-09 00:25:28 +0300 | [diff] [blame] | 488 | return false; |
| 489 | } |
| 490 | return true; |
| 491 | } |
| 492 | |
Damien George | 4d91723 | 2014-08-30 14:28:06 +0100 | [diff] [blame] | 493 | void mp_get_buffer_raise(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags) { |
Damien George | b11b85a | 2014-04-18 22:59:24 +0100 | [diff] [blame] | 494 | if (!mp_get_buffer(obj, bufinfo, flags)) { |
Damien George | 57a4b4f | 2014-04-18 22:29:21 +0100 | [diff] [blame] | 495 | nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "object with buffer protocol required")); |
Paul Sokolovsky | 3aa8ee7 | 2014-04-09 00:25:28 +0300 | [diff] [blame] | 496 | } |
| 497 | } |
Damien George | c2a4e4e | 2015-05-11 12:25:19 +0000 | [diff] [blame] | 498 | |
| 499 | mp_obj_t mp_generic_unary_op(mp_uint_t op, mp_obj_t o_in) { |
| 500 | switch (op) { |
| 501 | case MP_UNARY_OP_HASH: return MP_OBJ_NEW_SMALL_INT((mp_uint_t)o_in); |
| 502 | default: return MP_OBJ_NULL; // op not supported |
| 503 | } |
| 504 | } |