Port of MicroPython to the mbed platform. See micropython-repl for an interactive program.

Dependents:   micropython-repl

This a port of MicroPython to the mbed Classic platform.

This provides an interpreter running on the board's USB serial connection.

Getting Started

Import the micropython-repl program into your IDE workspace on developer.mbed.org. Compile and download to your board. Connect to the USB serial port in your usual manner. You should get a startup message similar to the following:

  MicroPython v1.7-155-gdddcdd8 on 2016-04-23; K64F with ARM
  Type "help()" for more information.
  >>>

Then you can start using micropython. For example:

  >>> from mbed import DigitalOut
  >>> from pins import LED1
  >>> led = DigitalOut(LED1)
  >>> led.write(1)

Requirements

You need approximately 100K of flash memory, so this will be no good for boards with smaller amounts of storage.

Caveats

This can be considered an alpha release of the port; things may not work; APIs may change in later releases. It is NOT an official part part the micropython project, so if anything doesn't work, blame me. If it does work, most of the credit is due to micropython.

  • Only a few of the mbed classes are available in micropython so far, and not all methods of those that are.
  • Only a few boards have their full range of pin names available; for others, only a few standard ones (USBTX, USBRX, LED1) are implemented.
  • The garbage collector is not yet implemented. The interpreter will gradually consume memory and then fail.
  • Exceptions from the mbed classes are not yet handled.
  • Asynchronous processing (e.g. events on inputs) is not supported.

Credits

  • Damien P. George and other contributors who created micropython.
  • Colin Hogben, author of this port.
Committer:
Colin Hogben
Date:
Wed Apr 27 22:11:29 2016 +0100
Revision:
10:33521d742af1
Parent:
0:5868e8752d44
Update README and version

Who changed what in which revision?

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