コード例 #1
0
 public PyObject __iter__() {
   PyType self_type = getType();
   PyObject impl = self_type.lookup("__iter__");
   if (impl != null) return impl.__get__(this, self_type).__call__();
   impl = self_type.lookup("__getitem__");
   if (impl == null) return super.__iter__();
   return new PySequenceIter(this);
 }
コード例 #2
0
ファイル: RaiseDerived.java プロジェクト: nakagami/jython3
 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__();
   if (o.getClass() != PyInteger.class && o.getClass() != PyBoolean.class) {
     throw Py.TypeError(
         String.format("__nonzero__ should return bool or int, returned %s", self_type.getName()));
   }
   return o.__nonzero__();
 }
コード例 #3
0
ファイル: RaiseDerived.java プロジェクト: nakagami/jython3
 public int hashCode() {
   PyType self_type = getType();
   PyObject impl = self_type.lookup("__hash__");
   if (impl != null) {
     PyObject res = impl.__get__(this, self_type).__call__();
     if (res instanceof PyInteger) {
       return ((PyInteger) res).getValue().intValue();
     }
     throw Py.TypeError("__hash__ should return a int");
   }
   if (self_type.lookup("__eq__") != null || self_type.lookup("__cmp__") != null) {
     throw Py.TypeError(String.format("unhashable type: '%.200s'", getType().fastGetName()));
   }
   return super.hashCode();
 }
コード例 #4
0
 public void __del_derived__() {
   PyType self_type = getType();
   PyObject impl = self_type.lookup("__del__");
   if (impl != null) {
     impl.__get__(this, self_type).__call__();
   }
 }
コード例 #5
0
 public PyObject __call__(PyObject args[], String keywords[]) {
   PyType self_type = getType();
   PyObject impl = self_type.lookup("__call__");
   if (impl != null) {
     return impl.__get__(this, self_type).__call__(args, keywords);
   }
   return super.__call__(args, keywords);
 }
コード例 #6
0
 public void __delattr__(String name) {
   PyType self_type = getType();
   PyObject impl = self_type.lookup("__delattr__");
   if (impl != null) {
     impl.__get__(this, self_type).__call__(PyString.fromInterned(name));
     return;
   }
   super.__delattr__(name);
 }
コード例 #7
0
 public void __delete__(PyObject obj) {
   PyType self_type = getType();
   PyObject impl = self_type.lookup("__delete__");
   if (impl != null) {
     impl.__get__(this, self_type).__call__(obj);
     return;
   }
   super.__delete__(obj);
 }
コード例 #8
0
 public void __delitem__(PyObject key) { // ???
   PyType self_type = getType();
   PyObject impl = self_type.lookup("__delitem__");
   if (impl != null) {
     impl.__get__(this, self_type).__call__(key);
     return;
   }
   super.__delitem__(key);
 }
コード例 #9
0
 public int __len__() {
   PyType self_type = getType();
   PyObject impl = self_type.lookup("__len__");
   if (impl != null) {
     PyObject res = impl.__get__(this, self_type).__call__();
     return res.asInt();
   }
   return super.__len__();
 }
コード例 #10
0
 public PyObject __ixor__(PyObject other) {
   PyType self_type = getType();
   PyObject impl = self_type.lookup("__ixor__");
   if (impl != null) {
     PyObject res = impl.__get__(this, self_type).__call__(other);
     if (res == Py.NotImplemented) return null;
     return res;
   }
   return super.__ixor__(other);
 }
コード例 #11
0
 public PyObject __int__() {
   PyType self_type = getType();
   PyObject impl = self_type.lookup("__int__");
   if (impl != null) {
     PyObject res = impl.__get__(this, self_type).__call__();
     if (res instanceof PyInteger || res instanceof PyLong) return res;
     throw Py.TypeError("__int__" + " should return an integer");
   }
   return super.__int__();
 }
コード例 #12
0
 public int __len__() {
   PyType self_type = getType();
   PyObject impl = self_type.lookup("__len__");
   if (impl != null) {
     PyObject res = impl.__get__(this, self_type).__call__();
     if (res instanceof PyInteger) return ((PyInteger) res).getValue();
     throw Py.TypeError("__len__ should return a int");
   }
   return super.__len__();
 }
コード例 #13
0
 public PyObject __get__(PyObject obj, PyObject type) {
   PyType self_type = getType();
   PyObject impl = self_type.lookup("__get__");
   if (impl != null) {
     if (obj == null) obj = Py.None;
     if (type == null) type = Py.None;
     return impl.__get__(this, self_type).__call__(obj, type);
   }
   return super.__get__(obj, type);
 }
