main_method = UtilityCode(
impl = """
int main(int argc, char** argv) {
- int r;
+ int r = 0;
+ PyObject* m = NULL;
Py_Initialize();
PySys_SetArgv(argc, argv);
#if PY_MAJOR_VERSION < 3
init%(module_name)s();
#else
- PyInit_%(module_name)s(name);
+ m = PyInit_%(module_name)s(name);
#endif
- r = PyErr_Occurred() != NULL;
+ if (PyErr_Occurred() != NULL) {
+ r = 1;
+ PyErr_Print(); /* This exits with the right code if SystemExit. */
+ if (Py_FlushLine()); PyErr_Clear();
+ }
+ Py_XDECREF(m);
Py_Finalize();
return r;
}
-""")
\ No newline at end of file
+""")