Skip to content

Commit

Permalink
Pass script object to start and stop functions
Browse files Browse the repository at this point in the history
  • Loading branch information
magicmq committed Jan 17, 2025
1 parent cc7bed7 commit c7a6a5a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@ public Script(Path path, String name, ScriptOptions options) {
this.options = options;
}

/**
* Prepares this script for execution by initializing its interpreter and logger. Called just prior to executing the script's code.
*/
public void prepare() {
protected void prepare() {
this.interpreter = new PythonInterpreter(null, ScriptUtils.initPySystemState());
this.interpreter.setOut(new PrintStreamWrapper(System.out, this, Level.INFO, "[STDOUT]"));
this.interpreter.setErr(new PrintStreamWrapper(System.err, this, Level.SEVERE, "[STDERR]"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,13 @@ public RunResult loadScript(Script script) throws IOException {
script.getInterpreter().execfile(scriptFileReader, script.getName());

PyObject start = script.getInterpreter().get("start");
if (start instanceof PyFunction)
start.__call__();
if (start instanceof PyFunction startFunction) {
int args = ((PyBaseCode) startFunction.__code__).co_argcount;
if (args == 0)
startFunction.__call__();
else
startFunction.__call__(Py.java2py(script));
}

callScriptLoadEvent(script);

Expand Down Expand Up @@ -500,9 +505,13 @@ private boolean stopScript(Script script, boolean error) {
boolean gracefulStop = true;
if (!error) {
PyObject stop = script.getInterpreter().get("stop");
if (stop instanceof PyFunction) {
if (stop instanceof PyFunction stopFunction) {
try {
stop.__call__();
int args = ((PyBaseCode) stopFunction.__code__).co_argcount;
if (args == 0)
stopFunction.__call__();
else
stopFunction.__call__(Py.java2py(script));
} catch (PyException e) {
handleScriptException(script, e, "Error when calling stop function");
gracefulStop = false;
Expand Down

0 comments on commit c7a6a5a

Please sign in to comment.