pyexec.c 20 KB

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