30ee7019ca
Fixes for stmhal USB mass storage, lwIP bindings and VFS regressions This release provides an important fix for the USB mass storage device in the stmhal port by implementing the SCSI SYNCHRONIZE_CACHE command, which is now require by some Operating Systems. There are also fixes for the lwIP bindings to improve non-blocking sockets and error codes. The VFS has some regressions fixed including the ability to statvfs the root. All changes are listed below. py core: - modbuiltins: add core-provided version of input() function - objstr: catch case of negative "maxsplit" arg to str.rsplit() - persistentcode: allow to compile with complex numbers disabled - objstr: allow to compile with obj-repr D, and unicode disabled - modsys: allow to compile with obj-repr D and PY_ATTRTUPLE disabled - provide mp_decode_uint_skip() to help reduce stack usage - makeqstrdefs.py: make script run correctly with Python 2.6 - objstringio: if created from immutable object, follow copy on write policy extmod: - modlwip: connect: for non-blocking mode, return EINPROGRESS - modlwip: fix error codes for duplicate calls to connect() - modlwip: accept: fix error code for non-blocking mode - vfs: allow to statvfs the root directory - vfs: allow "buffering" and "encoding" args to VFS's open() - modframebuf: fix signed/unsigned comparison pendantic warning lib: - libm: use isfinite instead of finitef, for C99 compatibility - utils/interrupt_char: remove support for KBD_EXCEPTION disabled tests: - basics/string_rsplit: add tests for negative "maxsplit" argument - float: convert "sys.exit()" to "raise SystemExit" - float/builtin_float_minmax: PEP8 fixes - basics: convert "sys.exit()" to "raise SystemExit" - convert remaining "sys.exit()" to "raise SystemExit" unix port: - convert to use core-provided version of built-in import() - Makefile: replace references to make with $(MAKE) windows port: - convert to use core-provided version of built-in import() qemu-arm port: - Makefile: adjust object-file lists to get correct dependencies - enable micropython.mem_*() functions to allow more tests stmhal port: - boards: enable DAC for NUCLEO_F767ZI board - add support for NUCLEO_F446RE board - pass USB handler as parameter to allow more than one USB handler - usb: use local USB handler variable in Start-of-Frame handler - usb: make state for USB device private to top-level USB driver - usbdev: for MSC implement SCSI SYNCHRONIZE_CACHE command - convert from using stmhal's input() to core provided version cc3200 port: - convert from using stmhal's input() to core provided version teensy port: - convert from using stmhal's input() to core provided version esp8266 port: - Makefile: replace references to make with $(MAKE) - Makefile: add clean-modules target - convert from using stmhal's input() to core provided version zephyr port: - modusocket: getaddrinfo: Fix mp_obj_len() usage - define MICROPY_PY_SYS_PLATFORM (to "zephyr") - machine_pin: use native Zephyr types for Zephyr API calls docs: - machine.Pin: remove out_value() method - machine.Pin: add on() and off() methods - esp8266: consistently replace Pin.high/low methods with .on/off - esp8266/quickref: polish Pin.on()/off() examples - network: move confusingly-named cc3200 Server class to its reference - uos: deconditionalize, remove minor port-specific details - uos: move cc3200 port legacy VFS mounting functions to its ref doc - machine: sort machine classes in logical order, not alphabetically - network: first step to describe standard network class interface examples: - embedding: use core-provided KeyboardInterrupt object
292 lines
9.1 KiB
C
292 lines
9.1 KiB
C
/*
|
|
* This file is part of the Micro Python project, http://micropython.org/
|
|
*
|
|
* The MIT License (MIT)
|
|
*
|
|
* Copyright (c) 2013, 2014 Damien P. George
|
|
* Copyright (c) 2014 Paul Sokolovsky
|
|
*
|
|
* 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.
|
|
*/
|
|
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include "py/nlr.h"
|
|
#include "py/smallint.h"
|
|
#include "py/objint.h"
|
|
#include "py/runtime0.h"
|
|
#include "py/runtime.h"
|
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
|
#include <math.h>
|
|
#endif
|
|
|
|
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_LONGLONG
|
|
|
|
#if MICROPY_PY_SYS_MAXSIZE
|
|
// Export value for sys.maxsize
|
|
const mp_obj_int_t mp_maxsize_obj = {{&mp_type_int}, MP_SSIZE_MAX};
|
|
#endif
|
|
|
|
mp_obj_t mp_obj_int_from_bytes_impl(bool big_endian, size_t len, const byte *buf) {
|
|
int delta = 1;
|
|
if (!big_endian) {
|
|
buf += len - 1;
|
|
delta = -1;
|
|
}
|
|
|
|
mp_longint_impl_t value = 0;
|
|
for (; len--; buf += delta) {
|
|
value = (value << 8) | *buf;
|
|
}
|
|
return mp_obj_new_int_from_ll(value);
|
|
}
|
|
|
|
void mp_obj_int_to_bytes_impl(mp_obj_t self_in, bool big_endian, size_t len, byte *buf) {
|
|
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_int));
|
|
mp_obj_int_t *self = self_in;
|
|
long long val = self->val;
|
|
if (big_endian) {
|
|
byte *b = buf + len;
|
|
while (b > buf) {
|
|
*--b = val;
|
|
val >>= 8;
|
|
}
|
|
} else {
|
|
for (; len > 0; --len) {
|
|
*buf++ = val;
|
|
val >>= 8;
|
|
}
|
|
}
|
|
}
|
|
|
|
int mp_obj_int_sign(mp_obj_t self_in) {
|
|
mp_longint_impl_t val;
|
|
if (MP_OBJ_IS_SMALL_INT(self_in)) {
|
|
val = MP_OBJ_SMALL_INT_VALUE(self_in);
|
|
} else {
|
|
mp_obj_int_t *self = self_in;
|
|
val = self->val;
|
|
}
|
|
if (val < 0) {
|
|
return -1;
|
|
} else if (val > 0) {
|
|
return 1;
|
|
} else {
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
// This must handle int and bool types, and must raise a
|
|
// TypeError if the argument is not integral
|
|
mp_obj_t mp_obj_int_abs(mp_obj_t self_in) {
|
|
if (MP_OBJ_IS_TYPE(self_in, &mp_type_int)) {
|
|
mp_obj_int_t *self = self_in;
|
|
self = mp_obj_new_int_from_ll(self->val);
|
|
if (self->val < 0) {
|
|
// TODO could overflow long long
|
|
self->val = -self->val;
|
|
}
|
|
return self;
|
|
} else {
|
|
mp_int_t val = mp_obj_get_int(self_in);
|
|
if (val == MP_SMALL_INT_MIN) {
|
|
return mp_obj_new_int_from_ll(-val);
|
|
} else {
|
|
if (val < 0) {
|
|
val = -val;
|
|
}
|
|
return MP_OBJ_NEW_SMALL_INT(val);
|
|
}
|
|
}
|
|
}
|
|
|
|
mp_obj_t mp_obj_int_unary_op(mp_uint_t op, mp_obj_t o_in) {
|
|
mp_obj_int_t *o = o_in;
|
|
switch (op) {
|
|
case MP_UNARY_OP_BOOL: return mp_obj_new_bool(o->val != 0);
|
|
|
|
// truncate value to fit in mp_int_t, which gives the same hash as
|
|
// small int if the value fits without truncation
|
|
case MP_UNARY_OP_HASH: return MP_OBJ_NEW_SMALL_INT((mp_int_t)o->val);
|
|
|
|
case MP_UNARY_OP_POSITIVE: return o_in;
|
|
case MP_UNARY_OP_NEGATIVE: return mp_obj_new_int_from_ll(-o->val);
|
|
case MP_UNARY_OP_INVERT: return mp_obj_new_int_from_ll(~o->val);
|
|
default: return MP_OBJ_NULL; // op not supported
|
|
}
|
|
}
|
|
|
|
mp_obj_t mp_obj_int_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
|
|
long long lhs_val;
|
|
long long rhs_val;
|
|
|
|
if (MP_OBJ_IS_SMALL_INT(lhs_in)) {
|
|
lhs_val = MP_OBJ_SMALL_INT_VALUE(lhs_in);
|
|
} else if (MP_OBJ_IS_TYPE(lhs_in, &mp_type_int)) {
|
|
lhs_val = ((mp_obj_int_t*)lhs_in)->val;
|
|
} else {
|
|
return MP_OBJ_NULL; // op not supported
|
|
}
|
|
|
|
if (MP_OBJ_IS_SMALL_INT(rhs_in)) {
|
|
rhs_val = MP_OBJ_SMALL_INT_VALUE(rhs_in);
|
|
} else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_int)) {
|
|
rhs_val = ((mp_obj_int_t*)rhs_in)->val;
|
|
} else {
|
|
// delegate to generic function to check for extra cases
|
|
return mp_obj_int_binary_op_extra_cases(op, lhs_in, rhs_in);
|
|
}
|
|
|
|
switch (op) {
|
|
case MP_BINARY_OP_ADD:
|
|
case MP_BINARY_OP_INPLACE_ADD:
|
|
return mp_obj_new_int_from_ll(lhs_val + rhs_val);
|
|
case MP_BINARY_OP_SUBTRACT:
|
|
case MP_BINARY_OP_INPLACE_SUBTRACT:
|
|
return mp_obj_new_int_from_ll(lhs_val - rhs_val);
|
|
case MP_BINARY_OP_MULTIPLY:
|
|
case MP_BINARY_OP_INPLACE_MULTIPLY:
|
|
return mp_obj_new_int_from_ll(lhs_val * rhs_val);
|
|
case MP_BINARY_OP_FLOOR_DIVIDE:
|
|
case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE:
|
|
return mp_obj_new_int_from_ll(lhs_val / rhs_val);
|
|
case MP_BINARY_OP_MODULO:
|
|
case MP_BINARY_OP_INPLACE_MODULO:
|
|
return mp_obj_new_int_from_ll(lhs_val % rhs_val);
|
|
|
|
case MP_BINARY_OP_AND:
|
|
case MP_BINARY_OP_INPLACE_AND:
|
|
return mp_obj_new_int_from_ll(lhs_val & rhs_val);
|
|
case MP_BINARY_OP_OR:
|
|
case MP_BINARY_OP_INPLACE_OR:
|
|
return mp_obj_new_int_from_ll(lhs_val | rhs_val);
|
|
case MP_BINARY_OP_XOR:
|
|
case MP_BINARY_OP_INPLACE_XOR:
|
|
return mp_obj_new_int_from_ll(lhs_val ^ rhs_val);
|
|
|
|
case MP_BINARY_OP_LSHIFT:
|
|
case MP_BINARY_OP_INPLACE_LSHIFT:
|
|
return mp_obj_new_int_from_ll(lhs_val << (int)rhs_val);
|
|
case MP_BINARY_OP_RSHIFT:
|
|
case MP_BINARY_OP_INPLACE_RSHIFT:
|
|
return mp_obj_new_int_from_ll(lhs_val >> (int)rhs_val);
|
|
|
|
case MP_BINARY_OP_POWER:
|
|
case MP_BINARY_OP_INPLACE_POWER: {
|
|
long long ans = 1;
|
|
while (rhs_val > 0) {
|
|
if (rhs_val & 1) {
|
|
ans *= lhs_val;
|
|
}
|
|
if (rhs_val == 1) {
|
|
break;
|
|
}
|
|
rhs_val /= 2;
|
|
lhs_val *= lhs_val;
|
|
}
|
|
return mp_obj_new_int_from_ll(ans);
|
|
}
|
|
|
|
case MP_BINARY_OP_LESS:
|
|
return mp_obj_new_bool(lhs_val < rhs_val);
|
|
case MP_BINARY_OP_MORE:
|
|
return mp_obj_new_bool(lhs_val > rhs_val);
|
|
case MP_BINARY_OP_LESS_EQUAL:
|
|
return mp_obj_new_bool(lhs_val <= rhs_val);
|
|
case MP_BINARY_OP_MORE_EQUAL:
|
|
return mp_obj_new_bool(lhs_val >= rhs_val);
|
|
case MP_BINARY_OP_EQUAL:
|
|
return mp_obj_new_bool(lhs_val == rhs_val);
|
|
|
|
default:
|
|
return MP_OBJ_NULL; // op not supported
|
|
}
|
|
}
|
|
|
|
mp_obj_t mp_obj_new_int(mp_int_t value) {
|
|
if (MP_SMALL_INT_FITS(value)) {
|
|
return MP_OBJ_NEW_SMALL_INT(value);
|
|
}
|
|
return mp_obj_new_int_from_ll(value);
|
|
}
|
|
|
|
mp_obj_t mp_obj_new_int_from_uint(mp_uint_t value) {
|
|
// SMALL_INT accepts only signed numbers, so make sure the input
|
|
// value fits completely in the small-int positive range.
|
|
if ((value & ~MP_SMALL_INT_POSITIVE_MASK) == 0) {
|
|
return MP_OBJ_NEW_SMALL_INT(value);
|
|
}
|
|
return mp_obj_new_int_from_ll(value);
|
|
}
|
|
|
|
mp_obj_t mp_obj_new_int_from_ll(long long val) {
|
|
mp_obj_int_t *o = m_new_obj(mp_obj_int_t);
|
|
o->base.type = &mp_type_int;
|
|
o->val = val;
|
|
return o;
|
|
}
|
|
|
|
mp_obj_t mp_obj_new_int_from_ull(unsigned long long val) {
|
|
// TODO raise an exception if the unsigned long long won't fit
|
|
if (val >> (sizeof(unsigned long long) * 8 - 1) != 0) {
|
|
mp_raise_msg(&mp_type_OverflowError, "ulonglong too large"));
|
|
}
|
|
mp_obj_int_t *o = m_new_obj(mp_obj_int_t);
|
|
o->base.type = &mp_type_int;
|
|
o->val = val;
|
|
return o;
|
|
}
|
|
|
|
mp_obj_t mp_obj_new_int_from_str_len(const char **str, size_t len, bool neg, unsigned int base) {
|
|
// TODO this does not honor the given length of the string, but it all cases it should anyway be null terminated
|
|
// TODO check overflow
|
|
mp_obj_int_t *o = m_new_obj(mp_obj_int_t);
|
|
o->base.type = &mp_type_int;
|
|
char *endptr;
|
|
o->val = strtoll(*str, &endptr, base);
|
|
*str = endptr;
|
|
return o;
|
|
}
|
|
|
|
mp_int_t mp_obj_int_get_truncated(mp_const_obj_t self_in) {
|
|
if (MP_OBJ_IS_SMALL_INT(self_in)) {
|
|
return MP_OBJ_SMALL_INT_VALUE(self_in);
|
|
} else {
|
|
const mp_obj_int_t *self = self_in;
|
|
return self->val;
|
|
}
|
|
}
|
|
|
|
mp_int_t mp_obj_int_get_checked(mp_const_obj_t self_in) {
|
|
// TODO: Check overflow
|
|
return mp_obj_int_get_truncated(self_in);
|
|
}
|
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
|
mp_float_t mp_obj_int_as_float_impl(mp_obj_t self_in) {
|
|
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_int));
|
|
mp_obj_int_t *self = self_in;
|
|
return self->val;
|
|
}
|
|
#endif
|
|
|
|
#endif
|