Author: Brian Kearns <bdkea...@gmail.com> Branch: Changeset: r69905:713f1b94343d Date: 2014-03-12 14:32 -0400 http://bitbucket.org/pypy/pypy/changeset/713f1b94343d/
Log: add hex/oct ops for ndarrays diff --git a/pypy/module/micronumpy/ndarray.py b/pypy/module/micronumpy/ndarray.py --- a/pypy/module/micronumpy/ndarray.py +++ b/pypy/module/micronumpy/ndarray.py @@ -1045,6 +1045,26 @@ value = self.get_scalar_value() return space.float(value) + def descr_hex(self, space): + if self.get_size() != 1: + raise oefmt(space.w_TypeError, + "only length-1 arrays can be converted to Python scalars") + if not self.get_dtype().is_int(): + raise oefmt(space.w_TypeError, + "don't know how to convert scalar number to hex") + value = self.get_scalar_value() + return space.hex(value) + + def descr_oct(self, space): + if self.get_size() != 1: + raise oefmt(space.w_TypeError, + "only length-1 arrays can be converted to Python scalars") + if not self.get_dtype().is_int(): + raise oefmt(space.w_TypeError, + "don't know how to convert scalar number to oct") + value = self.get_scalar_value() + return space.oct(value) + def descr_index(self, space): if self.get_size() != 1 or \ not self.get_dtype().is_int() or self.get_dtype().is_bool(): @@ -1237,6 +1257,8 @@ __int__ = interp2app(W_NDimArray.descr_int), __long__ = interp2app(W_NDimArray.descr_long), __float__ = interp2app(W_NDimArray.descr_float), + __hex__ = interp2app(W_NDimArray.descr_hex), + __oct__ = interp2app(W_NDimArray.descr_oct), __buffer__ = interp2app(W_NDimArray.descr_get_data), __index__ = interp2app(W_NDimArray.descr_index), diff --git a/pypy/module/micronumpy/test/test_ndarray.py b/pypy/module/micronumpy/test/test_ndarray.py --- a/pypy/module/micronumpy/test/test_ndarray.py +++ b/pypy/module/micronumpy/test/test_ndarray.py @@ -2276,6 +2276,30 @@ exc = raises(TypeError, "float(np.array([1.5, 2.5]))") assert exc.value[0] == 'only length-1 arrays can be converted to Python scalars' + def test__hex__(self): + import numpy as np + assert hex(np.array(True)) == '0x1' + assert hex(np.array(15)) == '0xf' + assert hex(np.array([15])) == '0xf' + exc = raises(TypeError, "hex(np.array(1.5))") + assert str(exc.value) == "don't know how to convert scalar number to hex" + exc = raises(TypeError, "hex(np.array('15'))") + assert str(exc.value) == "don't know how to convert scalar number to hex" + exc = raises(TypeError, "hex(np.array([1, 2]))") + assert str(exc.value) == "only length-1 arrays can be converted to Python scalars" + + def test__oct__(self): + import numpy as np + assert oct(np.array(True)) == '01' + assert oct(np.array(15)) == '017' + assert oct(np.array([15])) == '017' + exc = raises(TypeError, "oct(np.array(1.5))") + assert str(exc.value) == "don't know how to convert scalar number to oct" + exc = raises(TypeError, "oct(np.array('15'))") + assert str(exc.value) == "don't know how to convert scalar number to oct" + exc = raises(TypeError, "oct(np.array([1, 2]))") + assert str(exc.value) == "only length-1 arrays can be converted to Python scalars" + def test__reduce__(self): from numpypy import array, dtype from cPickle import loads, dumps _______________________________________________ pypy-commit mailing list pypy-commit@python.org https://mail.python.org/mailman/listinfo/pypy-commit