From dfc509f1f102cc1bf560b59527ef9a4805819314 Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Wed, 23 Dec 2020 08:09:18 -0800 Subject: [PATCH] Added fatal error messages to main (#929) --- src/main.c | 35 ++++++++++++++++++++++++++------- src/type_conversion/runpython.c | 6 ++++++ src/type_conversion/runpython.h | 8 +------- 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/src/main.c b/src/main.c index 1861b9409..c46c5f930 100644 --- a/src/main.c +++ b/src/main.c @@ -10,6 +10,24 @@ #include "python2js.h" #include "runpython.h" +#define FATAL_ERROR(args...) \ + do { \ + printf("FATAL ERROR: "); \ + printf(args); \ + if (PyErr_Occurred()) { \ + printf("Error was triggered by Python exception:\n"); \ + PyErr_Print(); \ + } \ + } while (0) + +#define TRY_INIT(mod) \ + do { \ + if (mod##_init()) { \ + FATAL_ERROR("Failed to initialize module %s.\n", #mod); \ + return 1; \ + } \ + } while (0) + int main(int argc, char** argv) { @@ -21,24 +39,27 @@ main(int argc, char** argv) // This doesn't seem to work anymore, but I'm keeping it for good measure // anyway The effective way to turn this off is below: setting - // sys.done_write_bytecode = True + // sys.dont_write_bytecode = True setenv("PYTHONDONTWRITEBYTECODE", "1", 0); PyObject* sys = PyImport_ImportModule("sys"); if (sys == NULL) { + FATAL_ERROR("Failed to import sys module."); return 1; } if (PyObject_SetAttrString(sys, "dont_write_bytecode", Py_True)) { + FATAL_ERROR("Failed to set attribute on sys module."); return 1; } Py_DECREF(sys); - if (js2python_init() || JsImport_init() || JsProxy_init() || - pyimport_init() || pyproxy_init() || python2js_init() || - runpython_init_js() || runpython_init_py() || runpython_finalize_js()) { - return 1; - } - + TRY_INIT(js2python); + TRY_INIT(JsImport); + TRY_INIT(JsProxy); + TRY_INIT(pyimport); + TRY_INIT(pyproxy); + TRY_INIT(python2js); + TRY_INIT(runpython); printf("Python initialization complete\n"); emscripten_exit_with_live_runtime(); diff --git a/src/type_conversion/runpython.c b/src/type_conversion/runpython.c index 753a353ab..b3bb1308c 100644 --- a/src/type_conversion/runpython.c +++ b/src/type_conversion/runpython.c @@ -167,3 +167,9 @@ EM_JS(int, runpython_finalize_js, (), { }; return 0; }); + +int +runpython_init() +{ + return runpython_init_js() || runpython_init_py() || runpython_finalize_js(); +} \ No newline at end of file diff --git a/src/type_conversion/runpython.h b/src/type_conversion/runpython.h index 0d2ae6e8a..ce6bedf6a 100644 --- a/src/type_conversion/runpython.h +++ b/src/type_conversion/runpython.h @@ -5,12 +5,6 @@ */ int -runpython_init_js(); - -int -runpython_init_py(); - -int -runpython_finalize_js(); +runpython_init(); #endif /* RUNPYTHON_H */