use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.
the class PyObjectType method __setattr__.
@DefinePyFunction(name = __setattr__)
public void __setattr__(PyObject self, PyObject name, PyObject value) {
PyObject strType = this.runtime.typeOrThrow("builtins.str");
if (!this.runtime.isInstance(self, strType)) {
throw this.runtime.newRaiseTypeError("attribute name must be string, not '" + name.getFullName() + "'");
}
self.getScope().put(name.toJava(String.class), value);
}
use of org.cafebabepy.runtime.PyObject 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.PyObject 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.PyObject 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.PyObject in project cafebabepy by cafebabepy.
the class PyTypeType method __call__.
@DefinePyFunction(name = __call__)
public PyObject __call__(PyObject self, PyObject... args) {
PyObject object = this.runtime.getattr(self, __new__).call();
this.runtime.getattr(object, __init__).call(args);
return object;
}
Aggregations