| 1 |
from rpython.rlib.rarithmetic import widen |
| 2 |
from pypy.module._hpy_universal.apiset import API |
| 3 |
|
| 4 |
def make_unary(name, spacemeth): |
| 5 |
assert spacemeth.startswith('space.') |
| 6 |
spacemeth = spacemeth[len('space.'):] |
| 7 |
|
| 8 |
@API.func("HPy HPy_unary(HPyContext *ctx, HPy h1)", func_name=name) |
| 9 |
def HPy_unary(space, handles, ctx, h1): |
| 10 |
w_obj1 = handles.deref(h1) |
| 11 |
meth = getattr(space, spacemeth) |
| 12 |
w_res = meth(w_obj1) |
| 13 |
return handles.new(w_res) |
| 14 |
|
| 15 |
globals()[name] = HPy_unary |
| 16 |
|
| 17 |
def make_binary(name, spacemeth): |
| 18 |
assert spacemeth.startswith('space.') |
| 19 |
spacemeth = spacemeth[len('space.'):] |
| 20 |
|
| 21 |
@API.func("HPy HPy_binary(HPyContext *ctx, HPy h1, HPy h2)", func_name=name) |
| 22 |
def HPy_binary(space, handles, ctx, h1, h2): |
| 23 |
w_obj1 = handles.deref(h1) |
| 24 |
w_obj2 = handles.deref(h2) |
| 25 |
meth = getattr(space, spacemeth) |
| 26 |
w_res = meth(w_obj1, w_obj2) |
| 27 |
return handles.new(w_res) |
| 28 |
|
| 29 |
globals()[name] = HPy_binary |
| 30 |
|
| 31 |
|
| 32 |
make_unary('HPy_Negative', 'space.neg') |
| 33 |
make_unary('HPy_Positive', 'space.pos') |
| 34 |
make_unary('HPy_Absolute', 'space.abs') |
| 35 |
make_unary('HPy_Invert', 'space.invert') |
| 36 |
make_unary('HPy_Index', 'space.index') |
| 37 |
|
| 38 |
make_binary('HPy_Add', 'space.add') |
| 39 |
make_binary('HPy_Subtract', 'space.sub') |
| 40 |
make_binary('HPy_Multiply', 'space.mul') |
| 41 |
make_binary('HPy_FloorDivide', 'space.floordiv') |
| 42 |
make_binary('HPy_TrueDivide', 'space.truediv') |
| 43 |
make_binary('HPy_Remainder', 'space.mod') |
| 44 |
make_binary('HPy_Divmod', 'space.divmod') |
| 45 |
make_binary('HPy_Lshift', 'space.lshift') |
| 46 |
make_binary('HPy_Rshift', 'space.rshift') |
| 47 |
make_binary('HPy_And', 'space.and_') |
| 48 |
make_binary('HPy_Xor', 'space.xor') |
| 49 |
make_binary('HPy_Or', 'space.or_') |
| 50 |
make_binary('HPy_MatrixMultiply', 'space.matmul') |
| 51 |
|
| 52 |
make_binary('HPy_InPlaceAdd', 'space.inplace_add') |
| 53 |
make_binary('HPy_InPlaceSubtract', 'space.inplace_sub'), |
| 54 |
make_binary('HPy_InPlaceMultiply', 'space.inplace_mul'), |
| 55 |
make_binary('HPy_InPlaceFloorDivide', 'space.inplace_floordiv'), |
| 56 |
make_binary('HPy_InPlaceTrueDivide', 'space.inplace_truediv'), |
| 57 |
make_binary('HPy_InPlaceRemainder', 'space.inplace_mod'), |
| 58 |
make_binary('HPy_InPlaceLshift', 'space.inplace_lshift'), |
| 59 |
make_binary('HPy_InPlaceRshift', 'space.inplace_rshift'), |
| 60 |
make_binary('HPy_InPlaceAnd', 'space.inplace_and'), |
| 61 |
make_binary('HPy_InPlaceXor', 'space.inplace_xor'), |
| 62 |
make_binary('HPy_InPlaceOr', 'space.inplace_or'), |
| 63 |
make_binary('HPy_InPlaceMatrixMultiply', 'space.inplace_matmul') |
| 64 |
|
| 65 |
|
| 66 |
@API.func("HPy HPy_Long(HPyContext *ctx, HPy h1)") |
| 67 |
def HPy_Long(space, handles, ctx, h1): |
| 68 |
w_obj1 = handles.deref(h1) |
| 69 |
w_res = space.call_function(space.w_int, w_obj1) |
| 70 |
return handles.new(w_res) |
| 71 |
|
| 72 |
|
| 73 |
@API.func("HPy HPy_Float(HPyContext *ctx, HPy h1)") |
| 74 |
def HPy_Float(space, handles, ctx, h1): |
| 75 |
w_obj1 = handles.deref(h1) |
| 76 |
w_res = space.call_function(space.w_float, w_obj1) |
| 77 |
return handles.new(w_res) |
| 78 |
|
| 79 |
|
| 80 |
@API.func("HPy HPy_Power(HPyContext *ctx, HPy h1, HPy h2, HPy h3)") |
| 81 |
def HPy_Power(space, handles, ctx, h1, h2, h3): |
| 82 |
w_o1 = handles.deref(h1) |
| 83 |
w_o2 = handles.deref(h2) |
| 84 |
w_o3 = handles.deref(h3) |
| 85 |
w_res = space.pow(w_o1, w_o2, w_o3) |
| 86 |
return handles.new(w_res) |
| 87 |
|
| 88 |
|
| 89 |
@API.func("HPy HPy_InPlacePower(HPyContext *ctx, HPy h1, HPy h2, HPy h3)") |
| 90 |
def HPy_InPlacePower(space, handles, ctx, h1, h2, h3): |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
w_o1 = handles.deref(h1) |
| 96 |
w_o2 = handles.deref(h2) |
| 97 |
w_o3 = handles.deref(h3) |
| 98 |
w_ipow = space.lookup(w_o1, '__ipow__') |
| 99 |
if w_ipow is None: |
| 100 |
w_res = space.pow(w_o1, w_o2, w_o3) |
| 101 |
else: |
| 102 |
w_res = space.inplace_pow(w_o1, w_o2) |
| 103 |
return handles.new(w_res) |
| 104 |
|
| 105 |
@API.func("int HPyNumber_Check(HPyContext *ctx, HPy h)", error_value='CANNOT_FAIL') |
| 106 |
def HPyNumber_Check(space, handles, ctx, h): |
| 107 |
|
| 108 |
w_obj = handles.deref(h) |
| 109 |
if (space.lookup(w_obj, '__int__') or space.lookup(w_obj, '__float__') or |
| 110 |
0): |
| 111 |
return API.int(1) |
| 112 |
return API.int(0) |
| 113 |
|
| 114 |
@API.func("HPy HPyBool_FromBool(HPyContext *ctx, int v)") |
| 115 |
def HPyBool_FromBool(space, handles, ctx, value): |
| 116 |
if widen(value) != 0: |
| 117 |
return handles.new(space.w_True) |
| 118 |
return handles.new(space.w_False) |