Author: Armin Rigo <ar...@tunes.org> Branch: Changeset: r68070:8d1f6d47d417 Date: 2013-11-14 10:48 +0100 http://bitbucket.org/pypy/pypy/changeset/8d1f6d47d417/
Log: Issue #1633 Give a more sensible error message on int() or long() typeerrors. diff --git a/pypy/objspace/std/inttype.py b/pypy/objspace/std/inttype.py --- a/pypy/objspace/std/inttype.py +++ b/pypy/objspace/std/inttype.py @@ -119,9 +119,14 @@ if not ok: # otherwise, use the __int__() or the __trunc__() methods w_obj = w_value - if space.lookup(w_obj, '__int__') is None: + if space.lookup(w_obj, '__int__') is not None: + w_obj = space.int(w_obj) + elif space.lookup(w_obj, '__trunc__') is not None: w_obj = space.trunc(w_obj) - w_obj = space.int(w_obj) + else: + raise operationerrfmt(space.w_TypeError, + "int() argument must be a string or a number, not '%T'", + w_obj) # 'int(x)' should return what x.__int__() returned, which should # be an int or long or a subclass thereof. if space.is_w(w_inttype, space.w_int): diff --git a/pypy/objspace/std/longtype.py b/pypy/objspace/std/longtype.py --- a/pypy/objspace/std/longtype.py +++ b/pypy/objspace/std/longtype.py @@ -1,4 +1,4 @@ -from pypy.interpreter.error import OperationError +from pypy.interpreter.error import OperationError, operationerrfmt from pypy.interpreter import typedef from pypy.interpreter.gateway import interp2app, unwrap_spec, WrappedDefault,\ interpindirect2app @@ -39,13 +39,17 @@ if (space.lookup(w_obj, '__long__') is not None or space.lookup(w_obj, '__int__') is not None): w_obj = space.long(w_obj) - else: + elif space.lookup(w_obj, '__trunc__') is not None: w_obj = space.trunc(w_obj) # :-( blame CPython 2.7 if space.lookup(w_obj, '__long__') is not None: w_obj = space.long(w_obj) else: w_obj = space.int(w_obj) + else: + raise operationerrfmt(space.w_TypeError, + "long() argument must be a string or a number, not '%T'", + w_obj) bigint = space.bigint_w(w_obj) return newbigint(space, w_longtype, bigint) else: diff --git a/pypy/objspace/std/test/test_intobject.py b/pypy/objspace/std/test/test_intobject.py --- a/pypy/objspace/std/test/test_intobject.py +++ b/pypy/objspace/std/test/test_intobject.py @@ -498,6 +498,11 @@ b = A(5).real assert type(b) is int + def test_int_error_msg(self): + e = raises(TypeError, int, []) + assert str(e.value) == ( + "int() argument must be a string or a number, not 'list'") + class AppTestIntOptimizedAdd(AppTestInt): spaceconfig = {"objspace.std.optimized_int_add": True} diff --git a/pypy/objspace/std/test/test_longobject.py b/pypy/objspace/std/test/test_longobject.py --- a/pypy/objspace/std/test/test_longobject.py +++ b/pypy/objspace/std/test/test_longobject.py @@ -341,3 +341,8 @@ assert int(long(3)) == long(3) assert int(A(13)) == 42 + + def test_long_error_msg(self): + e = raises(TypeError, long, []) + assert str(e.value) == ( + "long() argument must be a string or a number, not 'list'") _______________________________________________ pypy-commit mailing list pypy-commit@python.org https://mail.python.org/mailman/listinfo/pypy-commit