static final MemoryOp getComponentMemoryOp(PyType pyComponentType, CType componentType) { if (pyComponentType.isSubType(ScalarCData.TYPE)) { return componentType.getMemoryOp(); } else if (pyComponentType.isSubType(Structure.TYPE)) { return new MemoryOp.StructOp(pyComponentType); } else { throw Py.TypeError("only scalar and struct types supported"); } } }
/** * isSubType tailored for PySequence binops. * * @param other PyObject * @return true if subclass of other */ protected boolean isSubType(PyObject other) { PyType type = getType(); PyType otherType = other.getType(); return type == otherType || type.isSubType(otherType); }
/** * Determine whether obj is a Python 3 exception class * * @param obj a PyObject * @return true if an exception */ private static boolean isPy3kExceptionClass(PyObject obj) { if (!(obj instanceof PyType)) { return false; } PyType type = ((PyType)obj); if (type.isSubType(PyBaseException.TYPE)) { return true; } return type.getProxyType() != null && Throwable.class.isAssignableFrom(type.getProxyType()); }
protected void checkCallerType(PyType type) { if (type == dtype || type.isSubType(dtype)) { return; } String msg = String.format("descriptor '%s' requires a '%s' object but received a '%s'", name, dtype.fastGetName(), type.fastGetName()); throw Py.TypeError(msg); }
protected void checkGetterType(PyType type) { if (type == dtype || type.isSubType(dtype)) { return; } String msg = String.format("descriptor '%s' for '%s' objects doesn't apply to '%s' object", name, dtype.fastGetName(), type.fastGetName()); throw Py.TypeError(msg); }
Pointer(PyType subtype, PyType pyComponentType, CType componentType) { super(NativeType.POINTER, com.kenai.jffi.Type.POINTER, new MemoryOp.PointerOp(subtype, CType.POINTER)); this.pyComponentType = pyComponentType; this.componentType = componentType; if (pyComponentType.isSubType(ScalarCData.TYPE)) { this.componentMemoryOp = new ScalarOp(componentType.getMemoryOp(), pyComponentType); } else if (pyComponentType.isSubType(Structure.TYPE)) { this.componentMemoryOp = new MemoryOp.StructOp(pyComponentType); } else { throw Py.TypeError("pointer only supported for scalar types"); } }
@Override public void scanToAutoEnable() { PyScriptEngineScope scope = eval("globals()"); List<PyType> processorPyTypes = PROCESSOR_CLASSES.values().stream().map(cls -> (PyType) Py.java2py(cls)).collect(Collectors.toList()); List<Object> autoEnabled = new ArrayList<>(); SpongeUtils.stream(((ScopeIterator) scope.__iter__()).iterator()).forEachOrdered(element -> { String name = element.toString(); PyObject pyObject = scope.__finditem__(name); if (pyObject != null && pyObject instanceof PyType) { PyType pyType = (PyType) pyObject; if (processorPyTypes.stream().filter(processorClass -> !pyType.equals(processorClass) && pyType.isSubType(processorClass)) .findFirst().isPresent()) { if (!isProcessorAbstract(name)) { autoEnabled.add(name); ((JythonKnowledgeBaseEngineOperations) getEngineOperations()).enable(pyType); } } } }); if (logger.isDebugEnabled() && !autoEnabled.isEmpty()) { logger.debug("Auto-enabling: {}", autoEnabled); } }
private static PyObject mapLe(PyObject self, PyObject other) { Set<Object> selfKeys = ((Map<Object, Object>) self.getJavaProxy()).keySet(); if (other.getType().isSubType(PyDictionary.TYPE)) { PyDictionary oDict = (PyDictionary) other; for (Object jkey : selfKeys) { if (!oDict.__contains__(Py.java2py(jkey))) { return Py.False; } } return Py.True; } else { Object oj = other.getJavaProxy(); if (oj instanceof Map) { Map<Object, Object> oMap = (Map<Object, Object>) oj; return Py.newBoolean(oMap.keySet().containsAll(selfKeys)); } else { return null; } } }
@ExposedMethod(doc = BuiltinDocs.type___call___doc) final PyObject type___call__(PyObject[] args, String[] keywords) { PyObject new_ = lookup("__new__"); if (!instantiable || new_ == null) { throw Py.TypeError(String.format("cannot create '%.100s' instances", name)); } PyObject obj = invokeNew(new_, this, true, args, keywords); // When the call was type(something) or the returned object is not an instance of // type, it won't be initialized if ((this == TYPE && args.length == 1 && keywords.length == 0) || !obj.getType().isSubType(this)) { return obj; } obj.dispatch__init__(args, keywords); return obj; }
@ExposedMethod(type = MethodType.BINARY) final synchronized PyObject deque___ne__(PyObject o) { if (!(getType() == o.getType()) && !(getType().isSubType(o.getType()))) { return null; } int tl = __len__(); int ol = o.__len__(); if (tl != ol) { return Py.True; } int i = cmp(this, tl, o, ol); return (i < 0) ? Py.False : Py.True; }
@ExposedMethod(type = MethodType.BINARY) final synchronized PyObject stat_result___eq__(PyObject o) { if (getType() != o.getType() && !getType().isSubType(o.getType())) { return null; } int tl = __len__(); int ol = o.__len__(); if (tl != ol) { return Py.False; } int i = cmp(this, tl, o, ol); return (i < 0) ? Py.True : Py.False; }
@ExposedMethod(type = MethodType.BINARY) final synchronized PyObject struct_time___eq__(PyObject o) { if (getType() != o.getType() && !getType().isSubType(o.getType())) { return null; } int tl = __len__(); int ol = o.__len__(); if (tl != ol) { return Py.False; } int i = cmp(this, tl, o, ol); return (i < 0) ? Py.True : Py.False; }
@ExposedMethod(type = MethodType.BINARY) final synchronized PyObject deque___gt__(PyObject o) { if (!(getType() == o.getType()) && !(getType().isSubType(o.getType()))) { return null; } int i = cmp(this, -1, o, -1); if (i < 0) { return (i == -3) ? Py.True : Py.False; } return __finditem__(i)._gt(o.__finditem__(i)); }
@ExposedMethod(type = MethodType.BINARY) final synchronized PyObject deque___eq__(PyObject o) { if (!(getType() == o.getType()) && !(getType().isSubType(o.getType()))) { return null; } int tl = __len__(); int ol = o.__len__(); if (tl != ol) { return Py.False; } int i = cmp(this, tl, o, ol); return (i < 0) ? Py.True : Py.False; }
@ExposedMethod(type = MethodType.BINARY) final synchronized PyObject deque___le__(PyObject o) { if (!(getType() == o.getType()) && !(getType().isSubType(o.getType()))) { return null; } int i = cmp(this, -1, o, -1); if (i < 0) { return (i == -1 || i == -2) ? Py.True : Py.False; } return __finditem__(i)._le(o.__finditem__(i)); }
@ExposedMethod(type = MethodType.BINARY) final synchronized PyObject deque___lt__(PyObject o) { if (!(getType() == o.getType()) && !(getType().isSubType(o.getType()))) { return null; } int i = cmp(this, -1, o, -1); if (i < 0) { return (i == -1) ? Py.True : Py.False; } return __finditem__(i)._lt(o.__finditem__(i)); }
@ExposedMethod(type = MethodType.BINARY) final synchronized PyObject deque___ge__(PyObject o) { if (!(getType() == o.getType()) && !(getType().isSubType(o.getType()))) { return null; } int i = cmp(this, -1, o, -1); if (i < 0) { return (i == -3 || i == -2) ? Py.True : Py.False; } return __finditem__(i)._ge(o.__finditem__(i)); }
@ExposedNew public static PyObject PointerCData_new(PyNewWrapper new_, boolean init, PyType subtype, PyObject[] args, String[] keywords) { CType.Pointer pointerType = getPointerType(subtype); // No args == create NULL pointer if (args.length == 0) { return new PointerCData(subtype, pointerType, NullMemory.INSTANCE, pointerType.componentMemoryOp); } PyObject value = args[0]; if (value instanceof CData && value.getType().isSubType(pointerType.pyComponentType)) { return new PointerCData(subtype, pointerType, ((CData) value).getReferenceMemory(), pointerType.componentMemoryOp); } else { throw Py.TypeError("expected " + pointerType.pyComponentType.getName() + " instead of " + value.getType().getName()); } }
@Override public PyObject __eq__(PyObject o) { if (getType() != o.getType() && !(getType().isSubType(o.getType()))) { return null; } if (this == o) { return Py.True; } PySlice oSlice = (PySlice)o; return Py.newBoolean(eq(getStart(), oSlice.getStart()) && eq(getStop(), oSlice.getStop()) && eq(getStep(), oSlice.getStep())); }