1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-26 01:18:28 +00:00

core/emulator: assume -m main when no arguments are provided

fixes #1115
This commit is contained in:
matejcik 2020-07-24 15:42:44 +02:00 committed by Pavol Rusnak
parent f4c9d0a040
commit 8502412dbc
4 changed files with 52 additions and 39 deletions

View File

@ -7,6 +7,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
## 2.3.3 [to be released on 2nd September 2020]
### Added
- Running the frozen version of the emulator doesn't need arguments. [#1115]
### Changed
- Print inverted question mark for non-printable characters.
@ -252,4 +253,5 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
[#1074]: https://github.com/trezor/trezor-firmware/issues/1074
[#1089]: https://github.com/trezor/trezor-firmware/issues/1089
[#1098]: https://github.com/trezor/trezor-firmware/issues/1098
[#1115]: https://github.com/trezor/trezor-firmware/issues/1115
[#1126]: https://github.com/trezor/trezor-firmware/issues/1126

View File

@ -427,6 +427,46 @@ void main_clean_exit(int status) {
#define PATHLIST_SEP_CHAR ':'
#endif
static int do_import_module(const char *modname) {
mp_obj_t import_args[4];
import_args[0] = mp_obj_new_str(modname, strlen(modname));
import_args[1] = import_args[2] = mp_const_none;
// Ask __import__ to handle imported module specially - set its __name__
// to __main__, and also return this leaf module, not top-level package
// containing it.
import_args[3] = mp_const_false;
// TODO: https://docs.python.org/3/using/cmdline.html#cmdoption-m :
// "the first element of sys.argv will be the full path to
// the module file (while the module file is being located,
// the first element will be set to "-m")."
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();
} else {
// uncaught exception
exit(handle_uncaught_exception(nlr.ret_val) & 0xff);
}
if (mp_obj_is_package(mod) && !subpkg_tried) {
subpkg_tried = true;
vstr_t vstr;
int len = strlen(modname);
vstr_init(&vstr, len + sizeof(".__main__"));
vstr_add_strn(&vstr, modname, len);
vstr_add_strn(&vstr, ".__main__", sizeof(".__main__") - 1);
import_args[0] = mp_obj_new_str_from_vstr(&mp_type_str, &vstr);
goto reimport;
}
return 0;
}
MP_NOINLINE int main_(int argc, char **argv);
int main(int argc, char **argv) {
@ -555,9 +595,11 @@ MP_NOINLINE int main_(int argc, char **argv) {
const int NOTHING_EXECUTED = -2;
int ret = NOTHING_EXECUTED;
bool inspect = false;
bool default_import = true;
for (int a = 1; a < argc; a++) {
if (argv[a][0] == '-') {
if (strcmp(argv[a], "-i") == 0) {
default_import = false;
inspect = true;
} else if (strcmp(argv[a], "-c") == 0) {
if (a + 1 >= argc) {
@ -572,44 +614,9 @@ MP_NOINLINE int main_(int argc, char **argv) {
if (a + 1 >= argc) {
return usage(argv);
}
mp_obj_t import_args[4];
import_args[0] = mp_obj_new_str(argv[a + 1], strlen(argv[a + 1]));
import_args[1] = import_args[2] = mp_const_none;
// Ask __import__ to handle imported module specially - set its __name__
// to __main__, and also return this leaf module, not top-level package
// containing it.
import_args[3] = mp_const_false;
// TODO: https://docs.python.org/3/using/cmdline.html#cmdoption-m :
// "the first element of sys.argv will be the full path to
// the module file (while the module file is being located,
// the first element will be set to "-m")."
default_import = false;
set_sys_argv(argv, argc, a + 1);
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();
} else {
// uncaught exception
return handle_uncaught_exception(nlr.ret_val) & 0xff;
}
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;
ret = do_import_module(argv[a + 1]);
break;
} else if (strcmp(argv[a], "-X") == 0) {
a += 1;
@ -651,6 +658,10 @@ MP_NOINLINE int main_(int argc, char **argv) {
}
}
if (ret == NOTHING_EXECUTED && default_import) {
ret = do_import_module("main");
}
if (ret == NOTHING_EXECUTED || inspect) {
if (isatty(0)) {
prompt_read_history();

View File

@ -13,4 +13,4 @@ else
cd "src${TREZOR_MODEL}"
fi
../build/unix/micropython -O$PYOPT -X heapsize=20M -m main
../build/unix/micropython -O$PYOPT -X heapsize=20M

View File

@ -34,7 +34,7 @@ on the device gets wiped on every start in this firmware.
Build of Core into UNIX emulator. Something you can run on your laptop.
Frozen version. That means you do not need any other files to run it, it is just
a single binary file. Run it using `micropython -m main`.
a single binary file that you can execute directly.
See [Emulator](../core/emulator/index.md) for more info.