Search in sources :

Example 1 with PyIntObject

use of org.cafebabepy.runtime.object.java.PyIntObject in project cafebabepy by cafebabepy.

the class PyRangeType method __init__.

@DefinePyFunction(name = __init__)
public void __init__(PyObject self, PyObject... args) {
    if (args.length == 0) {
        throw this.runtime.newRaiseTypeError("range expected 1 arguments, got 0");
    } else if (3 < args.length) {
        throw this.runtime.newRaiseTypeError("range expected at most 3 arguments, got " + args.length);
    }
    PyObject start;
    PyObject stop;
    PyObject step;
    if (args.length == 1) {
        start = this.runtime.number(0);
        stop = getInt(args[0]);
        step = this.runtime.number(1);
    } else if (args.length == 2) {
        start = getInt(args[0]);
        stop = getInt(args[1]);
        step = this.runtime.number(1);
    } else {
        start = getInt(args[0]);
        stop = getInt(args[1]);
        step = getInt(args[2]);
    }
    self.getScope().put("start", start);
    self.getScope().put("stop", stop);
    self.getScope().put("step", step);
    int stepInt = ((PyIntObject) step).getIntValue();
    if (stepInt <= 0) {
        throw this.runtime.newRaiseException("builtins.ValueError", "range() arg 3 must not be zero");
    }
}
Also used : PyObject(org.cafebabepy.runtime.PyObject) PyIntObject(org.cafebabepy.runtime.object.java.PyIntObject) DefinePyFunction(org.cafebabepy.runtime.module.DefinePyFunction)

Example 2 with PyIntObject

use of org.cafebabepy.runtime.object.java.PyIntObject in project cafebabepy by cafebabepy.

the class PyRangeType method getInt.

private PyObject getInt(PyObject object) {
    if (object instanceof PyIntObject) {
        return object;
    }
    PyObject indexMethod = this.runtime.getattrOptional(object, __index__).orElseThrow(() -> this.runtime.newRaiseTypeError("'" + object.getFullName() + "' object cannot be interpreted as an integer"));
    PyObject intObject = indexMethod.call(object);
    if (intObject instanceof PyIntObject) {
        return intObject;
    }
    throw this.runtime.newRaiseTypeError("__index__ returned non-int (type " + intObject.getFullName() + ")");
}
Also used : PyObject(org.cafebabepy.runtime.PyObject) PyIntObject(org.cafebabepy.runtime.object.java.PyIntObject)

Example 3 with PyIntObject

use of org.cafebabepy.runtime.object.java.PyIntObject in project cafebabepy by cafebabepy.

the class PyRangeType method __iter__.

@DefinePyFunction(name = __iter__)
public PyObject __iter__(PyObject self) {
    if (!this.runtime.isInstance(self, this)) {
        throw this.runtime.newRaiseTypeError("descriptor '__iter__' requires a 'range' object but received a '" + self.getType().getFullName() + "'");
    }
    PyObject start = this.runtime.getattr(self, "start");
    PyObject stop = this.runtime.getattr(self, "stop");
    PyObject step = this.runtime.getattr(self, "step");
    int startInt = ((PyIntObject) start).getIntValue();
    int stopInt = ((PyIntObject) stop).getIntValue();
    int stepInt = ((PyIntObject) step).getIntValue();
    return new PyRangeIteratorObject(this.runtime, startInt, stopInt, stepInt);
}
Also used : PyRangeIteratorObject(org.cafebabepy.runtime.object.iterator.PyRangeIteratorObject) PyObject(org.cafebabepy.runtime.PyObject) PyIntObject(org.cafebabepy.runtime.object.java.PyIntObject) DefinePyFunction(org.cafebabepy.runtime.module.DefinePyFunction)

Example 4 with PyIntObject

use of org.cafebabepy.runtime.object.java.PyIntObject in project cafebabepy by cafebabepy.

the class PyTupleType method __getitem__.

@DefinePyFunction(name = __getitem__)
public PyObject __getitem__(PyObject self, PyObject key) {
    if (!(self instanceof PyTupleObject)) {
        throw this.runtime.newRaiseTypeError("descriptor '__getitem__' requires a 'tuple' object but received a '" + self.getType().getFullName() + "'");
    }
    if (!(key instanceof PyIntObject)) {
        throw this.runtime.newRaiseTypeError("tuple indices must be integers or slices, not " + key.getType().getFullName());
    }
    PyTupleObject tuple = (PyTupleObject) self;
    PyIntObject index = (PyIntObject) key;
    return tuple.get(index);
}
Also used : PyTupleObject(org.cafebabepy.runtime.object.java.PyTupleObject) PyIntObject(org.cafebabepy.runtime.object.java.PyIntObject) DefinePyFunction(org.cafebabepy.runtime.module.DefinePyFunction)

Example 5 with PyIntObject

use of org.cafebabepy.runtime.object.java.PyIntObject in project cafebabepy by cafebabepy.

the class PyListType method __getitem__.

@DefinePyFunction(name = __getitem__)
public PyObject __getitem__(PyObject self, PyObject key) {
    if (!(self instanceof PyListObject)) {
        throw this.runtime.newRaiseTypeError("descriptor '__getitem__' requires a 'list' object but received a '" + self.getType().getFullName() + "'");
    }
    if (!(key instanceof PyIntObject)) {
        throw this.runtime.newRaiseTypeError("list indices must be integers or slices, not " + key.getType().getFullName());
    }
    PyListObject list = (PyListObject) self;
    PyIntObject index = (PyIntObject) key;
    return list.get(index);
}
Also used : PyListObject(org.cafebabepy.runtime.object.java.PyListObject) PyIntObject(org.cafebabepy.runtime.object.java.PyIntObject) DefinePyFunction(org.cafebabepy.runtime.module.DefinePyFunction)

Aggregations

PyIntObject (org.cafebabepy.runtime.object.java.PyIntObject)5 DefinePyFunction (org.cafebabepy.runtime.module.DefinePyFunction)4 PyObject (org.cafebabepy.runtime.PyObject)3 PyRangeIteratorObject (org.cafebabepy.runtime.object.iterator.PyRangeIteratorObject)1 PyListObject (org.cafebabepy.runtime.object.java.PyListObject)1 PyTupleObject (org.cafebabepy.runtime.object.java.PyTupleObject)1