use of org.python.core.PyObject in project gda-core by openGDA.
the class JythonSyntaxChecker method apply.
@Override
public SyntaxState apply(String source) {
logger.trace("Checking {}", source);
PyObject code = compilePython(source);
if (code == null) {
return INVALID;
} else if (Py.None.equals(code)) {
return INCOMPLETE;
} else {
return COMPLETE;
}
}
use of org.python.core.PyObject in project gda-core by openGDA.
the class DummyScannableTest method test__getitem__.
@Test
public void test__getitem__() {
PyObject item = theScannable.__getitem__(new PyInteger(0));
assertEquals(item.toString(), "10.0");
}
use of org.python.core.PyObject in project gda-core by openGDA.
the class GdaBuiltin method boxVarargs.
/**
* If there are more arguments than expected, wrap all extra arguments into a single
* array to represent the varargs argument of a Java method. The types of the extra
* arguments are checked later so do not matter at this stage.
* <br>
* This is a modified version of the private function
* {@code ReflectedArgs#ensureBNoxedVarargs}.
* <br>
* There is still some ambiguity if the type of the varargs is itself an iterable
* and the final arg passed is a list. For now, this assumes that the final arg is
* the wrapped vararg and returns it as reveived.
*
* @param args All the arguments passed to the function
* @param length The number of arguments expected (including varargs array as 1)
* @return An array of objects matching the number expected - may be the original array
*/
private PyObject[] boxVarargs(PyObject[] args, int length) {
if (args.length == 0) {
// if length is > 1, this will still fail later but not our problem
return new PyObject[] { new PyList() };
}
PyObject lastArg = args[args.length - 1];
if (args.length == length && lastArg instanceof PySequenceList || lastArg instanceof PyArray || lastArg instanceof PyXRange || lastArg instanceof PyIterator) {
// will be boxed in an array once __tojava__ is called
return args;
}
int positionals = length - 1;
if (args.length < positionals) {
return args;
}
var boxedArgs = new PyObject[length];
System.arraycopy(args, 0, boxedArgs, 0, positionals);
int others = args.length - positionals;
var varargs = new PyObject[others];
System.arraycopy(args, positionals, varargs, 0, others);
boxedArgs[positionals] = new PyList(varargs);
return boxedArgs;
}
use of org.python.core.PyObject in project gda-core by openGDA.
the class GDAInteractiveConsole method showexception.
/**
* Show syntax error messages in the Terminal This is to work around an apparent bug in jython. showexception will
* print to the terminal for ordinary stuff, just not PySyntaxErrors.
* log as error as it is an exception
* Only show cause as a single line on teh Jython console
*/
@Override
public void showexception(PyException arg0) {
if (arg0.match(Py.SyntaxError) || arg0.match(Py.IndentationError)) {
if (logger.isTraceEnabled())
super.showexception(arg0);
InterfaceProvider.getTerminalPrinter().print(arg0.toString());
} else if (arg0.match(Py.KeyboardInterrupt)) {
InterfaceProvider.getTerminalPrinter().print("KeyboardInterrupt");
} else if (arg0.match(Py.SystemExit)) {
// super.showexception handles SystemExit as a special case and shuts down the JVM
blockExit();
} else {
InterfaceProvider.getTerminalPrinter().print("Error time: " + Instant.now());
if (arg0.type instanceof PyJavaType) {
// we cut it down to the jython stack trace and the exception message (and class)
if (arg0.traceback != null) {
// Occasionally exceptions have no traceback - see DAQ-3628
InterfaceProvider.getTerminalPrinter().print(arg0.traceback.dumpStack());
}
PyObject value = arg0.value;
Throwable throwable = (Throwable) value.__tojava__(Throwable.class);
InterfaceProvider.getTerminalPrinter().print(throwable.getClass().getName() + ": " + throwable.getMessage());
} else {
super.showexception(arg0);
}
}
// always log as error. It is up to the log config to decide how much of the exception is to be shown
// If a PyException getMessage returns null. toString returns the stack which is not what we want as we pass the whole stack in the throwable to the
// error method
// Sometimes the cause is null
String msg;
if (arg0.getMessage() == null) {
if (arg0.getCause() == null)
msg = arg0.toString();
else
msg = arg0.getCause().getMessage();
} else
msg = arg0.getMessage();
if (arg0.getCause() instanceof ScanInterruptedException) {
logger.info("InteractiveConsole exception: " + msg);
} else {
logger.error("InteractiveConsole exception: " + msg, arg0);
}
}
use of org.python.core.PyObject in project gda-core by openGDA.
the class GDAJythonInterpreter method evaluate.
/**
* Runs a Jython command which returns some output. As the Jython engine is in a distributed environment, only
* strings are returned. Object references will also be converted to strings.
*
* @param command
* String - must be python code - cannot run import javaclass - this results in fixParseError - unknown
* source
* @return String
*/
protected String evaluate(String command) {
String output = null;
try {
command = translator.translate(command);
PyObject result = interactiveConsole.eval(command);
output = result.toString();
} catch (PyException e) {
logger.error("Error evaluating command: {}", command, PythonException.from(e));
}
return output;
}
Aggregations