Author: Armin Rigo <ar...@tunes.org> Branch: Changeset: r85901:76db6bbbc6f6 Date: 2016-07-29 10:29 +0200 http://bitbucket.org/pypy/pypy/changeset/76db6bbbc6f6/
Log: Fix PyNumber_Check() to: - match the behavior of CPython (it returns true for complex numbers, for example) - hopefully fix a numpy bug, which might be caused by PyNumber_Check() causing unexpectedly more C calls, via space.float_w() diff --git a/pypy/module/cpyext/number.py b/pypy/module/cpyext/number.py --- a/pypy/module/cpyext/number.py +++ b/pypy/module/cpyext/number.py @@ -20,16 +20,12 @@ def PyNumber_Check(space, w_obj): """Returns 1 if the object o provides numeric protocols, and false otherwise. This function always succeeds.""" - try: - space.float_w(w_obj) + # According to CPython, this means: w_obj is not None, and + # the type of w_obj has got a method __int__ or __float__. + if w_obj is None: + return 0 + if space.lookup(w_obj, '__int__') or space.lookup(w_obj, '__float__'): return 1 - except OperationError: - pass - try: - space.int_w(w_obj) - return 1 - except OperationError: - pass return 0 @cpython_api([PyObject, PyObject], Py_ssize_t, error=-1) diff --git a/pypy/module/cpyext/test/test_number.py b/pypy/module/cpyext/test/test_number.py --- a/pypy/module/cpyext/test/test_number.py +++ b/pypy/module/cpyext/test/test_number.py @@ -15,7 +15,7 @@ assert api.PyNumber_Check(space.wraplong(-12L)) assert api.PyNumber_Check(space.wrap(12.1)) assert not api.PyNumber_Check(space.wrap('12')) - assert not api.PyNumber_Check(space.wrap(1+3j)) + assert api.PyNumber_Check(space.wrap(1+3j)) def test_number_long(self, space, api): w_l = api.PyNumber_Long(space.wrap(123)) _______________________________________________ pypy-commit mailing list pypy-commit@python.org https://mail.python.org/mailman/listinfo/pypy-commit