| 1 |
from pypy.module.micronumpy.test.test_base import BaseNumpyAppTest |
| 2 |
from pypy.conftest import option |
| 3 |
|
| 4 |
|
| 5 |
class AppTestObjectDtypes(BaseNumpyAppTest): |
| 6 |
spaceconfig = dict(usemodules=["micronumpy", "struct", "binascii"]) |
| 7 |
|
| 8 |
def setup_class(cls): |
| 9 |
BaseNumpyAppTest.setup_class.im_func(cls) |
| 10 |
cls.w_runappdirect = cls.space.wrap(option.runappdirect) |
| 11 |
|
| 12 |
def test_scalar_from_object(self): |
| 13 |
from numpy import array |
| 14 |
import sys |
| 15 |
class Polynomial(object): |
| 16 |
def whatami(self): |
| 17 |
return 'an object' |
| 18 |
a = array(Polynomial()) |
| 19 |
assert a.shape == () |
| 20 |
assert a.sum().whatami() == 'an object' |
| 21 |
|
| 22 |
def test_uninitialized_object_array_is_filled_by_None(self): |
| 23 |
import numpy as np |
| 24 |
|
| 25 |
a = np.ndarray([5], dtype="O") |
| 26 |
|
| 27 |
assert a[0] == None |
| 28 |
|
| 29 |
def test_object_arrays_add(self): |
| 30 |
import numpy as np |
| 31 |
|
| 32 |
a = np.array(["foo"], dtype=object) |
| 33 |
b = np.array(["bar"], dtype=object) |
| 34 |
raises(TypeError, np.add, a, 1) |
| 35 |
res = a + b |
| 36 |
assert res[0] == "foobar" |
| 37 |
|
| 38 |
def test_bool_func(self): |
| 39 |
import numpy as np |
| 40 |
a = np.array(["foo"], dtype=object) |
| 41 |
b = a and complex(1, -1) |
| 42 |
assert b == complex(1, -1) |
| 43 |
b = np.array(complex(1, -1)) and a |
| 44 |
assert (b == a).all() |
| 45 |
c = np.array([1, 2, 3]) |
| 46 |
assert (a[0] != c[0]) |
| 47 |
assert (c[0] != a[0]) |
| 48 |
assert (a[0] > c[0]) |
| 49 |
assert (not a[0] < c[0]) |
| 50 |
assert (c[0] < a[0]) |
| 51 |
assert (not c[0] > a[0]) |
| 52 |
|
| 53 |
def test_logical_ufunc(self): |
| 54 |
import numpy as np |
| 55 |
import sys |
| 56 |
|
| 57 |
a = np.array(["foo"], dtype=object) |
| 58 |
b = np.array([1], dtype=object) |
| 59 |
d = np.array([complex(1, 10)], dtype=object) |
| 60 |
c = np.logical_and(a, 1) |
| 61 |
assert c.dtype == np.dtype('object') |
| 62 |
assert c == 1 |
| 63 |
c = np.logical_and(b, complex(1, -1)) |
| 64 |
assert c.dtype == np.dtype('object') |
| 65 |
assert c == complex(1, -1) |
| 66 |
c = np.logical_and(d, b) |
| 67 |
assert c == 1 |
| 68 |
c = b & 1 |
| 69 |
assert c.dtype == np.dtype('object') |
| 70 |
assert (c == 1).all() |
| 71 |
c = np.array(1) & b |
| 72 |
assert (c == b).all() |
| 73 |
|
| 74 |
def test_reduce(self): |
| 75 |
import numpy as np |
| 76 |
class O(object): |
| 77 |
def whatami(self): |
| 78 |
return 'an object' |
| 79 |
fiveOs = [O()] * 5 |
| 80 |
a = np.array(fiveOs, dtype=object) |
| 81 |
print np.maximum |
| 82 |
b = np.maximum.reduce(a) |
| 83 |
assert b is not None |
| 84 |
|
| 85 |
def test_complex_op(self): |
| 86 |
import numpy as np |
| 87 |
import sys |
| 88 |
a = np.array(['abc', 'def'], dtype=object) |
| 89 |
b = np.array([1, 2, 3], dtype=object) |
| 90 |
c = np.array([complex(1, 1), complex(1, -1)], dtype=object) |
| 91 |
for arg in (a,b,c): |
| 92 |
assert (arg == np.real(arg)).all() |
| 93 |
assert (0 == np.imag(arg)).all() |
| 94 |
if '__pypy__' in sys.builtin_module_names: |
| 95 |
skip('not implemented yet') |
| 96 |
raises(AttributeError, np.conj, a) |
| 97 |
res = np.conj(b) |
| 98 |
assert (res == b).all() |
| 99 |
res = np.conj(c) |
| 100 |
assert res[0] == c[1] and res[1] == c[0] |
| 101 |
|
| 102 |
def test_keep_object_alive(self): |
| 103 |
|
| 104 |
import numpy as np |
| 105 |
import gc |
| 106 |
class O(object): |
| 107 |
def whatami(self): |
| 108 |
return 'an object' |
| 109 |
fiveOs = [O()] * 5 |
| 110 |
a = np.array(fiveOs, dtype=object) |
| 111 |
del fiveOs |
| 112 |
gc.collect() |
| 113 |
assert a[2].whatami() == 'an object' |
| 114 |
|
| 115 |
def test_array_interface(self): |
| 116 |
import numpy as np |
| 117 |
class DummyArray(object): |
| 118 |
def __init__(self, interface, base=None): |
| 119 |
self.__array_interface__ = interface |
| 120 |
self.base = base |
| 121 |
a = np.array([(1, 2, 3)], dtype='u4,u4,u4') |
| 122 |
b = np.array([(1, 2, 3), (4, 5, 6), (7, 8, 9)], dtype='u4,u4,u4') |
| 123 |
interface = dict(a.__array_interface__) |
| 124 |
interface['shape'] = tuple([3]) |
| 125 |
interface['strides'] = tuple([0]) |
| 126 |
c = np.array(DummyArray(interface, base=a)) |
| 127 |
c.dtype = a.dtype |
| 128 |
|
| 129 |
assert (c == np.array([(1, 2, 3), (1, 2, 3), (1, 2, 3)], dtype='u4,u4,u4') ).all() |
| 130 |
|
| 131 |
def test_for_object_scalar_creation(self): |
| 132 |
import numpy as np |
| 133 |
import sys |
| 134 |
a = np.object_() |
| 135 |
b = np.object_(3) |
| 136 |
b2 = np.object_(3.0) |
| 137 |
c = np.object_([4, 5]) |
| 138 |
d = np.array([None])[0] |
| 139 |
assert a is None |
| 140 |
assert type(b) is int |
| 141 |
assert type(b2) is float |
| 142 |
assert type(c) is np.ndarray |
| 143 |
assert c.dtype == object |
| 144 |
assert type(d) is type(None) |
| 145 |
if '__pypy__' in sys.builtin_module_names: |
| 146 |
skip('not implemented yet') |
| 147 |
e = np.object_([None, {}, []]) |
| 148 |
assert e.dtype == object |
| 149 |
|
| 150 |
def test_mem_array_creation_invalid_specification(self): |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
import numpy as np |
| 155 |
import sys |
| 156 |
ytype = np.object_ |
| 157 |
if '__pypy__' in sys.builtin_module_names: |
| 158 |
dt = np.dtype([('x', int), ('y', ytype)]) |
| 159 |
x = np.empty((4, 0), dtype = dt) |
| 160 |
raises(NotImplementedError, x.__getitem__, 'y') |
| 161 |
ytype = str |
| 162 |
dt = np.dtype([('x', int), ('y', ytype)]) |
| 163 |
|
| 164 |
a = np.array([(1, 'object')], dt) |
| 165 |
|
| 166 |
raises(ValueError, np.array, [1, 'object'], dt) |
| 167 |
|
| 168 |
def test_astype(self): |
| 169 |
import numpy as np |
| 170 |
a = np.array([b'a' * 100], dtype='O') |
| 171 |
assert 'a' * 100 in str(a) |
| 172 |
b = a.astype('S') |
| 173 |
assert b.dtype == 'S100' |
| 174 |
assert 'a' * 100 in str(b) |
| 175 |
a = np.array([u'a' * 100], dtype='O') |
| 176 |
assert 'a' * 100 in str(a) |
| 177 |
b = a.astype('U') |
| 178 |
assert b.dtype == 'U100' |
| 179 |
assert 'a' * 100 in str(b) |
| 180 |
|
| 181 |
a = np.array([123], dtype='U') |
| 182 |
assert a[0] == u'123' |
| 183 |
b = a.astype('O') |
| 184 |
assert b[0] == u'123' |
| 185 |
assert type(b[0]) is unicode |
| 186 |
|
| 187 |
class MyFloat(object): |
| 188 |
def __float__(self): |
| 189 |
return 1.0 |
| 190 |
a = np.array([MyFloat()]) |
| 191 |
assert a.shape == (1,) |
| 192 |
assert a.dtype == np.object_ |
| 193 |
b = a.astype(float) |
| 194 |
assert b.shape == (1,) |
| 195 |
assert b.dtype == np.float_ |
| 196 |
assert (b == 1.0).all() |
| 197 |
|
| 198 |
|
| 199 |
def test__reduce__(self): |
| 200 |
from numpy import arange, dtype |
| 201 |
from cPickle import loads, dumps |
| 202 |
import sys |
| 203 |
|
| 204 |
a = arange(15).astype(object) |
| 205 |
if '__pypy__' in sys.builtin_module_names: |
| 206 |
raises(NotImplementedError, dumps, a) |
| 207 |
skip('not implemented yet') |
| 208 |
b = loads(dumps(a)) |
| 209 |
assert (a == b).all() |
| 210 |
|
| 211 |
a = arange(15).astype(object).reshape((3, 5)) |
| 212 |
b = loads(dumps(a)) |
| 213 |
assert (a == b).all() |
| 214 |
|