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); }
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__(); // ??? }
/** * Returns the next element from an iterator. If it raises/throws StopIteration just store the * Exception and return null according to PyIterator practice. */ protected PyObject nextElement(PyObject pyIter) { PyObject element = null; try { element = pyIter.__iternext__(); // next(); } catch (PyException pyEx) { if (pyEx.match(Py.StopIteration)) { // store exception - will be used by PyIterator.next() stopException = pyEx; } else { throw pyEx; } } return element; }
@Override public PyObject __finditem__(PyObject key) { // ??? PyType self_type = getType(); PyObject impl = self_type.lookup("__getitem__"); if (impl != null) { try { return impl.__get__(this, self_type).__call__(key); } catch (PyException exc) { if (exc.match(Py.LookupError)) { return null; } throw exc; } } return super.__finditem__(key); }
private static int py2int(PyObject obj, int defaultValue, String msg) { if (obj instanceof PyNone) { return defaultValue; } else { int value = defaultValue; try { value = Py.py2int(obj); } catch (PyException pyEx) { if (pyEx.match(Py.TypeError)) { throw Py.ValueError(msg); } else { throw pyEx; } } return value; } }
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 (!e.match(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; }