Skip to content

Commit

Permalink
Added fatal error messages to main (pyodide#929)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hood Chatham authored and joemarshall committed Jan 3, 2021
1 parent 141714b commit bdee37f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 14 deletions.
35 changes: 28 additions & 7 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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();
Expand Down
6 changes: 6 additions & 0 deletions src/type_conversion/runpython.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
8 changes: 1 addition & 7 deletions src/type_conversion/runpython.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@
*/

int
runpython_init_js();

int
runpython_init_py();

int
runpython_finalize_js();
runpython_init();

#endif /* RUNPYTHON_H */

0 comments on commit bdee37f

Please sign in to comment.