From: <pj...@us...> - 2008-08-02 19:51:20
|
Revision: 5053 http://jython.svn.sourceforge.net/jython/?rev=5053&view=rev Author: pjenvey Date: 2008-08-02 19:51:15 +0000 (Sat, 02 Aug 2008) Log Message: ----------- switch some new style unary ops to throw TypeErrors instead of AttributeErrors when not implemented. the old styles still raise AttributeErrors (both styles now match CPython). we assume __len/int/long/float/complex__ raise AttributeErrors in many places so leaving them alone for now Modified Paths: -------------- branches/asm/Lib/test/test_descr_jy.py branches/asm/src/org/python/core/PyInstance.java branches/asm/src/org/python/core/PyObject.java branches/asm/src/org/python/core/__builtin__.java Modified: branches/asm/Lib/test/test_descr_jy.py =================================================================== --- branches/asm/Lib/test/test_descr_jy.py 2008-08-02 08:49:57 UTC (rev 5052) +++ branches/asm/Lib/test/test_descr_jy.py 2008-08-02 19:51:15 UTC (rev 5053) @@ -2,10 +2,21 @@ Made for Jython. """ -import test_support import types import unittest +from test import test_support +class Old: + pass + + +class New(object): + pass + + +old = Old() +new = New() + class TestDescrTestCase(unittest.TestCase): def test_class_dict_is_copy(self): @@ -255,10 +266,39 @@ self.assertEqual(foo, set([1])) +class DescrExceptionsTestCase(unittest.TestCase): + + def test_hex(self): + self._test(hex) + + def test_oct(self): + self._test(oct) + + def test_other(self): + for op in '-', '+', '~': + try: + eval('%s(old)' % op) + except AttributeError: + pass + else: + self._assert(False, 'Expected an AttributeError, op: %s' % op) + try: + eval('%s(new)' % op) + except TypeError: + pass + else: + self._assert(False, 'Expected a TypeError, op: %s' % op) + + def _test(self, func): + self.assertRaises(AttributeError, func, old) + self.assertRaises(TypeError, func, new) + + def test_main(): test_support.run_unittest(TestDescrTestCase, SubclassDescrTestCase, - InPlaceTestCase) + InPlaceTestCase, + DescrExceptionsTestCase) if __name__ == '__main__': test_main() Modified: branches/asm/src/org/python/core/PyInstance.java =================================================================== --- branches/asm/src/org/python/core/PyInstance.java 2008-08-02 08:49:57 UTC (rev 5052) +++ branches/asm/src/org/python/core/PyInstance.java 2008-08-02 19:51:15 UTC (rev 5053) @@ -264,7 +264,7 @@ } } if (f == null) f = ifindfunction(name); - if (f == null) throw Py.AttributeError(name); + if (f == null) noAttributeError(name); return f.__call__(); } @@ -281,7 +281,7 @@ } } if (f == null) f = ifindfunction(name); - if (f == null) throw Py.AttributeError(name); + if (f == null) noAttributeError(name); return f.__call__(arg1); } @@ -298,11 +298,16 @@ } } if (f == null) f = ifindfunction(name); - if (f == null) throw Py.AttributeError(name); + if (f == null) noAttributeError(name); return f.__call__(arg1, arg2); } + public void noAttributeError(String name) { + throw Py.AttributeError(String.format("%.50s instance has no attribute '%.400s'", + instclass.__name__, name)); + } + public void __setattr__(String name, PyObject value) { if (name == "__class__") { if (value instanceof PyClass) { Modified: branches/asm/src/org/python/core/PyObject.java =================================================================== --- branches/asm/src/org/python/core/PyObject.java 2008-08-02 08:49:57 UTC (rev 5052) +++ branches/asm/src/org/python/core/PyObject.java 2008-08-02 19:51:15 UTC (rev 5053) @@ -581,7 +581,8 @@ * @exception Py.KeyError if the key is not found in the container **/ public void __delitem__(PyObject key) { - throw Py.AttributeError("__delitem__"); + throw Py.TypeError(String.format("'%.200s' object doesn't support item deletion", + getType().fastGetName())); } /** @@ -1504,7 +1505,7 @@ * @return a string representing this object as a hexadecimal number. **/ public PyString __hex__() { - throw Py.AttributeError("__hex__"); + throw Py.TypeError("hex() argument can't be converted to hex"); } /** @@ -1515,7 +1516,7 @@ * @return a string representing this object as an octal number. **/ public PyString __oct__() { - throw Py.AttributeError("__oct__"); + throw Py.TypeError("oct() argument can't be converted to oct"); } /** @@ -1568,7 +1569,8 @@ * @return +this. **/ public PyObject __pos__() { - throw Py.AttributeError("__pos__"); + throw Py.TypeError(String.format("bad operand type for unary +: '%.200s'", + getType().fastGetName())); } /** @@ -1577,7 +1579,8 @@ * @return -this. **/ public PyObject __neg__() { - throw Py.AttributeError("__neg__"); + throw Py.TypeError(String.format("bad operand type for unary -: '%.200s'", + getType().fastGetName())); } /** @@ -1586,7 +1589,8 @@ * @return abs(this). **/ public PyObject __abs__() { - throw Py.AttributeError("__abs__"); + throw Py.TypeError(String.format("bad operand type for abs(): '%.200s'", + getType().fastGetName())); } /** @@ -1595,7 +1599,8 @@ * @return ~this. **/ public PyObject __invert__() { - throw Py.AttributeError("__invert__"); + throw Py.TypeError(String.format("bad operand type for unary ~: '%.200s'", + getType().fastGetName())); } /** Modified: branches/asm/src/org/python/core/__builtin__.java =================================================================== --- branches/asm/src/org/python/core/__builtin__.java 2008-08-02 08:49:57 UTC (rev 5052) +++ branches/asm/src/org/python/core/__builtin__.java 2008-08-02 19:51:15 UTC (rev 5053) @@ -387,10 +387,7 @@ } public static PyObject abs(PyObject o) { - if (o.isNumberType()) { - return o.__abs__(); - } - throw Py.TypeError("bad operand type for abs()"); + return o.__abs__(); } public static PyObject apply(PyObject o) { @@ -725,14 +722,7 @@ } public static PyString hex(PyObject o) { - try { - return o.__hex__(); - } catch (PyException e) { - if (Py.matchException(e, Py.AttributeError)) { - throw Py.TypeError("hex() argument can't be converted to hex"); - } - throw e; - } + return o.__hex__(); } public static long id(PyObject o) { @@ -856,14 +846,7 @@ } public static PyString oct(PyObject o) { - try { - return o.__oct__(); - } catch (PyException e) { - if (Py.matchException(e, Py.AttributeError)) { - throw Py.TypeError("oct() argument can't be converted to oct"); - } - throw e; - } + return o.__oct__(); } public static final int ord(PyObject c) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |