use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.
the class PyRangeIteratorObject method next.
public final PyObject next() {
if (hasNext()) {
int number = this.start + this.step * this.i;
PyObject result = this.runtime.number(number);
this.i++;
return result;
} else {
throw this.runtime.newRaiseException("builtins.StopIteration");
}
}
use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.
the class PyJavaFunctionObject method call.
@Override
public PyObject call(PyObject... args) {
PyObject target = this.targetType;
Class<?> methodClass = this.method.getDeclaringClass();
for (PyObject type : target.getTypes()) {
Class<?> typeClass = type.getClass();
if (methodClass.isAssignableFrom(typeClass)) {
target = type;
break;
}
}
try {
Object[] compArgs;
Class<?>[] paramClasses = this.method.getParameterTypes();
if (paramClasses.length == 0) {
compArgs = new Object[0];
} else {
if (paramClasses.length == 1 && paramClasses[0].isArray()) {
compArgs = new Object[] { args };
} else if (paramClasses[paramClasses.length - 1].isArray()) {
// Split argument and variable argument from args array
if (args.length > 0) {
if (args.length - paramClasses.length >= 0) {
// Last argument is variable argument
compArgs = new Object[paramClasses.length];
PyObject[] lastArrayArg = new PyObject[args.length - compArgs.length + 1];
System.arraycopy(args, 0, compArgs, 0, compArgs.length - 1);
for (int i = 0; i < lastArrayArg.length; i++) {
lastArrayArg[i] = args[i + compArgs.length - 1];
}
compArgs[paramClasses.length - 1] = lastArrayArg;
} else {
compArgs = new Object[args.length + 1];
for (int i = 0; i < args.length; i++) {
if (args[i].getClass().isArray()) {
compArgs[i] = new PyObject[] { args[i] };
} else {
compArgs[i] = args[i];
}
}
compArgs[compArgs.length - 1] = new PyObject[0];
}
} else {
compArgs = new Object[] { new PyObject[0] };
}
} else if (paramClasses.length != args.length) {
throw this.runtime.newRaiseException("builtins.TypeError", this.name + "() takes at most " + paramClasses.length + " arguments (" + args.length + " given)");
} else {
compArgs = new Object[args.length];
for (int i = 0; i < args.length; i++) {
Class<?> paramClass = args[i].getClass();
if (paramClass.isArray()) {
compArgs[i] = new PyObject[] { args[i] };
} else {
compArgs[i] = args[i];
}
}
}
}
Object result = this.method.invoke(target, compArgs);
if (result == null) {
return this.runtime.None();
} else if (result instanceof PyObject) {
return (PyObject) result;
} else {
// FIXME Java object to Python object
throw new UnsupportedOperationException("FIXME Java object to Python object !!!!!!!!");
}
} catch (IllegalAccessException | IllegalArgumentException e) {
// FIXME CPython message???
throw new CafeBabePyException("Not accessible method " + this.method.getDeclaringClass().getName() + "#" + this.method.getName() + Arrays.stream(this.method.getParameterTypes()).map(Class::getName).collect(Collectors.joining(", ", " (", ")")), e);
} catch (InvocationTargetException e) {
Throwable targetException = e.getTargetException();
if (targetException instanceof RuntimeException) {
throw (RuntimeException) targetException;
}
throw new CafeBabePyException(targetException);
}
}
use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.
the class PyObjectType method __getattribute__.
@DefinePyFunction(name = __getattribute__)
public PyObject __getattribute__(PyObject self, PyObject name) {
String n = name.toJava(String.class);
Optional<PyObject> resultOpt = self.getScope().get(n);
if (resultOpt.isPresent()) {
return resultOpt.get();
}
boolean isParent = false;
resultOpt = getFromTypes(self, n);
if (!resultOpt.isPresent()) {
resultOpt = getFromType(self, n);
if (!resultOpt.isPresent()) {
resultOpt = getFromParent(self, n);
if (!resultOpt.isPresent()) {
if (self.isModule()) {
throw this.runtime.newRaiseException("builtins.AttributeError", "module '" + self.getName() + "' has no attribute '" + name + "'");
} else if (self.isType()) {
throw this.runtime.newRaiseException("builtins.AttributeError", "type object '" + self.getName() + "' has no attribute '" + name + "'");
} else {
throw this.runtime.newRaiseException("builtins.AttributeError", "'" + self.getName() + "' object has no attribute '" + name + "'");
}
} else {
// Module
isParent = true;
}
}
}
PyObject result = resultOpt.get();
if (!result.isCallable() || isParent) {
return result;
} else {
synchronized (this) {
// if (this.methodMap == null) {
// this.methodMap = new LinkedHashMap<>();
// }
// PyObject method = this.methodMap.get(name);
PyObject method = null;
if (method == null) {
// method = newPyObject("types.MethodType", result, object);
method = new PyMethodTypeObject(this.runtime, self, result);
// this.methodMap.put(name, method);
}
return method;
}
}
}
use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.
the class PyObjectType method getFromParent.
public Optional<PyObject> getFromParent(PyObject object, String name) {
Optional<PyObjectScope> parentOpt = object.getScope().getParent();
while (parentOpt.isPresent()) {
PyObjectScope parent = parentOpt.get();
Optional<PyObject> result = parent.get(name);
if (result.isPresent()) {
return result;
}
parentOpt = parent.getParent();
}
return Optional.empty();
}
use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.
the class PySuperType method __getattribute__.
@DefinePyFunction(name = __getattribute__)
public PyObject __getattribute__(PyObject self, PyObject name) {
PyObject strType = this.runtime.typeOrThrow("builtins.str");
if (!this.runtime.isInstance(name, strType)) {
throw this.runtime.newRaiseTypeError("attribute name must be string, not '" + name.getFullName() + "'");
}
PyObject object = self.getScope().get("_proxy_object", false).orElseThrow(() -> getRuntime().newRaiseException("builtins.AttributeError", "'" + self.getFullName() + "' object has no attribute '_proxy_object'"));
PyObject selfProxy;
List<PyObject> types = object.getType().getTypes();
if (types.size() == 1) {
selfProxy = types.get(0);
} else {
selfProxy = types.get(1);
}
return this.runtime.getattr(selfProxy, name.toJava(String.class));
}
Aggregations