# HG changeset patch # User Leonardo Soto # Date 1211665844 14400 # Branch doj # Node ID c9eb4332304efeb4405e12e7835e2fb5df187179 # Parent d9e13aa9afa87ebfca12a5acd66fc683e5d1f1e4 __getitem__ methods written in python can now raise custom subclasses of LookupError diff -r d9e13aa9afa8 -r c9eb4332304e jython/Lib/test/test_dict_jy.py --- a/jython/Lib/test/test_dict_jy.py Sat May 24 00:57:07 2008 -0400 +++ b/jython/Lib/test/test_dict_jy.py Sat May 24 17:50:44 2008 -0400 @@ -67,9 +67,19 @@ class DictCmpTest(unittest.TestCase): self.assertEqual(derived_dict_with_custom_cmp(), '') self.assertEqual(yet_another_dict(), '') +class DerivedDictTest(unittest.TestCase): + "Tests for derived dict behaviour" + def test_raising_custom_key_error(self): + class CustomKeyError(KeyError): + pass + class DerivedDict(dict): + def __getitem__(self, key): + raise CustomKeyError("custom message") + self.assertRaises(CustomKeyError, lambda: DerivedDict()['foo']) + def test_main(): - test_support.run_unittest(DictInitTest, DictCmpTest) + test_support.run_unittest(DictInitTest, DictCmpTest, DerivedDictTest) if __name__ == '__main__': test_main() diff -r d9e13aa9afa8 -r c9eb4332304e jython/src/org/python/core/PyArrayDerived.java --- a/jython/src/org/python/core/PyArrayDerived.java Sat May 24 00:57:07 2008 -0400 +++ b/jython/src/org/python/core/PyArrayDerived.java Sat May 24 17:50:44 2008 -0400 @@ -809,6 +809,32 @@ public class PyArrayDerived extends PyAr return super.__finditem__(key); } + 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); + } + public void __setitem__(PyObject key,PyObject value) { // ??? PyType self_type=getType(); PyObject impl=self_type.lookup("__setitem__"); diff -r d9e13aa9afa8 -r c9eb4332304e jython/src/org/python/core/PyBooleanDerived.java --- a/jython/src/org/python/core/PyBooleanDerived.java Sat May 24 00:57:07 2008 -0400 +++ b/jython/src/org/python/core/PyBooleanDerived.java Sat May 24 17:50:44 2008 -0400 @@ -809,6 +809,32 @@ public class PyBooleanDerived extends Py return super.__finditem__(key); } + 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); + } + public void __setitem__(PyObject key,PyObject value) { // ??? PyType self_type=getType(); PyObject impl=self_type.lookup("__setitem__"); diff -r d9e13aa9afa8 -r c9eb4332304e jython/src/org/python/core/PyClassMethodDerived.java --- a/jython/src/org/python/core/PyClassMethodDerived.java Sat May 24 00:57:07 2008 -0400 +++ b/jython/src/org/python/core/PyClassMethodDerived.java Sat May 24 17:50:44 2008 -0400 @@ -809,6 +809,32 @@ public class PyClassMethodDerived extend return super.__finditem__(key); } + 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); + } + public void __setitem__(PyObject key,PyObject value) { // ??? PyType self_type=getType(); PyObject impl=self_type.lookup("__setitem__"); diff -r d9e13aa9afa8 -r c9eb4332304e jython/src/org/python/core/PyComplexDerived.java --- a/jython/src/org/python/core/PyComplexDerived.java Sat May 24 00:57:07 2008 -0400 +++ b/jython/src/org/python/core/PyComplexDerived.java Sat May 24 17:50:44 2008 -0400 @@ -809,6 +809,32 @@ public class PyComplexDerived extends Py return super.__finditem__(key); } + 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); + } + public void __setitem__(PyObject key,PyObject value) { // ??? PyType self_type=getType(); PyObject impl=self_type.lookup("__setitem__"); diff -r d9e13aa9afa8 -r c9eb4332304e jython/src/org/python/core/PyDictionaryDerived.java --- a/jython/src/org/python/core/PyDictionaryDerived.java Sat May 24 00:57:07 2008 -0400 +++ b/jython/src/org/python/core/PyDictionaryDerived.java Sat May 24 17:50:44 2008 -0400 @@ -809,6 +809,32 @@ public class PyDictionaryDerived extends return super.__finditem__(key); } + 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); + } + public void __setitem__(PyObject key,PyObject value) { // ??? PyType self_type=getType(); PyObject impl=self_type.lookup("__setitem__"); diff -r d9e13aa9afa8 -r c9eb4332304e jython/src/org/python/core/PyFileDerived.java --- a/jython/src/org/python/core/PyFileDerived.java Sat May 24 00:57:07 2008 -0400 +++ b/jython/src/org/python/core/PyFileDerived.java Sat May 24 17:50:44 2008 -0400 @@ -809,6 +809,32 @@ public class PyFileDerived extends PyFil return super.__finditem__(key); } + 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); + } + public void __setitem__(PyObject key,PyObject value) { // ??? PyType self_type=getType(); PyObject impl=self_type.lookup("__setitem__"); diff -r d9e13aa9afa8 -r c9eb4332304e jython/src/org/python/core/PyFloatDerived.java --- a/jython/src/org/python/core/PyFloatDerived.java Sat May 24 00:57:07 2008 -0400 +++ b/jython/src/org/python/core/PyFloatDerived.java Sat May 24 17:50:44 2008 -0400 @@ -809,6 +809,32 @@ public class PyFloatDerived extends PyFl return super.__finditem__(key); } + 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); + } + public void __setitem__(PyObject key,PyObject value) { // ??? PyType self_type=getType(); PyObject impl=self_type.lookup("__setitem__"); diff -r d9e13aa9afa8 -r c9eb4332304e jython/src/org/python/core/PyFrozenSetDerived.java --- a/jython/src/org/python/core/PyFrozenSetDerived.java Sat May 24 00:57:07 2008 -0400 +++ b/jython/src/org/python/core/PyFrozenSetDerived.java Sat May 24 17:50:44 2008 -0400 @@ -809,6 +809,32 @@ public class PyFrozenSetDerived extends return super.__finditem__(key); } + 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); + } + public void __setitem__(PyObject key,PyObject value) { // ??? PyType self_type=getType(); PyObject impl=self_type.lookup("__setitem__"); diff -r d9e13aa9afa8 -r c9eb4332304e jython/src/org/python/core/PyIntegerDerived.java --- a/jython/src/org/python/core/PyIntegerDerived.java Sat May 24 00:57:07 2008 -0400 +++ b/jython/src/org/python/core/PyIntegerDerived.java Sat May 24 17:50:44 2008 -0400 @@ -809,6 +809,32 @@ public class PyIntegerDerived extends Py return super.__finditem__(key); } + 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); + } + public void __setitem__(PyObject key,PyObject value) { // ??? PyType self_type=getType(); PyObject impl=self_type.lookup("__setitem__"); diff -r d9e13aa9afa8 -r c9eb4332304e jython/src/org/python/core/PyListDerived.java --- a/jython/src/org/python/core/PyListDerived.java Sat May 24 00:57:07 2008 -0400 +++ b/jython/src/org/python/core/PyListDerived.java Sat May 24 17:50:44 2008 -0400 @@ -809,6 +809,32 @@ public class PyListDerived extends PyLis return super.__finditem__(key); } + 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); + } + public void __setitem__(PyObject key,PyObject value) { // ??? PyType self_type=getType(); PyObject impl=self_type.lookup("__setitem__"); diff -r d9e13aa9afa8 -r c9eb4332304e jython/src/org/python/core/PyLongDerived.java --- a/jython/src/org/python/core/PyLongDerived.java Sat May 24 00:57:07 2008 -0400 +++ b/jython/src/org/python/core/PyLongDerived.java Sat May 24 17:50:44 2008 -0400 @@ -809,6 +809,32 @@ public class PyLongDerived extends PyLon return super.__finditem__(key); } + 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); + } + public void __setitem__(PyObject key,PyObject value) { // ??? PyType self_type=getType(); PyObject impl=self_type.lookup("__setitem__"); diff -r d9e13aa9afa8 -r c9eb4332304e jython/src/org/python/core/PyModuleDerived.java --- a/jython/src/org/python/core/PyModuleDerived.java Sat May 24 00:57:07 2008 -0400 +++ b/jython/src/org/python/core/PyModuleDerived.java Sat May 24 17:50:44 2008 -0400 @@ -785,6 +785,32 @@ public class PyModuleDerived extends PyM return super.__finditem__(key); } + 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); + } + public void __setitem__(PyObject key,PyObject value) { // ??? PyType self_type=getType(); PyObject impl=self_type.lookup("__setitem__"); diff -r d9e13aa9afa8 -r c9eb4332304e jython/src/org/python/core/PyObjectDerived.java --- a/jython/src/org/python/core/PyObjectDerived.java Sat May 24 00:57:07 2008 -0400 +++ b/jython/src/org/python/core/PyObjectDerived.java Sat May 24 17:50:44 2008 -0400 @@ -809,6 +809,32 @@ public class PyObjectDerived extends PyO return super.__finditem__(key); } + 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); + } + public void __setitem__(PyObject key,PyObject value) { // ??? PyType self_type=getType(); PyObject impl=self_type.lookup("__setitem__"); diff -r d9e13aa9afa8 -r c9eb4332304e jython/src/org/python/core/PyPropertyDerived.java --- a/jython/src/org/python/core/PyPropertyDerived.java Sat May 24 00:57:07 2008 -0400 +++ b/jython/src/org/python/core/PyPropertyDerived.java Sat May 24 17:50:44 2008 -0400 @@ -809,6 +809,32 @@ public class PyPropertyDerived extends P return super.__finditem__(key); } + 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); + } + public void __setitem__(PyObject key,PyObject value) { // ??? PyType self_type=getType(); PyObject impl=self_type.lookup("__setitem__"); diff -r d9e13aa9afa8 -r c9eb4332304e jython/src/org/python/core/PySetDerived.java --- a/jython/src/org/python/core/PySetDerived.java Sat May 24 00:57:07 2008 -0400 +++ b/jython/src/org/python/core/PySetDerived.java Sat May 24 17:50:44 2008 -0400 @@ -809,6 +809,32 @@ public class PySetDerived extends PySet return super.__finditem__(key); } + 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); + } + public void __setitem__(PyObject key,PyObject value) { // ??? PyType self_type=getType(); PyObject impl=self_type.lookup("__setitem__"); diff -r d9e13aa9afa8 -r c9eb4332304e jython/src/org/python/core/PySliceDerived.java --- a/jython/src/org/python/core/PySliceDerived.java Sat May 24 00:57:07 2008 -0400 +++ b/jython/src/org/python/core/PySliceDerived.java Sat May 24 17:50:44 2008 -0400 @@ -809,6 +809,32 @@ public class PySliceDerived extends PySl return super.__finditem__(key); } + 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); + } + public void __setitem__(PyObject key,PyObject value) { // ??? PyType self_type=getType(); PyObject impl=self_type.lookup("__setitem__"); diff -r d9e13aa9afa8 -r c9eb4332304e jython/src/org/python/core/PyStringDerived.java --- a/jython/src/org/python/core/PyStringDerived.java Sat May 24 00:57:07 2008 -0400 +++ b/jython/src/org/python/core/PyStringDerived.java Sat May 24 17:50:44 2008 -0400 @@ -809,6 +809,32 @@ public class PyStringDerived extends PyS return super.__finditem__(key); } + 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); + } + public void __setitem__(PyObject key,PyObject value) { // ??? PyType self_type=getType(); PyObject impl=self_type.lookup("__setitem__"); diff -r d9e13aa9afa8 -r c9eb4332304e jython/src/org/python/core/PySuperDerived.java --- a/jython/src/org/python/core/PySuperDerived.java Sat May 24 00:57:07 2008 -0400 +++ b/jython/src/org/python/core/PySuperDerived.java Sat May 24 17:50:44 2008 -0400 @@ -809,6 +809,32 @@ public class PySuperDerived extends PySu return super.__finditem__(key); } + 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); + } + public void __setitem__(PyObject key,PyObject value) { // ??? PyType self_type=getType(); PyObject impl=self_type.lookup("__setitem__"); diff -r d9e13aa9afa8 -r c9eb4332304e jython/src/org/python/core/PyTupleDerived.java --- a/jython/src/org/python/core/PyTupleDerived.java Sat May 24 00:57:07 2008 -0400 +++ b/jython/src/org/python/core/PyTupleDerived.java Sat May 24 17:50:44 2008 -0400 @@ -809,6 +809,32 @@ public class PyTupleDerived extends PyTu return super.__finditem__(key); } + 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); + } + public void __setitem__(PyObject key,PyObject value) { // ??? PyType self_type=getType(); PyObject impl=self_type.lookup("__setitem__"); diff -r d9e13aa9afa8 -r c9eb4332304e jython/src/org/python/core/PyTypeDerived.java --- a/jython/src/org/python/core/PyTypeDerived.java Sat May 24 00:57:07 2008 -0400 +++ b/jython/src/org/python/core/PyTypeDerived.java Sat May 24 17:50:44 2008 -0400 @@ -785,6 +785,32 @@ public class PyTypeDerived extends PyTyp return super.__finditem__(key); } + 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); + } + public void __setitem__(PyObject key,PyObject value) { // ??? PyType self_type=getType(); PyObject impl=self_type.lookup("__setitem__"); diff -r d9e13aa9afa8 -r c9eb4332304e jython/src/org/python/core/PyUnicodeDerived.java --- a/jython/src/org/python/core/PyUnicodeDerived.java Sat May 24 00:57:07 2008 -0400 +++ b/jython/src/org/python/core/PyUnicodeDerived.java Sat May 24 17:50:44 2008 -0400 @@ -809,6 +809,32 @@ public class PyUnicodeDerived extends Py return super.__finditem__(key); } + 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); + } + public void __setitem__(PyObject key,PyObject value) { // ??? PyType self_type=getType(); PyObject impl=self_type.lookup("__setitem__"); diff -r d9e13aa9afa8 -r c9eb4332304e jython/src/org/python/modules/collections/PyDefaultDictDerived.java --- a/jython/src/org/python/modules/collections/PyDefaultDictDerived.java Sat May 24 00:57:07 2008 -0400 +++ b/jython/src/org/python/modules/collections/PyDefaultDictDerived.java Sat May 24 17:50:44 2008 -0400 @@ -810,6 +810,32 @@ public class PyDefaultDictDerived extend return super.__finditem__(key); } + 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); + } + public void __setitem__(PyObject key,PyObject value) { // ??? PyType self_type=getType(); PyObject impl=self_type.lookup("__setitem__"); diff -r d9e13aa9afa8 -r c9eb4332304e jython/src/org/python/modules/collections/PyDequeDerived.java --- a/jython/src/org/python/modules/collections/PyDequeDerived.java Sat May 24 00:57:07 2008 -0400 +++ b/jython/src/org/python/modules/collections/PyDequeDerived.java Sat May 24 17:50:44 2008 -0400 @@ -810,6 +810,32 @@ public class PyDequeDerived extends PyDe return super.__finditem__(key); } + 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); + } + public void __setitem__(PyObject key,PyObject value) { // ??? PyType self_type=getType(); PyObject impl=self_type.lookup("__setitem__"); diff -r d9e13aa9afa8 -r c9eb4332304e jython/src/org/python/modules/zipimport/zipimporterDerived.java --- a/jython/src/org/python/modules/zipimport/zipimporterDerived.java Sat May 24 00:57:07 2008 -0400 +++ b/jython/src/org/python/modules/zipimport/zipimporterDerived.java Sat May 24 17:50:44 2008 -0400 @@ -786,6 +786,32 @@ public class zipimporterDerived extends return super.__finditem__(key); } + 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); + } + public void __setitem__(PyObject key,PyObject value) { // ??? PyType self_type=getType(); PyObject impl=self_type.lookup("__setitem__"); diff -r d9e13aa9afa8 -r c9eb4332304e jython/src/templates/object.derived --- a/jython/src/templates/object.derived Sat May 24 00:57:07 2008 -0400 +++ b/jython/src/templates/object.derived Sat May 24 17:50:44 2008 -0400 @@ -174,6 +174,33 @@ rest: return super.__finditem__(key); } + 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); + } + + public void __setitem__(PyObject key, PyObject value) { // ??? PyType self_type = getType(); PyObject impl = self_type.lookup("__setitem__");