Search in sources :

Example 11 with PyObject

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;
    }
}
Also used : PyObject(org.python.core.PyObject)

Example 12 with PyObject

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");
}
Also used : PyInteger(org.python.core.PyInteger) PyObject(org.python.core.PyObject) Test(org.junit.Test)

Example 13 with PyObject

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;
}
Also used : PySequenceList(org.python.core.PySequenceList) PyXRange(org.python.core.PyXRange) PyIterator(org.python.core.PyIterator) PyArray(org.python.core.PyArray) PyList(org.python.core.PyList) PyObject(org.python.core.PyObject)

Example 14 with PyObject

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);
    }
}
Also used : PyString(org.python.core.PyString) PyObject(org.python.core.PyObject) PyJavaType(org.python.core.PyJavaType) ScanInterruptedException(gda.scan.ScanInterruptedException)

Example 15 with PyObject

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;
}
Also used : PyException(org.python.core.PyException) PyString(org.python.core.PyString) PyObject(org.python.core.PyObject)

Aggregations

PyObject (org.python.core.PyObject)61 PyString (org.python.core.PyString)20 PyException (org.python.core.PyException)11 IOException (java.io.IOException)8 PyList (org.python.core.PyList)8 HashMap (java.util.HashMap)7 PyInteger (org.python.core.PyInteger)7 Serializable (java.io.Serializable)6 UnsupportedEncodingException (java.io.UnsupportedEncodingException)6 PyFloat (org.python.core.PyFloat)6 PythonInterpreter (org.python.util.PythonInterpreter)6 ArrayList (java.util.ArrayList)5 Test (org.junit.Test)5 PyLong (org.python.core.PyLong)5 PyNone (org.python.core.PyNone)5 PyStringMap (org.python.core.PyStringMap)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 InputStream (java.io.InputStream)4 List (java.util.List)4 File (java.io.File)3