| 1 |
from rpython.rlib import jit |
| 2 |
from rpython.rlib.rarithmetic import ovfcheck |
| 3 |
from rpython.rtyper.lltypesystem import rffi, lltype |
| 4 |
|
| 5 |
from pypy.interpreter.error import OperationError, oefmt |
| 6 |
from pypy.interpreter.gateway import unwrap_spec, appdef |
| 7 |
from pypy.interpreter.typedef import GetSetProperty |
| 8 |
from pypy.objspace.std.typeobject import W_TypeObject |
| 9 |
from pypy.objspace.std.objspace import StdObjSpace |
| 10 |
from pypy.module.micronumpy import constants as NPY |
| 11 |
from pypy.module.exceptions.interp_exceptions import _new_exception, W_UserWarning |
| 12 |
|
| 13 |
W_VisibleDeprecationWarning = _new_exception('VisibleDeprecationWarning', W_UserWarning, |
| 14 |
"""Visible deprecation warning. |
| 15 |
|
| 16 |
By default, python will not show deprecation warnings, so this class |
| 17 |
can be used when a very visible warning is helpful, for example because |
| 18 |
the usage is most likely a user bug. |
| 19 |
|
| 20 |
""") |
| 21 |
|
| 22 |
|
| 23 |
def issequence_w(space, w_obj): |
| 24 |
from pypy.module.micronumpy.base import W_NDimArray |
| 25 |
return (space.isinstance_w(w_obj, space.w_tuple) or |
| 26 |
space.isinstance_w(w_obj, space.w_list) or |
| 27 |
space.isinstance_w(w_obj, space.w_memoryview) or |
| 28 |
isinstance(w_obj, W_NDimArray)) |
| 29 |
|
| 30 |
|
| 31 |
def index_w(space, w_obj): |
| 32 |
try: |
| 33 |
return space.int_w(space.index(w_obj)) |
| 34 |
except OperationError: |
| 35 |
try: |
| 36 |
return space.int_w(space.int(w_obj)) |
| 37 |
except OperationError: |
| 38 |
raise oefmt(space.w_IndexError, "only integers, slices (`:`), " |
| 39 |
"ellipsis (`...`), numpy.newaxis (`None`) and integer or " |
| 40 |
"boolean arrays are valid indices") |
| 41 |
|
| 42 |
|
| 43 |
@jit.unroll_safe |
| 44 |
def product(s): |
| 45 |
i = 1 |
| 46 |
for x in s: |
| 47 |
i *= x |
| 48 |
return i |
| 49 |
|
| 50 |
@jit.unroll_safe |
| 51 |
def product_check(s): |
| 52 |
i = 1 |
| 53 |
for x in s: |
| 54 |
try: |
| 55 |
i = ovfcheck(i * x) |
| 56 |
except OverflowError: |
| 57 |
raise |
| 58 |
return i |
| 59 |
|
| 60 |
def check_and_adjust_index(space, index, size, axis): |
| 61 |
if index < -size or index >= size: |
| 62 |
if axis >= 0: |
| 63 |
raise oefmt(space.w_IndexError, |
| 64 |
"index %d is out of bounds for axis %d with size %d", |
| 65 |
index, axis, size) |
| 66 |
else: |
| 67 |
raise oefmt(space.w_IndexError, |
| 68 |
"index %d is out of bounds for size %d", |
| 69 |
index, size) |
| 70 |
if index < 0: |
| 71 |
index += size |
| 72 |
return index |
| 73 |
|
| 74 |
def _next_non_white_space(s, offset): |
| 75 |
ret = offset |
| 76 |
while ret < len(s) and (s[ret] == ' ' or s[ret] == '\t'): |
| 77 |
ret += 1 |
| 78 |
if ret >= len(s): |
| 79 |
break |
| 80 |
return ret |
| 81 |
|
| 82 |
def _is_alpha_underscore(ch): |
| 83 |
return (ch >= 'A' and ch <= 'Z') or (ch >= 'a' and ch <= 'z') or ch == '_' |
| 84 |
|
| 85 |
def _is_alnum_underscore(ch): |
| 86 |
return _is_alpha_underscore(ch) or (ch >= '0' and ch <='9') |
| 87 |
|
| 88 |
def _parse_signature(space, ufunc, signature): |
| 89 |
''' |
| 90 |
rewritten from _parse_signature in numpy/core/src/umath/ufunc_object.c |
| 91 |
it takes a signature like '(),()->()' or '(i)->(i)' or '(i,j),(j,k)->(i,k)' |
| 92 |
and sets up the ufunc to handle the actual call appropriately |
| 93 |
|
| 94 |
cpython numpy chops the dim names i,j,k out of the signature using pointers with |
| 95 |
no copying, while faster than this code it seems like a marginally useful optimization. |
| 96 |
We copy them out into var_names |
| 97 |
''' |
| 98 |
i = _next_non_white_space(signature, 0) |
| 99 |
cur_arg = 0 |
| 100 |
cur_core_dim = 0 |
| 101 |
nd = 0 |
| 102 |
var_names = {} |
| 103 |
while i < len(signature): |
| 104 |
|
| 105 |
if cur_arg == ufunc.nin: |
| 106 |
if signature[i:i+2] != '->': |
| 107 |
raise oefmt(space.w_ValueError, '%s at %d in "%s"', |
| 108 |
"expect '->'", i, signature) |
| 109 |
i = _next_non_white_space(signature, i+2) |
| 110 |
|
| 111 |
|
| 112 |
if signature[i] != '(': |
| 113 |
raise oefmt(space.w_ValueError, '%s at %d in "%s"', |
| 114 |
"expect '('", i, signature) |
| 115 |
i = _next_non_white_space(signature, i+1) |
| 116 |
end_of_arg = signature.find(')', i) |
| 117 |
if end_of_arg < 0: |
| 118 |
raise oefmt(space.w_ValueError, '%s %d in "%s"', |
| 119 |
"could not find ')' after", i, signature) |
| 120 |
if end_of_arg == i: |
| 121 |
|
| 122 |
next_comma = -1 |
| 123 |
i += 1 |
| 124 |
else: |
| 125 |
next_comma = signature.find(',', i, end_of_arg) |
| 126 |
if next_comma < 0: |
| 127 |
next_comma = end_of_arg |
| 128 |
while next_comma > 0 and next_comma <= end_of_arg: |
| 129 |
|
| 130 |
name_end = next_comma - 1 |
| 131 |
while signature[name_end] == ' ' or signature[name_end] == '\t': |
| 132 |
name_end -= 1 |
| 133 |
if name_end < i: |
| 134 |
raise oefmt(space.w_ValueError, '%s at %d in "%s"', |
| 135 |
"expect dimension name", i, signature) |
| 136 |
var_name = signature[i:name_end + 1] |
| 137 |
for s in var_name: |
| 138 |
if not _is_alnum_underscore(s): |
| 139 |
raise oefmt(space.w_ValueError, '%s at %d in "%s"', |
| 140 |
"expect dimension name", i, signature) |
| 141 |
if var_name not in var_names: |
| 142 |
var_names[var_name] = ufunc.core_num_dim_ix |
| 143 |
ufunc.core_num_dim_ix += 1 |
| 144 |
ufunc.core_dim_ixs.append(var_names[var_name]) |
| 145 |
cur_core_dim += 1 |
| 146 |
nd += 1 |
| 147 |
i = next_comma |
| 148 |
if signature[i] == ',': |
| 149 |
i = _next_non_white_space(signature, i + 1); |
| 150 |
if signature[i] == ')': |
| 151 |
raise oefmt(space.w_ValueError, '%s at %d in "%s"', |
| 152 |
"',' must not be followed by ')'", i, signature) |
| 153 |
if end_of_arg <= i: |
| 154 |
next_comma = -1 |
| 155 |
i = end_of_arg + 1 |
| 156 |
else: |
| 157 |
next_comma = signature.find(',', i, end_of_arg) |
| 158 |
if next_comma < 0: |
| 159 |
next_comma = end_of_arg |
| 160 |
ufunc.core_num_dims[cur_arg] = nd |
| 161 |
ufunc.core_offsets[cur_arg] = cur_core_dim - nd |
| 162 |
cur_arg += 1 |
| 163 |
nd = 0 |
| 164 |
if i < len(signature): |
| 165 |
i = _next_non_white_space(signature, i) |
| 166 |
if cur_arg != ufunc.nin and cur_arg != ufunc.nargs: |
| 167 |
|
| 168 |
|
| 169 |
if signature[i] != ',': |
| 170 |
raise oefmt(space.w_ValueError, '%s at %d in "%s"', |
| 171 |
"expect ','", i, signature) |
| 172 |
i = _next_non_white_space(signature, i + 1); |
| 173 |
if cur_arg != ufunc.nargs: |
| 174 |
raise oefmt(space.w_ValueError, '%s at %d in "%s"', |
| 175 |
"incomplete signature: not all arguments found", i, signature) |
| 176 |
if cur_core_dim == 0: |
| 177 |
ufunc.core_enabled = False |
| 178 |
return 0 |
| 179 |
|
| 180 |
def get_storage_as_int(storage, start=0): |
| 181 |
return rffi.cast(lltype.Signed, storage) + start |
| 182 |
|
| 183 |
def is_rhs_priority_higher(space, w_lhs, w_rhs): |
| 184 |
w_zero = space.newfloat(0.0) |
| 185 |
w_priority_l = space.findattr(w_lhs, space.newtext('__array_priority__')) or w_zero |
| 186 |
w_priority_r = space.findattr(w_rhs, space.newtext('__array_priority__')) or w_zero |
| 187 |
|
| 188 |
return space.is_true(space.gt(w_priority_r, w_priority_l)) |
| 189 |
|
| 190 |
def get_order_as_CF(proto_order, req_order): |
| 191 |
if req_order == NPY.CORDER: |
| 192 |
return NPY.CORDER |
| 193 |
elif req_order == NPY.FORTRANORDER: |
| 194 |
return NPY.FORTRANORDER |
| 195 |
return proto_order |
| 196 |
|
| 197 |
def descr_set_docstring(space, w_obj, w_docstring): |
| 198 |
if not isinstance(space, StdObjSpace): |
| 199 |
raise oefmt(space.w_NotImplementedError, |
| 200 |
"This only works with the real object space") |
| 201 |
if isinstance(w_obj, W_TypeObject): |
| 202 |
w_obj.w_doc = w_docstring |
| 203 |
return |
| 204 |
elif isinstance(w_obj, GetSetProperty): |
| 205 |
if space.is_none(w_docstring): |
| 206 |
doc = None |
| 207 |
else: |
| 208 |
doc = space.text_w(w_docstring) |
| 209 |
w_obj.doc = doc |
| 210 |
return |
| 211 |
app_set_docstring(space, w_obj, w_docstring) |
| 212 |
|
| 213 |
app_set_docstring = appdef("""app_set_docstring_(obj, docstring): |
| 214 |
import types |
| 215 |
if isinstance(obj, types.MethodType): |
| 216 |
obj.im_func.__doc__ = docstring |
| 217 |
else: |
| 218 |
obj.__doc__ = docstring |
| 219 |
""") |