# HG changeset patch # User Leonardo Soto # Date 1211680083 14400 # Branch doj # Node ID f782e0688bd38c537ce890327ba93d19475ed29d # Parent c9eb4332304efeb4405e12e7835e2fb5df187179 Fix for AttributeError message on type instances (new-style classes) diff -r c9eb4332304e -r f782e0688bd3 jython/Lib/test/test_class_jy.py --- a/jython/Lib/test/test_class_jy.py Sat May 24 17:50:44 2008 -0400 +++ b/jython/Lib/test/test_class_jy.py Sat May 24 21:48:03 2008 -0400 @@ -121,6 +121,18 @@ class ClassGeneralTestCase(unittest.Test self.assert_(hasattr(Bar, 'hello')) self.assertEquals(Bar().hello(), 'hello') + def test_attribute_error_message(self): + # Ensure that AttributeError matches the CPython message + class Foo(object): + pass + try: + Foo.bar + self._assert(False) # The previous line should have raised + # AttributeError + except AttributeError, e: + self.assertEqual("type object 'Foo' has no attribute 'bar'", + str(e)) + class ClassNamelessModuleTestCase(unittest.TestCase): diff -r c9eb4332304e -r f782e0688bd3 jython/src/org/python/core/PyType.java --- a/jython/src/org/python/core/PyType.java Sat May 24 17:50:44 2008 -0400 +++ b/jython/src/org/python/core/PyType.java Sat May 24 21:48:03 2008 -0400 @@ -1299,6 +1299,16 @@ public class PyType extends PyObject imp return String.format("<%s '%s'>", kind, getName()); } + /** + * Raises AttributeError on type objects. The message differs from + * PyObject#noAttributeError, to mimic CPython behaviour. + */ + public void noAttributeError(String name) { + throw Py.AttributeError( + String.format("type object '%.50s' has no attribute '%.400s'", + fastGetName(), name)); + } + //XXX: consider pulling this out into a generally accessible place // I bet this is duplicated more or less in other places. private static void confirmIdentifier(PyObject o) {