summaryrefslogtreecommitdiff
path: root/unix/main.c
diff options
context:
space:
mode:
authorScott Shawcroft <scott.shawcroft@gmail.com>2017-06-20 10:56:05 -0700
committerScott Shawcroft <scott.shawcroft@gmail.com>2017-06-20 10:56:05 -0700
commit30ee7019ca651bce1fe966c1089fe977d51dc92b (patch)
tree0114b6f744dd175b7722dd8edcbe24f4ee84bb45 /unix/main.c
parent97fcdfbd08c922efd7470d6482d7c7c703ffc0ae (diff)
parent869cdcfdfc860b1177baf9b0f8818915ba54f3f5 (diff)
Merge tag 'v1.9.1'2.0.0-alpha.0
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
Diffstat (limited to 'unix/main.c')
-rw-r--r--unix/main.c87
1 files changed, 62 insertions, 25 deletions
diff --git a/unix/main.c b/unix/main.c
index f67c257cd..633144c86 100644
--- a/unix/main.c
+++ b/unix/main.c
@@ -35,6 +35,7 @@
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
+#include <signal.h>
#include "py/mpstate.h"
#include "py/nlr.h"
@@ -90,19 +91,33 @@ STATIC int handle_uncaught_exception(mp_obj_base_t *exc) {
return 1;
}
+#define LEX_SRC_STR (1)
+#define LEX_SRC_VSTR (2)
+#define LEX_SRC_FILENAME (3)
+#define LEX_SRC_STDIN (4)
+
// Returns standard error codes: 0 for success, 1 for all other errors,
// except if FORCED_EXIT bit is set then script raised SystemExit and the
// value of the exit is in the lower 8 bits of the return value
-STATIC int execute_from_lexer(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, bool is_repl) {
- if (lex == NULL) {
- printf("MemoryError: lexer could not allocate memory\n");
- return 1;
- }
-
+STATIC int execute_from_lexer(int source_kind, const void *source, mp_parse_input_kind_t input_kind, bool is_repl) {
mp_hal_set_interrupt_char(CHAR_CTRL_C);
nlr_buf_t nlr;
if (nlr_push(&nlr) == 0) {
+ // create lexer based on source kind
+ mp_lexer_t *lex;
+ if (source_kind == LEX_SRC_STR) {
+ const char *line = source;
+ lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, line, strlen(line), false);
+ } else if (source_kind == LEX_SRC_VSTR) {
+ const vstr_t *vstr = source;
+ lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, vstr->buf, vstr->len, false);
+ } else if (source_kind == LEX_SRC_FILENAME) {
+ lex = mp_lexer_new_from_file((const char*)source);
+ } else { // LEX_SRC_STDIN
+ lex = mp_lexer_new_from_fd(MP_QSTR__lt_stdin_gt_, 0, false);
+ }
+
qstr source_name = lex->source_name;
#if MICROPY_PY___FILE__
@@ -240,8 +255,7 @@ STATIC int do_repl(void) {
mp_hal_stdio_mode_orig();
- mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, line.buf, line.len, false);
- ret = execute_from_lexer(lex, parse_input_kind, true);
+ ret = execute_from_lexer(LEX_SRC_VSTR, &line, parse_input_kind, true);
if (ret & FORCED_EXIT) {
return ret;
}
@@ -249,7 +263,7 @@ STATIC int do_repl(void) {
#else
- // use GNU or simple readline
+ // use simple readline
for (;;) {
char *line = prompt(">>> ");
@@ -268,8 +282,7 @@ STATIC int do_repl(void) {
line = line3;
}
- mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, line, strlen(line), false);
- int ret = execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, true);
+ int ret = execute_from_lexer(LEX_SRC_STR, line, MP_PARSE_SINGLE_INPUT, true);
if (ret & FORCED_EXIT) {
return ret;
}
@@ -280,13 +293,11 @@ STATIC int do_repl(void) {
}
STATIC int do_file(const char *file) {
- mp_lexer_t *lex = mp_lexer_new_from_file(file);
- return execute_from_lexer(lex, MP_PARSE_FILE_INPUT, false);
+ return execute_from_lexer(LEX_SRC_FILENAME, file, MP_PARSE_FILE_INPUT, false);
}
STATIC int do_str(const char *str) {
- mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, str, strlen(str), false);
- return execute_from_lexer(lex, MP_PARSE_FILE_INPUT, false);
+ return execute_from_lexer(LEX_SRC_STR, str, MP_PARSE_FILE_INPUT, false);
}
STATIC int usage(char **argv) {
@@ -364,6 +375,10 @@ STATIC void pre_process_options(int argc, char **argv) {
if (word_adjust) {
heap_size = heap_size * BYTES_PER_WORD / 4;
}
+ // If requested size too small, we'll crash anyway
+ if (heap_size < 700) {
+ goto invalid_arg;
+ }
#endif
} else {
invalid_arg:
@@ -404,6 +419,20 @@ int main(int argc, char **argv) {
}
MP_NOINLINE int main_(int argc, char **argv) {
+ #ifdef SIGPIPE
+ // Do not raise SIGPIPE, instead return EPIPE. Otherwise, e.g. writing
+ // to peer-closed socket will lead to sudden termination of MicroPython
+ // process. SIGPIPE is particularly nasty, because unix shell doesn't
+ // print anything for it, so the above looks like completely sudden and
+ // silent termination for unknown reason. Ignoring SIGPIPE is also what
+ // CPython does. Note that this may lead to problems using MicroPython
+ // scripts as pipe filters, but again, that's what CPython does. So,
+ // scripts which want to follow unix shell pipe semantics (where SIGPIPE
+ // means "pipe was requested to terminate, it's not an error"), should
+ // catch EPIPE themselves.
+ signal(SIGPIPE, SIG_IGN);
+ #endif
+
mp_stack_set_limit(40000 * (BYTES_PER_WORD / 4));
pre_process_options(argc, argv);
@@ -415,9 +444,6 @@ MP_NOINLINE int main_(int argc, char **argv) {
mp_init();
- // create keyboard interrupt object
- MP_STATE_VM(keyboard_interrupt_obj) = mp_obj_new_exception(&mp_type_KeyboardInterrupt);
-
char *home = getenv("HOME");
char *path = getenv("MICROPYPATH");
if (path == NULL) {
@@ -427,7 +453,10 @@ MP_NOINLINE int main_(int argc, char **argv) {
path = "~/.micropython/lib:/usr/lib/micropython";
#endif
}
- mp_uint_t path_num = 1; // [0] is for current dir (or base dir of the script)
+ size_t path_num = 1; // [0] is for current dir (or base dir of the script)
+ if (*path == ':') {
+ path_num++;
+ }
for (char *p = path; p != NULL; p = strchr(p, PATHLIST_SEP_CHAR)) {
path_num++;
if (p != NULL) {
@@ -524,6 +553,9 @@ MP_NOINLINE int main_(int argc, char **argv) {
mp_obj_t mod;
nlr_buf_t nlr;
+ bool subpkg_tried = false;
+
+ reimport:
if (nlr_push(&nlr) == 0) {
mod = mp_builtin___import__(MP_ARRAY_SIZE(import_args), import_args);
nlr_pop();
@@ -532,11 +564,17 @@ MP_NOINLINE int main_(int argc, char **argv) {
return handle_uncaught_exception(nlr.ret_val) & 0xff;
}
- if (mp_obj_is_package(mod)) {
- // TODO
- mp_printf(&mp_stderr_print, "%s: -m for packages not yet implemented\n", argv[0]);
- exit(1);
+ if (mp_obj_is_package(mod) && !subpkg_tried) {
+ subpkg_tried = true;
+ vstr_t vstr;
+ int len = strlen(argv[a + 1]);
+ vstr_init(&vstr, len + sizeof(".__main__"));
+ vstr_add_strn(&vstr, argv[a + 1], len);
+ vstr_add_strn(&vstr, ".__main__", sizeof(".__main__") - 1);
+ import_args[0] = mp_obj_new_str_from_vstr(&mp_type_str, &vstr);
+ goto reimport;
}
+
ret = 0;
break;
} else if (strcmp(argv[a], "-X") == 0) {
@@ -582,8 +620,7 @@ MP_NOINLINE int main_(int argc, char **argv) {
ret = do_repl();
prompt_write_history();
} else {
- mp_lexer_t *lex = mp_lexer_new_from_fd(MP_QSTR__lt_stdin_gt_, 0, false);
- ret = execute_from_lexer(lex, MP_PARSE_FILE_INPUT, false);
+ ret = execute_from_lexer(LEX_SRC_STDIN, NULL, MP_PARSE_FILE_INPUT, false);
}
}