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");
}
}
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() + ")");
}
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);
}
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);
}
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);
}
Aggregations