| 1 | from pypy.interpreter.error import OperationError, oefmt |
| 2 | from pypy.module.micronumpy import constants as NPY |
| 3 | |
| 4 | |
| 5 | def byteorder_converter(space, new_order): |
| 6 | endian = new_order[0] |
| 7 | if endian not in (NPY.BIG, NPY.LITTLE, NPY.NATIVE, NPY.IGNORE, NPY.SWAP): |
| 8 | ch = endian |
| 9 | if ch in ('b', 'B'): |
| 10 | endian = NPY.BIG |
| 11 | elif ch in ('l', 'L'): |
| 12 | endian = NPY.LITTLE |
| 13 | elif ch in ('n', 'N'): |
| 14 | endian = NPY.NATIVE |
| 15 | elif ch in ('i', 'I'): |
| 16 | endian = NPY.IGNORE |
| 17 | elif ch in ('s', 'S'): |
| 18 | endian = NPY.SWAP |
| 19 | else: |
| 20 | raise oefmt(space.w_ValueError, |
| 21 | "%s is an unrecognized byteorder", new_order) |
| 22 | return endian |
| 23 | |
| 24 | |
| 25 | def clipmode_converter(space, w_mode): |
| 26 | if space.is_none(w_mode): |
| 27 | return NPY.RAISE |
| 28 | if space.isinstance_w(w_mode, space.w_text): |
| 29 | mode = space.text_w(w_mode) |
| 30 | if mode.startswith('C') or mode.startswith('c'): |
| 31 | return NPY.CLIP |
| 32 | if mode.startswith('W') or mode.startswith('w'): |
| 33 | return NPY.WRAP |
| 34 | if mode.startswith('R') or mode.startswith('r'): |
| 35 | return NPY.RAISE |
| 36 | elif space.isinstance_w(w_mode, space.w_int): |
| 37 | mode = space.int_w(w_mode) |
| 38 | if NPY.CLIP <= mode <= NPY.RAISE: |
| 39 | return mode |
| 40 | raise oefmt(space.w_TypeError, "clipmode not understood") |
| 41 | |
| 42 | |
| 43 | def searchside_converter(space, w_obj): |
| 44 | try: |
| 45 | s = space.text_w(w_obj) |
| 46 | except OperationError: |
| 47 | s = None |
| 48 | if not s: |
| 49 | raise oefmt(space.w_ValueError, |
| 50 | "expected nonempty string for keyword 'side'") |
| 51 | if s[0] == 'l' or s[0] == 'L': |
| 52 | return NPY.SEARCHLEFT |
| 53 | elif s[0] == 'r' or s[0] == 'R': |
| 54 | return NPY.SEARCHRIGHT |
| 55 | else: |
| 56 | raise oefmt(space.w_ValueError, |
| 57 | "'%s' is an invalid value for keyword 'side'", s) |
| 58 | |
| 59 | |
| 60 | def order_converter(space, w_order, default): |
| 61 | if space.is_none(w_order): |
| 62 | return default |
| 63 | if not space.isinstance_w(w_order, space.w_text): |
| 64 | if space.is_true(w_order): |
| 65 | return NPY.FORTRANORDER |
| 66 | else: |
| 67 | return NPY.CORDER |
| 68 | else: |
| 69 | order = space.text_w(w_order) |
| 70 | if order.startswith('C') or order.startswith('c'): |
| 71 | return NPY.CORDER |
| 72 | elif order.startswith('F') or order.startswith('f'): |
| 73 | return NPY.FORTRANORDER |
| 74 | elif order.startswith('A') or order.startswith('a'): |
| 75 | return NPY.ANYORDER |
| 76 | elif order.startswith('K') or order.startswith('k'): |
| 77 | return NPY.KEEPORDER |
| 78 | else: |
| 79 | raise oefmt(space.w_TypeError, "Unknown order: '%s'", order) |
| 80 | return -1 |
| 81 | |
| 82 | def multi_axis_converter(space, w_axis, ndim): |
| 83 | if space.is_none(w_axis): |
| 84 | return [True] * ndim |
| 85 | out = [False] * ndim |
| 86 | if not space.isinstance_w(w_axis, space.w_tuple): |
| 87 | w_axis = space.newtuple([w_axis]) |
| 88 | for w_item in space.fixedview(w_axis): |
| 89 | item = space.int_w(w_item) |
| 90 | axis = item |
| 91 | if axis < 0: |
| 92 | axis += ndim |
| 93 | if axis < 0 or axis >= ndim: |
| 94 | raise oefmt(space.w_ValueError, |
| 95 | "'axis' entry %d is out of bounds [-%d, %d)", |
| 96 | item, ndim, ndim) |
| 97 | if out[axis]: |
| 98 | raise oefmt(space.w_ValueError, "duplicate value in 'axis'") |
| 99 | out[axis] = True |
| 100 | return out |
| 101 | |
| 102 | |
| 103 | def shape_converter(space, w_size, dtype): |
| 104 | if space.is_none(w_size): |
| 105 | return [] |
| 106 | if space.isinstance_w(w_size, space.w_int): |
| 107 | return [space.int_w(w_size)] |
| 108 | shape = [] |
| 109 | for w_item in space.fixedview(w_size): |
| 110 | shape.append(space.int_w(w_item)) |
| 111 | shape += dtype.shape |
| 112 | return shape[:] |
| 113 | |
| 114 | def out_converter(space, w_out): |
| 115 | from .ndarray import W_NDimArray |
| 116 | if space.is_none(w_out): |
| 117 | return None |
| 118 | elif not isinstance(w_out, W_NDimArray): |
| 119 | raise oefmt(space.w_TypeError, 'output must be an array') |
| 120 | else: |
| 121 | return w_out |