| 1 | import sys |
| 2 | from pypy.interpreter.error import oefmt |
| 3 | from pypy.objspace.std.sliceobject import W_SliceObject |
| 4 | from .apiset import API |
| 5 | from pypy.interpreter.error import OperationError, oefmt |
| 6 | |
| 7 | @API.func("int HPySlice_Unpack(HPyContext *ctx, HPy h, HPy_ssize_t *start, HPy_ssize_t *stop, HPy_ssize_t *step)", error_value=API.int(-1)) |
| 8 | def HPySlice_Unpack(space, handles, ctx, h, start_p, stop_p, step_p): |
| 9 | if not h: |
| 10 | raise oefmt(space.w_SystemError, "NULL handle passed to HPySlice_Unpack") |
| 11 | w_slice = handles.deref(h) |
| 12 | if not isinstance(w_slice, W_SliceObject): |
| 13 | raise oefmt(space.w_SystemError, "non-slice passed to HPySlice_Unpack") |
| 14 | |
| 15 | if space.is_none(w_slice.w_step): |
| 16 | step = 1 |
| 17 | else: |
| 18 | step = W_SliceObject.eval_slice_index(space, w_slice.w_step) |
| 19 | if step == 0: |
| 20 | raise oefmt(space.w_ValueError, "slice step cannot be zero") |
| 21 | if step < -sys.maxint: |
| 22 | step = -sys.maxint |
| 23 | step_p[0] = step |
| 24 | |
| 25 | if space.is_none(w_slice.w_start): |
| 26 | start = sys.maxint if step < 0 else 0 |
| 27 | else: |
| 28 | start = W_SliceObject.eval_slice_index(space, w_slice.w_start) |
| 29 | start_p[0] = start |
| 30 | |
| 31 | if space.is_none(w_slice.w_stop): |
| 32 | stop = -sys.maxint-1 if step < 0 else sys.maxint |
| 33 | else: |
| 34 | stop = W_SliceObject.eval_slice_index(space, w_slice.w_stop) |
| 35 | stop_p[0] = stop |
| 36 | |
| 37 | return API.int(0) |
| 38 | |
| 39 | @API.func("HPy HPySlice_New(HPyContext *ctx, HPy start, HPy stop, HPy step)") |
| 40 | def HPySlice_New(space, handles, ctx, h_start, h_stop, h_step): |
| 41 | w_start = handles.deref(h_start) |
| 42 | w_stop = handles.deref(h_stop) |
| 43 | w_step = handles.deref(h_step) |
| 44 | return handles.new(W_SliceObject(w_start, w_stop, w_step)) |
| 45 | |
| 46 | @API.func("int HPyIter_Check(HPyContext *ctx, HPy obj)", error_value=API.int(-1)) |
| 47 | def HPyIter_Check(space, handles, ctx, h_obj): |
| 48 | w_obj = handles.deref(h_obj) |
| 49 | try: |
| 50 | w_attr = space.getattr(space.type(w_obj), space.newtext("__next__")) |
| 51 | except: |
| 52 | pass |
| 53 | else: |
| 54 | if space.is_true(space.callable(w_attr)): |
| 55 | return API.int(1) |
| 56 | return API.int(0) |
| 57 | |
| 58 | @API.func("HPy HPy_GetIter(HPyContext *ctx, HPy obj)") |
| 59 | def HPy_GetIter(space, handles, ctx, h_obj): |
| 60 | w_obj = handles.deref(h_obj) |
| 61 | return handles.new(space.iter(w_obj)) |
| 62 | |
| 63 | @API.func("HPy HPyIter_Next(HPyContext *ctx, HPy obj)") |
| 64 | def HPyIter_Next(space, handles, ctx, h_obj): |
| 65 | w_obj = handles.deref(h_obj) |
| 66 | try: |
| 67 | ret = space.next(w_obj) |
| 68 | except OperationError as e: |
| 69 | if not e.match(space, space.w_StopIteration): |
| 70 | raise |
| 71 | return 0 |
| 72 | return handles.new(ret) |
| 73 | |
| 74 | @API.func("int HPy_DelSlice(HPyContext *ctx, HPy obj, HPy_ssize_t start, HPy_ssize_t end)", error_value=API.int(-1)) |
| 75 | def HPy_DelSlice(space, handles, ctx, h_obj, start, end): |
| 76 | if not h_obj: |
| 77 | raise oefmt(space.w_SystemError, "NULL handle passed to HPy_DelSlice") |
| 78 | w_obj = handles.deref(h_obj) |
| 79 | space.delslice(w_obj, space.newint(start), space.newint(end)) |
| 80 | return API.int(0) |
| 81 | |
| 82 | @API.func("int HPy_SetSlice(HPyContext *ctx, HPy obj, HPy_ssize_t start, HPy_ssize_t end, HPy value)", error_value=API.int(-1)) |
| 83 | def HPy_SetSlice(space, handles, ctx, h_obj, start, end, h_value): |
| 84 | if not h_obj: |
| 85 | raise oefmt(space.w_SystemError, "NULL handle passed to HPy_SetSlice") |
| 86 | w_obj = handles.deref(h_obj) |
| 87 | if not h_value: |
| 88 | space.delslice(w_obj, space.newint(start), space.newint(end)) |
| 89 | return API.int(0) |
| 90 | w_value = handles.deref(h_value) |
| 91 | space.setslice(w_obj, space.newint(start), space.newint(end), w_value) |
| 92 | return API.int(0) |
| 93 | |
| 94 | @API.func("HPy HPy_GetSlice(HPyContext *ctx, HPy obj, HPy_ssize_t start, HPy_ssize_t end)") |
| 95 | def HPy_GetSlice(space, handles, ctx, h_obj, start, end): |
| 96 | if not h_obj: |
| 97 | raise oefmt(space.w_SystemError, "NULL handle passed to HPy_GetSlice") |
| 98 | w_obj = handles.deref(h_obj) |
| 99 | ret = space.getslice(w_obj, space.newint(start), space.newint(end)) |
| 100 | return handles.new(ret) |
| 101 |