Author: Armin Rigo <ar...@tunes.org> Branch: Changeset: r54093:849a04adb9cb Date: 2012-03-30 09:40 +0200 http://bitbucket.org/pypy/pypy/changeset/849a04adb9cb/
Log: merge heads diff --git a/pypy/module/cpyext/bufferobject.py b/pypy/module/cpyext/bufferobject.py --- a/pypy/module/cpyext/bufferobject.py +++ b/pypy/module/cpyext/bufferobject.py @@ -4,6 +4,8 @@ PyObjectFields, PyObject) from pypy.module.cpyext.pyobject import make_typedescr, Py_DecRef from pypy.interpreter.buffer import Buffer, StringBuffer, SubBuffer +from pypy.interpreter.error import OperationError +from pypy.module.array.interp_array import ArrayBuffer PyBufferObjectStruct = lltype.ForwardReference() @@ -43,10 +45,15 @@ if isinstance(w_obj, StringBuffer): py_buf.c_b_base = rffi.cast(PyObject, 0) # space.wrap(w_obj.value) - py_buf.c_b_ptr = rffi.cast(rffi.VOIDP, rffi.str2charp(w_obj.as_str())) + py_buf.c_b_ptr = rffi.cast(rffi.VOIDP, rffi.str2charp(w_obj.value)) + py_buf.c_b_size = w_obj.getlength() + elif isinstance(w_obj, ArrayBuffer): + py_buf.c_b_base = rffi.cast(PyObject, 0) # space.wrap(w_obj.value) + py_buf.c_b_ptr = rffi.cast(rffi.VOIDP, w_obj.data) py_buf.c_b_size = w_obj.getlength() else: - raise Exception("Fail fail fail fail fail") + raise OperationError(space.w_NotImplementedError, space.wrap( + "buffer flavor not supported")) def buffer_realize(space, py_obj): diff --git a/pypy/module/cpyext/test/test_bufferobject.py b/pypy/module/cpyext/test/test_bufferobject.py --- a/pypy/module/cpyext/test/test_bufferobject.py +++ b/pypy/module/cpyext/test/test_bufferobject.py @@ -48,3 +48,17 @@ ]) b = module.buffer_new() raises(AttributeError, getattr, b, 'x') + + def test_array_buffer(self): + module = self.import_extension('foo', [ + ("roundtrip", "METH_O", + """ + PyBufferObject *buf = (PyBufferObject *)args; + return PyString_FromStringAndSize(buf->b_ptr, buf->b_size); + """), + ]) + import array + a = array.array('c', 'text') + b = buffer(a) + assert module.roundtrip(b) == 'text' + diff --git a/pypy/module/micronumpy/__init__.py b/pypy/module/micronumpy/__init__.py --- a/pypy/module/micronumpy/__init__.py +++ b/pypy/module/micronumpy/__init__.py @@ -99,6 +99,8 @@ ("exp2", "exp2"), ("expm1", "expm1"), ("fabs", "fabs"), + ("fmax", "fmax"), + ("fmin", "fmin"), ("fmod", "fmod"), ("floor", "floor"), ("ceil", "ceil"), @@ -122,12 +124,14 @@ ("sinh", "sinh"), ("subtract", "subtract"), ('sqrt', 'sqrt'), + ('square', 'square'), ("tan", "tan"), ("tanh", "tanh"), ('bitwise_and', 'bitwise_and'), ('bitwise_or', 'bitwise_or'), ('bitwise_xor', 'bitwise_xor'), ('bitwise_not', 'invert'), + ('invert', 'invert'), ('isnan', 'isnan'), ('isinf', 'isinf'), ('isneginf', 'isneginf'), diff --git a/pypy/module/micronumpy/interp_ufuncs.py b/pypy/module/micronumpy/interp_ufuncs.py --- a/pypy/module/micronumpy/interp_ufuncs.py +++ b/pypy/module/micronumpy/interp_ufuncs.py @@ -541,6 +541,8 @@ ("reciprocal", "reciprocal", 1), ("fabs", "fabs", 1, {"promote_to_float": True}), + ("fmax", "fmax", 2, {"promote_to_float": True}), + ("fmin", "fmin", 2, {"promote_to_float": True}), ("fmod", "fmod", 2, {"promote_to_float": True}), ("floor", "floor", 1, {"promote_to_float": True}), ("ceil", "ceil", 1, {"promote_to_float": True}), @@ -549,6 +551,7 @@ ("expm1", "expm1", 1, {"promote_to_float": True}), ('sqrt', 'sqrt', 1, {'promote_to_float': True}), + ('square', 'square', 1, {'promote_to_float': True}), ("sin", "sin", 1, {"promote_to_float": True}), ("cos", "cos", 1, {"promote_to_float": True}), diff --git a/pypy/module/micronumpy/test/test_ufuncs.py b/pypy/module/micronumpy/test/test_ufuncs.py --- a/pypy/module/micronumpy/test/test_ufuncs.py +++ b/pypy/module/micronumpy/test/test_ufuncs.py @@ -135,6 +135,38 @@ assert fabs(float('-inf')) == float('inf') assert isnan(fabs(float('nan'))) + def test_fmax(self): + from _numpypy import fmax + import math + + nnan, nan, inf, ninf = float('-nan'), float('nan'), float('inf'), float('-inf') + + a = [ninf, -5, 0, 5, inf] + assert (fmax(a, [ninf]*5) == a).all() + assert (fmax(a, [inf]*5) == [inf]*5).all() + assert (fmax(a, [1]*5) == [1, 1, 1, 5, inf]).all() + assert math.isnan(fmax(nan, 0)) + assert math.isnan(fmax(0, nan)) + assert math.isnan(fmax(nan, nan)) + # The numpy docs specify that the FIRST NaN should be used if both are NaN + assert math.copysign(1.0, fmax(nnan, nan)) == -1.0 + + def test_fmin(self): + from _numpypy import fmin + import math + + nnan, nan, inf, ninf = float('-nan'), float('nan'), float('inf'), float('-inf') + + a = [ninf, -5, 0, 5, inf] + assert (fmin(a, [ninf]*5) == [ninf]*5).all() + assert (fmin(a, [inf]*5) == a).all() + assert (fmin(a, [1]*5) == [ninf, -5, 0, 1, 1]).all() + assert math.isnan(fmin(nan, 0)) + assert math.isnan(fmin(0, nan)) + assert math.isnan(fmin(nan, nan)) + # The numpy docs specify that the FIRST NaN should be used if both are NaN + assert math.copysign(1.0, fmin(nnan, nan)) == -1.0 + def test_fmod(self): from _numpypy import fmod import math @@ -455,6 +487,19 @@ assert math.isnan(sqrt(-1)) assert math.isnan(sqrt(nan)) + def test_square(self): + import math + from _numpypy import square + + nan, inf, ninf = float("nan"), float("inf"), float("-inf") + + assert math.isnan(square(nan)) + assert math.isinf(square(inf)) + assert math.isinf(square(ninf)) + assert square(ninf) > 0 + assert [square(x) for x in range(-5, 5)] == [x*x for x in range(-5, 5)] + assert math.isinf(square(1e300)) + def test_radians(self): import math from _numpypy import radians, array @@ -546,10 +591,11 @@ raises(TypeError, 'array([1.0]) & 1') def test_unary_bitops(self): - from _numpypy import bitwise_not, array + from _numpypy import bitwise_not, invert, array a = array([1, 2, 3, 4]) assert (~a == [-2, -3, -4, -5]).all() assert (bitwise_not(a) == ~a).all() + assert (invert(a) == ~a).all() def test_comparisons(self): import operator diff --git a/pypy/module/micronumpy/types.py b/pypy/module/micronumpy/types.py --- a/pypy/module/micronumpy/types.py +++ b/pypy/module/micronumpy/types.py @@ -631,6 +631,22 @@ return math.fabs(v) @simple_binary_op + def fmax(self, v1, v2): + if math.isnan(v1): + return v1 + elif math.isnan(v2): + return v2 + return max(v1, v2) + + @simple_binary_op + def fmin(self, v1, v2): + if math.isnan(v1): + return v1 + elif math.isnan(v2): + return v2 + return min(v1, v2) + + @simple_binary_op def fmod(self, v1, v2): try: return math.fmod(v1, v2) @@ -741,6 +757,10 @@ except ValueError: return rfloat.NAN + @simple_unary_op + def square(self, v): + return v*v + @raw_unary_op def isnan(self, v): return rfloat.isnan(v) _______________________________________________ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit