/** * Initializes a default PythonInterpreter and runs the code loaded from the * {@link CodeBootstrap} as __main__ Called by the code generated in * {@link org.python.compiler.Module#addMain()} */ public static void runMain(CodeBootstrap main, String[] args) throws Exception { PySystemState.initialize(null, null, args, main.getClass().getClassLoader()); try { imp.createFromCode("__main__", CodeLoader.loadCode(main)); } catch (PyException e) { Py.getSystemState().callExitFunc(); if (e.match(Py.SystemExit)) { return; } throw e; } Py.getSystemState().callExitFunc(); } //XXX: this needs review to make sure we are cutting out all of the Java
static void maybeSystemExit(PyException exc) { if (exc.match(Py.SystemExit)) { PyObject value = exc.value; if (PyException.isExceptionInstance(exc.value)) { value = value.__findattr__("code"); } Py.getSystemState().callExitFunc(); if (value instanceof PyInteger) { System.exit(((PyInteger) value).getValue()); } else { if (value != Py.None) { try { Py.println(value); System.exit(1); } catch (Throwable t) { // continue } } System.exit(0); } } } public static PyObject StopIteration;
public void cleanup() { setSystemState(); PySystemState sys = Py.getSystemState(); sys.callExitFunc(); try { sys.stdout.invoke("flush"); } catch (PyException pye) { // fall through } try { sys.stderr.invoke("flush"); } catch (PyException pye) { // fall through } threadLocals.remove(); sys.cleanup(); }