コード例 #14
0
 public void __setattr__(String name, PyObject value) {
   PyType self_type = getType();
   PyObject impl = self_type.lookup("__setattr__");
   if (impl != null) {
     impl.__get__(this, self_type).__call__(PyString.fromInterned(name), value);
     // CPython does not support instance-acquired finalizers.
     // So we don't check for __del__ here.
     return;
   }
   super.__setattr__(name, value);
 }
コード例 #15
0
 public Object __coerce_ex__(PyObject o) {
   PyType self_type = getType();
   PyObject impl = self_type.lookup("__coerce__");
   if (impl != null) {
     PyObject res = impl.__get__(this, self_type).__call__(o);
     if (res == Py.NotImplemented) return Py.None;
     if (!(res instanceof PyTuple)) throw Py.TypeError("__coerce__ didn't return a 2-tuple");
     return ((PyTuple) res).getArray();
   }
   return super.__coerce_ex__(o);
 }
コード例 #16
0
 public PyFloat __float__() {
   PyType self_type = getType();
   PyObject impl = self_type.lookup("__float__");
   if (impl != null) {
     PyObject res = impl.__get__(this, self_type).__call__();
     if (res instanceof PyFloat) return (PyFloat) res;
     throw Py.TypeError(
         "__float__" + " returned non-" + "float" + " (type " + res.getType().fastGetName() + ")");
   }
   return super.__float__();
 }
コード例 #17
0
 public PyUnicode __unicode__() {
   PyType self_type = getType();
   PyObject impl = self_type.lookup("__unicode__");
   if (impl != null) {
     PyObject res = impl.__get__(this, self_type).__call__();
     if (res instanceof PyUnicode) return (PyUnicode) res;
     if (res instanceof PyString) return new PyUnicode((PyString) res);
     throw Py.TypeError("__unicode__" + " should return a " + "unicode");
   }
   return super.__unicode__();
 }
コード例 #18
0
 public PyObject __getslice__(PyObject start, PyObject stop, PyObject step) { // ???
   if (step != null) {
     return __getitem__(new PySlice(start, stop, step));
   }
   PyType self_type = getType();
   PyObject impl = self_type.lookup("__getslice__");
   if (impl != null) {
     PyObject[] indices = PySlice.indices2(this, start, stop);
     return impl.__get__(this, self_type).__call__(indices[0], indices[1]);
   }
   return super.__getslice__(start, stop, step);
 }
コード例 #19
0
 public PyObject __finditem__(int key) {
   PyType self_type = getType();
   PyObject impl = self_type.lookup("__getitem__");
   if (impl != null)
     try {
       return impl.__get__(this, self_type).__call__(new PyInteger(key));
     } catch (PyException exc) {
       if (exc.match(Py.LookupError)) return null;
       throw exc;
     }
   return super.__finditem__(key);
 }
コード例 #20
0
 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();
 }
コード例 #21
0
 public PyObject __call__(PyObject args[], String keywords[]) {
   ThreadState ts = Py.getThreadState();
   if (ts.recursion_depth++ > ts.systemState.getrecursionlimit())
     throw Py.RuntimeError("maximum __call__ recursion depth exceeded");
   try {
     PyType self_type = getType();
     PyObject impl = self_type.lookup("__call__");
     if (impl != null) return impl.__get__(this, self_type).__call__(args, keywords);
     return super.__call__(args, keywords);
   } finally {
     --ts.recursion_depth;
   }
 }
コード例 #22
0
 public PyObject __iternext__() {
   PyType self_type = getType();
   PyObject impl = self_type.lookup("next");
   if (impl != null) {
     try {
       return impl.__get__(this, self_type).__call__();
     } catch (PyException exc) {
       if (exc.match(Py.StopIteration)) return null;
       throw exc;
     }
   }
   return super.__iternext__(); // ???
 }
コード例 #23
0
 public PyObject __findattr_ex__(String name) {
   PyType self_type = getType();
   // TODO: We should speed this up. As the __getattribute__ slot almost never
   //       changes, it is a good candidate for caching, as PyClass does with
   //       __getattr__. See #1102.
   PyObject getattribute = self_type.lookup("__getattribute__");
   PyString py_name = null;
   PyException firstAttributeError = null;
   try {
     if (getattribute != null) {
       py_name = PyString.fromInterned(name);
       return getattribute.__get__(this, self_type).__call__(py_name);
     } else {
       Py.Warning(String.format("__getattribute__ not found on type %s", self_type.getName()));
       PyObject ret = super.__findattr_ex__(name);
       if (ret != null) {
         return ret;
       } // else: pass through to __getitem__ invocation
     }
   } catch (PyException e) {
     if (!Py.matchException(e, Py.AttributeError)) {
       throw e;
     } else {
       firstAttributeError = e; // saved to avoid swallowing custom AttributeErrors
       // and pass through to __getattr__ invocation.
     }
   }
   PyObject getattr = self_type.lookup("__getattr__");
   if (getattr != null) {
     if (py_name == null) {
       py_name = PyString.fromInterned(name);
     }
     return getattr.__get__(this, self_type).__call__(py_name);
   }
   if (firstAttributeError != null) {
     throw firstAttributeError;
   }
   return null;
 }
