From: <pj...@us...> - 2010-01-22 02:51:26
|
Revision: 6960 http://jython.svn.sourceforge.net/jython/?rev=6960&view=rev Author: pjenvey Date: 2010-01-22 02:51:19 +0000 (Fri, 22 Jan 2010) Log Message: ----------- make getattr maintain custom AttributeErrors Modified Paths: -------------- trunk/jython/Lib/test/test_builtin_jy.py trunk/jython/src/org/python/core/__builtin__.java Modified: trunk/jython/Lib/test/test_builtin_jy.py =================================================================== --- trunk/jython/Lib/test/test_builtin_jy.py 2010-01-09 06:38:57 UTC (rev 6959) +++ trunk/jython/Lib/test/test_builtin_jy.py 2010-01-22 02:51:19 UTC (rev 6960) @@ -18,6 +18,17 @@ raise TypeError() self.assert_(not hasattr(Foo(), 'bar')) + def test_getattr_custom_AttributeError(self): + class Foo(object): + def __getattr__(self, name): + raise AttributeError('baz') + try: + getattr(Foo(), 'bar') + except AttributeError, ae: + self.assertEqual(str(ae), 'baz') + else: + self.assertTrue(False) + def test_dir(self): # for http://bugs.jython.org/issue1196 class Foo(object): Modified: trunk/jython/src/org/python/core/__builtin__.java =================================================================== --- trunk/jython/src/org/python/core/__builtin__.java 2010-01-09 06:38:57 UTC (rev 6959) +++ trunk/jython/src/org/python/core/__builtin__.java 2010-01-22 02:51:19 UTC (rev 6960) @@ -613,16 +613,29 @@ public static PyObject getattr(PyObject obj, PyObject nameObj, PyObject def) { String name = asName(nameObj, "getattr"); - PyObject result = obj.__findattr__(name); + PyObject result = null; + PyException attributeError = null; + + try { + result = obj.__findattr_ex__(name); + } catch (PyException pye) { + if (!pye.match(Py.AttributeError)) { + throw pye; + } + attributeError = pye; + } if (result != null) { return result; } if (def != null) { return def; } - // throws AttributeError - obj.noAttributeError(name); - return null; + + if (attributeError == null) { + // throws AttributeError + obj.noAttributeError(name); + } + throw attributeError; } public static PyObject globals() { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |