Search in sources :

Example 1 with PyXRange

use of org.python.core.PyXRange 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)

Aggregations

PyArray (org.python.core.PyArray)1 PyIterator (org.python.core.PyIterator)1 PyList (org.python.core.PyList)1 PyObject (org.python.core.PyObject)1 PySequenceList (org.python.core.PySequenceList)1 PyXRange (org.python.core.PyXRange)1