pyexec.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. /*
  2. * This file is part of the MicroPython 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. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include <stdint.h>
  29. #include <string.h>
  30. #include "py/compile.h"
  31. #include "py/runtime.h"
  32. #include "py/repl.h"
  33. #include "py/gc.h"
  34. #include "py/frozenmod.h"
  35. #include "py/mphal.h"
  36. #if MICROPY_HW_ENABLE_USB
  37. #include "irq.h"
  38. #include "usb.h"
  39. #endif
  40. #include "lib/mp-readline/readline.h"
  41. #include "lib/utils/pyexec.h"
  42. #include "genhdr/mpversion.h"
  43. pyexec_mode_kind_t pyexec_mode_kind = PYEXEC_MODE_FRIENDLY_REPL;
  44. int pyexec_system_exit = 0;
  45. #if MICROPY_REPL_INFO
  46. STATIC bool repl_display_debugging_info = 0;
  47. #endif
  48. #define EXEC_FLAG_PRINT_EOF (1)
  49. #define EXEC_FLAG_ALLOW_DEBUGGING (2)
  50. #define EXEC_FLAG_IS_REPL (4)
  51. #define EXEC_FLAG_SOURCE_IS_RAW_CODE (8)
  52. #define EXEC_FLAG_SOURCE_IS_VSTR (16)
  53. #define EXEC_FLAG_SOURCE_IS_FILENAME (32)
  54. // parses, compiles and executes the code in the lexer
  55. // frees the lexer before returning
  56. // EXEC_FLAG_PRINT_EOF prints 2 EOF chars: 1 after normal output, 1 after exception output
  57. // EXEC_FLAG_ALLOW_DEBUGGING allows debugging info to be printed after executing the code
  58. // EXEC_FLAG_IS_REPL is used for REPL inputs (flag passed on to mp_compile)
  59. STATIC int parse_compile_execute(const void *source, mp_parse_input_kind_t input_kind, int exec_flags) {
  60. int ret = 0;
  61. #if MICROPY_REPL_INFO
  62. uint32_t start = 0;
  63. #endif
  64. // by default a SystemExit exception returns 0
  65. pyexec_system_exit = 0;
  66. nlr_buf_t nlr;
  67. if (nlr_push(&nlr) == 0) {
  68. mp_obj_t module_fun;
  69. #if MICROPY_MODULE_FROZEN_MPY
  70. if (exec_flags & EXEC_FLAG_SOURCE_IS_RAW_CODE) {
  71. // source is a raw_code object, create the function
  72. module_fun = mp_make_function_from_raw_code(source, MP_OBJ_NULL, MP_OBJ_NULL);
  73. } else
  74. #endif
  75. {
  76. #if MICROPY_ENABLE_COMPILER
  77. mp_lexer_t *lex;
  78. if (exec_flags & EXEC_FLAG_SOURCE_IS_VSTR) {
  79. const vstr_t *vstr = source;
  80. lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, vstr->buf, vstr->len, 0);
  81. } else if (exec_flags & EXEC_FLAG_SOURCE_IS_FILENAME) {
  82. lex = mp_lexer_new_from_file(source);
  83. } else {
  84. lex = (mp_lexer_t *)source;
  85. }
  86. // source is a lexer, parse and compile the script
  87. qstr source_name = lex->source_name;
  88. mp_parse_tree_t parse_tree = mp_parse(lex, input_kind);
  89. module_fun = mp_compile(&parse_tree, source_name, exec_flags & EXEC_FLAG_IS_REPL);
  90. #else
  91. mp_raise_msg(&mp_type_RuntimeError, MP_ERROR_TEXT("script compilation not supported"));
  92. #endif
  93. }
  94. // execute code
  95. mp_hal_set_interrupt_char(CHAR_CTRL_C); // allow ctrl-C to interrupt us
  96. #if MICROPY_REPL_INFO
  97. start = mp_hal_ticks_ms();
  98. #endif
  99. mp_call_function_0(module_fun);
  100. mp_hal_set_interrupt_char(-1); // disable interrupt
  101. mp_handle_pending(true); // handle any pending exceptions (and any callbacks)
  102. nlr_pop();
  103. ret = 1;
  104. if (exec_flags & EXEC_FLAG_PRINT_EOF) {
  105. mp_hal_stdout_tx_strn("\x04", 1);
  106. }
  107. } else {
  108. // uncaught exception
  109. mp_hal_set_interrupt_char(-1); // disable interrupt
  110. mp_handle_pending(false); // clear any pending exceptions (and run any callbacks)
  111. // print EOF after normal output
  112. if (exec_flags & EXEC_FLAG_PRINT_EOF) {
  113. mp_hal_stdout_tx_strn("\x04", 1);
  114. }
  115. // check for SystemExit
  116. if (mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(((mp_obj_base_t *)nlr.ret_val)->type), MP_OBJ_FROM_PTR(&mp_type_SystemExit))) {
  117. // at the moment, the value of SystemExit is unused
  118. ret = pyexec_system_exit;
  119. } else {
  120. mp_obj_print_exception(&mp_plat_print, MP_OBJ_FROM_PTR(nlr.ret_val));
  121. ret = 0;
  122. }
  123. }
  124. #if MICROPY_REPL_INFO
  125. // display debugging info if wanted
  126. if ((exec_flags & EXEC_FLAG_ALLOW_DEBUGGING) && repl_display_debugging_info) {
  127. mp_uint_t ticks = mp_hal_ticks_ms() - start; // TODO implement a function that does this properly
  128. printf("took " UINT_FMT " ms\n", ticks);
  129. // qstr info
  130. {
  131. size_t n_pool, n_qstr, n_str_data_bytes, n_total_bytes;
  132. qstr_pool_info(&n_pool, &n_qstr, &n_str_data_bytes, &n_total_bytes);
  133. printf("qstr:\n n_pool=%u\n n_qstr=%u\n "
  134. "n_str_data_bytes=%u\n n_total_bytes=%u\n",
  135. (unsigned)n_pool, (unsigned)n_qstr, (unsigned)n_str_data_bytes, (unsigned)n_total_bytes);
  136. }
  137. #if MICROPY_ENABLE_GC
  138. // run collection and print GC info
  139. gc_collect();
  140. gc_dump_info();
  141. #endif
  142. }
  143. #endif
  144. if (exec_flags & EXEC_FLAG_PRINT_EOF) {
  145. mp_hal_stdout_tx_strn("\x04", 1);
  146. }
  147. return ret;
  148. }
  149. #if MICROPY_ENABLE_COMPILER
  150. #if MICROPY_REPL_EVENT_DRIVEN
  151. typedef struct _repl_t {
  152. // This structure originally also held current REPL line,
  153. // but it was moved to MP_STATE_VM(repl_line) as containing
  154. // root pointer. Still keep structure in case more state
  155. // will be added later.
  156. // vstr_t line;
  157. bool cont_line;
  158. bool paste_mode;
  159. } repl_t;
  160. repl_t repl;
  161. STATIC int pyexec_raw_repl_process_char(int c);
  162. STATIC int pyexec_friendly_repl_process_char(int c);
  163. void pyexec_event_repl_init(void) {
  164. MP_STATE_VM(repl_line) = vstr_new(32);
  165. repl.cont_line = false;
  166. repl.paste_mode = false;
  167. // no prompt before printing friendly REPL banner or entering raw REPL
  168. readline_init(MP_STATE_VM(repl_line), "");
  169. if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) {
  170. pyexec_raw_repl_process_char(CHAR_CTRL_A);
  171. } else {
  172. pyexec_friendly_repl_process_char(CHAR_CTRL_B);
  173. }
  174. }
  175. STATIC int pyexec_raw_repl_process_char(int c) {
  176. if (c == CHAR_CTRL_A) {
  177. // reset raw REPL
  178. mp_hal_stdout_tx_str("raw REPL; CTRL-B to exit\r\n");
  179. goto reset;
  180. } else if (c == CHAR_CTRL_B) {
  181. // change to friendly REPL
  182. pyexec_mode_kind = PYEXEC_MODE_FRIENDLY_REPL;
  183. vstr_reset(MP_STATE_VM(repl_line));
  184. repl.cont_line = false;
  185. repl.paste_mode = false;
  186. pyexec_friendly_repl_process_char(CHAR_CTRL_B);
  187. return 0;
  188. } else if (c == CHAR_CTRL_C) {
  189. // clear line
  190. vstr_reset(MP_STATE_VM(repl_line));
  191. return 0;
  192. } else if (c == CHAR_CTRL_D) {
  193. // input finished
  194. } else {
  195. // let through any other raw 8-bit value
  196. vstr_add_byte(MP_STATE_VM(repl_line), c);
  197. return 0;
  198. }
  199. // indicate reception of command
  200. mp_hal_stdout_tx_str("OK");
  201. if (MP_STATE_VM(repl_line)->len == 0) {
  202. // exit for a soft reset
  203. mp_hal_stdout_tx_str("\r\n");
  204. vstr_clear(MP_STATE_VM(repl_line));
  205. return PYEXEC_FORCED_EXIT;
  206. }
  207. int ret = parse_compile_execute(MP_STATE_VM(repl_line), MP_PARSE_FILE_INPUT, EXEC_FLAG_PRINT_EOF | EXEC_FLAG_SOURCE_IS_VSTR);
  208. if (ret & PYEXEC_FORCED_EXIT) {
  209. return ret;
  210. }
  211. reset:
  212. vstr_reset(MP_STATE_VM(repl_line));
  213. mp_hal_stdout_tx_str(">");
  214. return 0;
  215. }
  216. STATIC int pyexec_friendly_repl_process_char(int c) {
  217. if (repl.paste_mode) {
  218. if (c == CHAR_CTRL_C) {
  219. // cancel everything
  220. mp_hal_stdout_tx_str("\r\n");
  221. goto input_restart;
  222. } else if (c == CHAR_CTRL_D) {
  223. // end of input
  224. mp_hal_stdout_tx_str("\r\n");
  225. int ret = parse_compile_execute(MP_STATE_VM(repl_line), MP_PARSE_FILE_INPUT, EXEC_FLAG_ALLOW_DEBUGGING | EXEC_FLAG_IS_REPL | EXEC_FLAG_SOURCE_IS_VSTR);
  226. if (ret & PYEXEC_FORCED_EXIT) {
  227. return ret;
  228. }
  229. goto input_restart;
  230. } else {
  231. // add char to buffer and echo
  232. vstr_add_byte(MP_STATE_VM(repl_line), c);
  233. if (c == '\r') {
  234. mp_hal_stdout_tx_str("\r\n=== ");
  235. } else {
  236. char buf[1] = {c};
  237. mp_hal_stdout_tx_strn(buf, 1);
  238. }
  239. return 0;
  240. }
  241. }
  242. int ret = readline_process_char(c);
  243. if (!repl.cont_line) {
  244. if (ret == CHAR_CTRL_A) {
  245. // change to raw REPL
  246. pyexec_mode_kind = PYEXEC_MODE_RAW_REPL;
  247. mp_hal_stdout_tx_str("\r\n");
  248. pyexec_raw_repl_process_char(CHAR_CTRL_A);
  249. return 0;
  250. } else if (ret == CHAR_CTRL_B) {
  251. // reset friendly REPL
  252. mp_hal_stdout_tx_str("\r\n");
  253. mp_hal_stdout_tx_str("MicroPython " MICROPY_GIT_TAG " on " MICROPY_BUILD_DATE "; " MICROPY_HW_BOARD_NAME " with " MICROPY_HW_MCU_NAME "\r\n");
  254. #if MICROPY_PY_BUILTINS_HELP
  255. mp_hal_stdout_tx_str("Type \"help()\" for more information.\r\n");
  256. #endif
  257. goto input_restart;
  258. } else if (ret == CHAR_CTRL_C) {
  259. // break
  260. mp_hal_stdout_tx_str("\r\n");
  261. goto input_restart;
  262. } else if (ret == CHAR_CTRL_D) {
  263. // exit for a soft reset
  264. mp_hal_stdout_tx_str("\r\n");
  265. vstr_clear(MP_STATE_VM(repl_line));
  266. return PYEXEC_FORCED_EXIT;
  267. } else if (ret == CHAR_CTRL_E) {
  268. // paste mode
  269. mp_hal_stdout_tx_str("\r\npaste mode; Ctrl-C to cancel, Ctrl-D to finish\r\n=== ");
  270. vstr_reset(MP_STATE_VM(repl_line));
  271. repl.paste_mode = true;
  272. return 0;
  273. }
  274. if (ret < 0) {
  275. return 0;
  276. }
  277. if (!mp_repl_continue_with_input(vstr_null_terminated_str(MP_STATE_VM(repl_line)))) {
  278. goto exec;
  279. }
  280. vstr_add_byte(MP_STATE_VM(repl_line), '\n');
  281. repl.cont_line = true;
  282. readline_note_newline("... ");
  283. return 0;
  284. } else {
  285. if (ret == CHAR_CTRL_C) {
  286. // cancel everything
  287. mp_hal_stdout_tx_str("\r\n");
  288. repl.cont_line = false;
  289. goto input_restart;
  290. } else if (ret == CHAR_CTRL_D) {
  291. // stop entering compound statement
  292. goto exec;
  293. }
  294. if (ret < 0) {
  295. return 0;
  296. }
  297. if (mp_repl_continue_with_input(vstr_null_terminated_str(MP_STATE_VM(repl_line)))) {
  298. vstr_add_byte(MP_STATE_VM(repl_line), '\n');
  299. readline_note_newline("... ");
  300. return 0;
  301. }
  302. exec:;
  303. int ret = parse_compile_execute(MP_STATE_VM(repl_line), MP_PARSE_SINGLE_INPUT, EXEC_FLAG_ALLOW_DEBUGGING | EXEC_FLAG_IS_REPL | EXEC_FLAG_SOURCE_IS_VSTR);
  304. if (ret & PYEXEC_FORCED_EXIT) {
  305. return ret;
  306. }
  307. input_restart:
  308. vstr_reset(MP_STATE_VM(repl_line));
  309. repl.cont_line = false;
  310. repl.paste_mode = false;
  311. readline_init(MP_STATE_VM(repl_line), ">>> ");
  312. return 0;
  313. }
  314. }
  315. uint8_t pyexec_repl_active;
  316. int pyexec_event_repl_process_char(int c) {
  317. pyexec_repl_active = 1;
  318. int res;
  319. if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) {
  320. res = pyexec_raw_repl_process_char(c);
  321. } else {
  322. res = pyexec_friendly_repl_process_char(c);
  323. }
  324. pyexec_repl_active = 0;
  325. return res;
  326. }
  327. #else // MICROPY_REPL_EVENT_DRIVEN
  328. int pyexec_raw_repl(void) {
  329. vstr_t line;
  330. vstr_init(&line, 32);
  331. raw_repl_reset:
  332. mp_hal_stdout_tx_str("raw REPL; CTRL-B to exit\r\n");
  333. for (;;) {
  334. vstr_reset(&line);
  335. mp_hal_stdout_tx_str(">");
  336. for (;;) {
  337. int c = mp_hal_stdin_rx_chr();
  338. if (c == CHAR_CTRL_A) {
  339. // reset raw REPL
  340. goto raw_repl_reset;
  341. } else if (c == CHAR_CTRL_B) {
  342. // change to friendly REPL
  343. mp_hal_stdout_tx_str("\r\n");
  344. vstr_clear(&line);
  345. pyexec_mode_kind = PYEXEC_MODE_FRIENDLY_REPL;
  346. return 0;
  347. } else if (c == CHAR_CTRL_C) {
  348. // clear line
  349. vstr_reset(&line);
  350. } else if (c == CHAR_CTRL_D) {
  351. // input finished
  352. break;
  353. } else {
  354. // let through any other raw 8-bit value
  355. vstr_add_byte(&line, c);
  356. }
  357. }
  358. // indicate reception of command
  359. mp_hal_stdout_tx_str("OK");
  360. if (line.len == 0) {
  361. // exit for a soft reset
  362. mp_hal_stdout_tx_str("\r\n");
  363. vstr_clear(&line);
  364. return PYEXEC_FORCED_EXIT;
  365. }
  366. int ret = parse_compile_execute(&line, MP_PARSE_FILE_INPUT, EXEC_FLAG_PRINT_EOF | EXEC_FLAG_SOURCE_IS_VSTR);
  367. if (ret & PYEXEC_FORCED_EXIT) {
  368. return ret;
  369. }
  370. }
  371. }
  372. int pyexec_friendly_repl(void) {
  373. vstr_t line;
  374. vstr_init(&line, 32);
  375. #if defined(USE_HOST_MODE) && MICROPY_HW_HAS_LCD
  376. // in host mode, we enable the LCD for the repl
  377. mp_obj_t lcd_o = mp_call_function_0(mp_load_name(qstr_from_str("LCD")));
  378. mp_call_function_1(mp_load_attr(lcd_o, qstr_from_str("light")), mp_const_true);
  379. #endif
  380. friendly_repl_reset:
  381. mp_hal_stdout_tx_str("MicroPython " MICROPY_GIT_TAG " on " MICROPY_BUILD_DATE "; " MICROPY_HW_BOARD_NAME " with " MICROPY_HW_MCU_NAME "\r\n");
  382. #if MICROPY_PY_BUILTINS_HELP
  383. mp_hal_stdout_tx_str("Type \"help()\" for more information.\r\n");
  384. #endif
  385. // to test ctrl-C
  386. /*
  387. {
  388. uint32_t x[4] = {0x424242, 0xdeaddead, 0x242424, 0xdeadbeef};
  389. for (;;) {
  390. nlr_buf_t nlr;
  391. printf("pyexec_repl: %p\n", x);
  392. mp_hal_set_interrupt_char(CHAR_CTRL_C);
  393. if (nlr_push(&nlr) == 0) {
  394. for (;;) {
  395. }
  396. } else {
  397. printf("break\n");
  398. }
  399. }
  400. }
  401. */
  402. for (;;) {
  403. input_restart:
  404. #if MICROPY_HW_ENABLE_USB
  405. if (usb_vcp_is_enabled()) {
  406. // If the user gets to here and interrupts are disabled then
  407. // they'll never see the prompt, traceback etc. The USB REPL needs
  408. // interrupts to be enabled or no transfers occur. So we try to
  409. // do the user a favor and reenable interrupts.
  410. if (query_irq() == IRQ_STATE_DISABLED) {
  411. enable_irq(IRQ_STATE_ENABLED);
  412. mp_hal_stdout_tx_str("MPY: enabling IRQs\r\n");
  413. }
  414. }
  415. #endif
  416. // If the GC is locked at this point there is no way out except a reset,
  417. // so force the GC to be unlocked to help the user debug what went wrong.
  418. if (MP_STATE_MEM(gc_lock_depth) != 0) {
  419. MP_STATE_MEM(gc_lock_depth) = 0;
  420. }
  421. vstr_reset(&line);
  422. int ret = readline(&line, ">>> ");
  423. mp_parse_input_kind_t parse_input_kind = MP_PARSE_SINGLE_INPUT;
  424. if (ret == CHAR_CTRL_A) {
  425. // change to raw REPL
  426. mp_hal_stdout_tx_str("\r\n");
  427. vstr_clear(&line);
  428. pyexec_mode_kind = PYEXEC_MODE_RAW_REPL;
  429. return 0;
  430. } else if (ret == CHAR_CTRL_B) {
  431. // reset friendly REPL
  432. mp_hal_stdout_tx_str("\r\n");
  433. goto friendly_repl_reset;
  434. } else if (ret == CHAR_CTRL_C) {
  435. // break
  436. mp_hal_stdout_tx_str("\r\n");
  437. continue;
  438. } else if (ret == CHAR_CTRL_D) {
  439. // exit for a soft reset
  440. mp_hal_stdout_tx_str("\r\n");
  441. vstr_clear(&line);
  442. return PYEXEC_FORCED_EXIT;
  443. } else if (ret == CHAR_CTRL_E) {
  444. // paste mode
  445. mp_hal_stdout_tx_str("\r\npaste mode; Ctrl-C to cancel, Ctrl-D to finish\r\n=== ");
  446. vstr_reset(&line);
  447. for (;;) {
  448. char c = mp_hal_stdin_rx_chr();
  449. if (c == CHAR_CTRL_C) {
  450. // cancel everything
  451. mp_hal_stdout_tx_str("\r\n");
  452. goto input_restart;
  453. } else if (c == CHAR_CTRL_D) {
  454. // end of input
  455. mp_hal_stdout_tx_str("\r\n");
  456. break;
  457. } else {
  458. // add char to buffer and echo
  459. vstr_add_byte(&line, c);
  460. if (c == '\r') {
  461. mp_hal_stdout_tx_str("\r\n=== ");
  462. } else {
  463. mp_hal_stdout_tx_strn(&c, 1);
  464. }
  465. }
  466. }
  467. parse_input_kind = MP_PARSE_FILE_INPUT;
  468. } else if (vstr_len(&line) == 0) {
  469. continue;
  470. } else {
  471. // got a line with non-zero length, see if it needs continuing
  472. while (mp_repl_continue_with_input(vstr_null_terminated_str(&line))) {
  473. vstr_add_byte(&line, '\n');
  474. ret = readline(&line, "... ");
  475. if (ret == CHAR_CTRL_C) {
  476. // cancel everything
  477. mp_hal_stdout_tx_str("\r\n");
  478. goto input_restart;
  479. } else if (ret == CHAR_CTRL_D) {
  480. // stop entering compound statement
  481. break;
  482. }
  483. }
  484. }
  485. ret = parse_compile_execute(&line, parse_input_kind, EXEC_FLAG_ALLOW_DEBUGGING | EXEC_FLAG_IS_REPL | EXEC_FLAG_SOURCE_IS_VSTR);
  486. if (ret & PYEXEC_FORCED_EXIT) {
  487. return ret;
  488. }
  489. }
  490. }
  491. #endif // MICROPY_REPL_EVENT_DRIVEN
  492. #endif // MICROPY_ENABLE_COMPILER
  493. int pyexec_file(const char *filename) {
  494. return parse_compile_execute(filename, MP_PARSE_FILE_INPUT, EXEC_FLAG_SOURCE_IS_FILENAME);
  495. }
  496. int pyexec_file_if_exists(const char *filename) {
  497. #if MICROPY_MODULE_FROZEN
  498. if (mp_frozen_stat(filename) == MP_IMPORT_STAT_FILE) {
  499. return pyexec_frozen_module(filename);
  500. }
  501. #endif
  502. if (mp_import_stat(filename) != MP_IMPORT_STAT_FILE) {
  503. return 1; // success (no file is the same as an empty file executing without fail)
  504. }
  505. return pyexec_file(filename);
  506. }
  507. #if MICROPY_MODULE_FROZEN
  508. int pyexec_frozen_module(const char *name) {
  509. void *frozen_data;
  510. int frozen_type = mp_find_frozen_module(name, strlen(name), &frozen_data);
  511. switch (frozen_type) {
  512. #if MICROPY_MODULE_FROZEN_STR
  513. case MP_FROZEN_STR:
  514. return parse_compile_execute(frozen_data, MP_PARSE_FILE_INPUT, 0);
  515. #endif
  516. #if MICROPY_MODULE_FROZEN_MPY
  517. case MP_FROZEN_MPY:
  518. return parse_compile_execute(frozen_data, MP_PARSE_FILE_INPUT, EXEC_FLAG_SOURCE_IS_RAW_CODE);
  519. #endif
  520. default:
  521. printf("could not find module '%s'\n", name);
  522. return false;
  523. }
  524. }
  525. #endif
  526. #if MICROPY_REPL_INFO
  527. mp_obj_t pyb_set_repl_info(mp_obj_t o_value) {
  528. repl_display_debugging_info = mp_obj_get_int(o_value);
  529. return mp_const_none;
  530. }
  531. MP_DEFINE_CONST_FUN_OBJ_1(pyb_set_repl_info_obj, pyb_set_repl_info);
  532. #endif