@Override public String getPythonClassName(PyObject pyObject) { return pyObject.getType().getName(); }
public String getPythonClassName(PyObject pyObject) { return pyObject.getType().getFullName(); }
public boolean __nonzero__() { PyType self_type=getType(); PyObject impl=self_type.lookup("__nonzero__"); if (impl==null) { impl=self_type.lookup("__len__"); if (impl==null) return super.__nonzero__(); } PyObject o=impl.__get__(this,self_type).__call__(); Class c=o.getClass(); if (c!=PyInteger.class&&c!=PyBoolean.class) { throw Py.TypeError(String.format("__nonzero__ should return bool or int, returned %s",self_type.getName())); } return o.__nonzero__(); }
static final CType.Builtin getScalarType(PyType subtype) { PyObject jffi_type = subtype.__getattr__("_jffi_type"); if (!(jffi_type instanceof CType.Builtin)) { throw Py.TypeError("invalid _jffi_type for " + subtype.getName()); } return (CType.Builtin) jffi_type; }
public boolean __nonzero__() { PyType self_type=getType(); PyObject impl=self_type.lookup("__nonzero__"); if (impl==null) { impl=self_type.lookup("__len__"); if (impl==null) return super.__nonzero__(); } PyObject o=impl.__get__(this,self_type).__call__(); Class c=o.getClass(); if (c!=PyInteger.class&&c!=PyBoolean.class) { throw Py.TypeError(String.format("__nonzero__ should return bool or int, returned %s",self_type.getName())); } return o.__nonzero__(); }
if (((PyType)newBases[i]).isSubType(this)) { throw Py.TypeError("a __bases__ item causes an inheritance cycle"); PyType newBase = best_base(newBases); base.compatibleForAssignment(newBase, "__bases__"); PyObject[] savedBases = bases; PyType savedBase = base; bases = newBases; base = newBase; mro_internal(); mro_subclasses(savedSubMros); for (PyObject saved : savedBases) { if (saved instanceof PyType) { ((PyType)saved).detachSubclass(this); ((PyType)newb).attachSubclass(this); throw t; postSetattr("__getattribute__");
@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()); } }
/** * Determine if the binary op on types t1 and t2 is an add * operation dealing with a str/unicode and a str/unicode * subclass. * * This operation is special cased in _binop_rule to match * CPython's handling; CPython uses tp_as_number and * tp_as_sequence to allow string/unicode subclasses to override * the left side's __add__ when that left side is an actual str or * unicode object (see test_concat_jy for examples). * * @param t1 left side PyType * @param t2 right side PyType * @param op the binary operation's String * @return true if this is a special case */ private boolean isStrUnicodeSpecialCase(PyType t1, PyType t2, String op) { // XXX: We may need to generalize this rule to apply to other // situations // XXX: This method isn't expensive but could (and maybe // should?) be optimized for worst case scenarios return (op == "+") && (t1 == PyString.TYPE || t1 == PyUnicode.TYPE) && (t2.isSubType(PyString.TYPE) || t2.isSubType(PyUnicode.TYPE)); }
private void mro_internal() { if (getType() == TYPE) { mro = computeMro(); } else { PyObject mroDescr = getType().lookup("mro"); if (mroDescr == null) { throw Py.AttributeError("mro"); } PyObject[] result = Py.make_array(mroDescr.__get__(null, getType()).__call__(this)); PyType solid = solid_base(this); for (PyObject cls : result) { if (cls instanceof PyClass) { continue; } if (!(cls instanceof PyType)) { throw Py.TypeError(String.format("mro() returned a non-class ('%.500s')", cls.getType().fastGetName())); } PyType t = (PyType)cls; if (!solid.isSubType(solid_base(t))) { throw Py.TypeError(String.format("mro() returned base with unsuitable layout " + "('%.500s')", t.fastGetName())); } } mro = result; } }
public static PyObject classobj___new__(PyObject name, PyObject bases, PyObject dict) { if (!name.getType().isSubType(PyString.TYPE)) { throw Py.TypeError("PyClass_New: name must be a string"); } if (!(dict instanceof PyStringMap || dict instanceof PyDictionary)) { throw Py.TypeError("PyClass_New: dict must be a dictionary"); } PyType.ensureDoc(dict); PyType.ensureModule(dict); if (!(bases instanceof PyTuple)) { throw Py.TypeError("PyClass_New: bases must be a tuple"); } PyTuple basesTuple = (PyTuple)bases; for (PyObject base : basesTuple.getArray()) { if (!(base instanceof PyClass)) { if (base.getType().isCallable()) { return base.getType().__call__(name, bases, dict); } else { throw Py.TypeError("PyClass_New: base must be a class"); } } } PyClass klass = new PyClass(); klass.__name__ = name.toString(); klass.__bases__ = basesTuple; klass.__dict__ = dict; klass.cacheDescriptors(); return klass; }
@Override public final String toString() { return String.format("<ctypes.Array elem_type=%s length=%d>", pyComponentType.toString(), length); }
public String toString() { PyType self_type=getType(); PyObject impl=self_type.lookup("__repr__"); if (impl!=null) { PyObject res=impl.__get__(this,self_type).__call__(); if (!(res instanceof PyString)) throw Py.TypeError("__repr__ returned non-string (type "+res.getType().fastGetName()+")"); return((PyString)res).toString(); } return super.toString(); }
@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); } }
/** * Ensures that the physical layout between this type and <code>other</code> are compatible. * Raises a TypeError if not. */ public void compatibleForAssignment(PyType other, String attribute) { if (!getLayout().equals(other.getLayout()) || needs_userdict != other.needs_userdict || needs_finalizer != other.needs_finalizer) { throw Py.TypeError(String.format("%s assignment: '%s' object layout differs from '%s'", attribute, other.fastGetName(), fastGetName())); } }
/** * 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()); }
public PyObject __call__(PyObject[] args, String[] keywords) { int nargs = args.length; if (nargs < 1 || nargs == keywords.length) { throw Py.TypeError(for_type.fastGetName() + ".__new__(): not enough arguments"); } PyObject arg0 = args[0]; if (!(arg0 instanceof PyType)) { throw Py.TypeError(for_type.fastGetName() + ".__new__(X): X is not a type object (" + arg0.getType().fastGetName() + ")"); } PyType subtype = (PyType)arg0; if (!subtype.isSubType(for_type)) { throw Py.TypeError(for_type.fastGetName() + ".__new__(" + subtype.fastGetName() + "): " + subtype.fastGetName() + " is not a subtype of " + for_type.fastGetName()); } if (subtype.getStatic() != for_type) { throw Py.TypeError(for_type.fastGetName() + ".__new__(" + subtype.fastGetName() + ") is not safe, use " + subtype.fastGetName() + ".__new__()"); } PyObject[] rest = new PyObject[nargs - 1]; System.arraycopy(args, 1, rest, 0, nargs - 1); return new_impl(false, subtype, rest, keywords); }
@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; }
throw Py.TypeError("invalid self argument to constructor"); Class<?> javaClass = self.getType().getProxyType(); if (javaClass == null) { throw Py.TypeError("self invalid - must be a Java subclass [self=" + self + "]"); return PyType.fromClass(javaClass).lookup("__init__").__call__(self, args, keywords);
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); }