Search in sources :

Example 1 with DefinePyFunction

use of org.cafebabepy.runtime.module.DefinePyFunction 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);
}
Also used : PyObject(org.cafebabepy.runtime.PyObject) DefinePyFunction(org.cafebabepy.runtime.module.DefinePyFunction)

Example 2 with DefinePyFunction

use of org.cafebabepy.runtime.module.DefinePyFunction 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 3 with DefinePyFunction

use of org.cafebabepy.runtime.module.DefinePyFunction 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 DefinePyFunction

use of org.cafebabepy.runtime.module.DefinePyFunction 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 DefinePyFunction

use of org.cafebabepy.runtime.module.DefinePyFunction 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;
}
Also used : PyObject(org.cafebabepy.runtime.PyObject) DefinePyFunction(org.cafebabepy.runtime.module.DefinePyFunction)

Aggregations

DefinePyFunction (org.cafebabepy.runtime.module.DefinePyFunction)11 PyObject (org.cafebabepy.runtime.PyObject)9 PyIntObject (org.cafebabepy.runtime.object.java.PyIntObject)4 ArrayList (java.util.ArrayList)1 PyRangeIteratorObject (org.cafebabepy.runtime.object.iterator.PyRangeIteratorObject)1 PyListObject (org.cafebabepy.runtime.object.java.PyListObject)1 PyTupleObject (org.cafebabepy.runtime.object.java.PyTupleObject)1 PyMethodTypeObject (org.cafebabepy.runtime.object.proxy.PyMethodTypeObject)1