コード例 #24
0
 public void __setslice__(PyObject start, PyObject stop, PyObject step, PyObject value) {
   if (step != null) {
     __setitem__(new PySlice(start, stop, step), value);
     return;
   }
   PyType self_type = getType();
   PyObject impl = self_type.lookup("__setslice__");
   if (impl != null) {
     PyObject[] indices = PySlice.indices2(this, start, stop);
     impl.__get__(this, self_type).__call__(indices[0], indices[1], value);
     return;
   }
   super.__setslice__(start, stop, step, value);
 }
コード例 #25
0
 public PyObject __index__() {
   PyType self_type = getType();
   PyObject impl = self_type.lookup("__index__");
   if (impl != null) {
     PyObject res = impl.__get__(this, self_type).__call__();
     if (res instanceof PyInteger || res instanceof PyLong) {
       return res;
     }
     throw Py.TypeError(
         String.format(
             "__index__ returned non-(int,long) (type %s)", res.getType().fastGetName()));
   }
   return super.__index__();
 }
コード例 #26
0
 public void dispatch__init__(PyType type, PyObject[] args, String[] keywords) {
   PyType self_type = getType();
   if (self_type.isSubType(type)) {
     PyObject impl = self_type.lookup("__init__");
     if (impl != null) {
       PyObject res = impl.__get__(this, self_type).__call__(args, keywords);
       if (res != Py.None) {
         throw Py.TypeError(
             String.format(
                 "__init__() should return None, not '%.200s'", res.getType().fastGetName()));
       }
       proxyInit();
     }
   }
 }
コード例 #27
0
 public PyObject __pow__(PyObject other, PyObject modulo) {
   PyType self_type = getType();
   PyObject impl = self_type.lookup("__pow__");
   if (impl != null) {
     PyObject res;
     if (modulo == null) {
       res = impl.__get__(this, self_type).__call__(other);
     } else {
       res = impl.__get__(this, self_type).__call__(other, modulo);
     }
     if (res == Py.NotImplemented) return null;
     return res;
   }
   return super.__pow__(other, modulo);
 }
コード例 #28
0
 public Object __tojava__(Class c) {
   // If we are not being asked by the "default" conversion to java, then
   // we can provide this as the result, as long as it is a instance of the
   // specified class. Without this, derived.__tojava__(PyObject.class)
   // would broke. (And that's not pure speculation: PyReflectedFunction's
   // ReflectedArgs asks for things like that).
   if ((c != Object.class) && (c != Serializable.class) && (c.isInstance(this))) {
     return this;
   }
   // Otherwise, we call the derived __tojava__, if it exists:
   PyType self_type = getType();
   PyObject impl = self_type.lookup("__tojava__");
   if (impl != null)
     return impl.__get__(this, self_type).__call__(Py.java2py(c)).__tojava__(Object.class);
   return super.__tojava__(c);
 }
コード例 #29
0
  public PyObject __getitem__(PyObject key) {
    // Same as __finditem__, without swallowing LookupErrors. This allows
    // __getitem__ implementations written in Python to raise custom
    // exceptions (such as subclasses of KeyError).
    //
    // We are forced to duplicate the code, instead of defining __finditem__
    // in terms of __getitem__. That's because PyObject defines __getitem__
    // in terms of __finditem__. Therefore, we would end with an infinite
    // loop when self_type.lookup("__getitem__") returns null:
    //
    //  __getitem__ -> super.__getitem__ -> __finditem__ -> __getitem__
    //
    // By duplicating the (short) lookup and call code, we are safe, because
    // the call chains will be:
    //
    // __finditem__ -> super.__finditem__
    //
    // __getitem__ -> super.__getitem__ -> __finditem__ -> super.__finditem__

    PyType self_type = getType();
    PyObject impl = self_type.lookup("__getitem__");
    if (impl != null) return impl.__get__(this, self_type).__call__(key);
    return super.__getitem__(key);
  }
コード例 #30
0
 public boolean __contains__(PyObject o) {
   PyType self_type = getType();
   PyObject impl = self_type.lookup("__contains__");
   if (impl == null) return super.__contains__(o);
   return impl.__get__(this, self_type).__call__(o).__nonzero__();
 }