| 1 |
import os |
| 2 |
from rpython.rtyper.lltypesystem import lltype, rffi |
| 3 |
from rpython.rlib.debug import ll_assert |
| 4 |
from pypy.interpreter.error import OperationError, oefmt |
| 5 |
import pypy.module.__builtin__.operation as operation |
| 6 |
from pypy.objspace.std.bytesobject import invoke_bytes_method |
| 7 |
from pypy.module._hpy_universal.apiset import API |
| 8 |
from . import llapi |
| 9 |
|
| 10 |
HPy_RichCmpOp = llapi.cts.gettype('HPy_RichCmpOp') |
| 11 |
|
| 12 |
@API.func("int HPy_IsTrue(HPyContext *ctx, HPy h)", error_value=API.int(-1)) |
| 13 |
def HPy_IsTrue(space, handles, ctx, h_obj): |
| 14 |
w_obj = handles.deref(h_obj) |
| 15 |
return API.int(space.is_true(w_obj)) |
| 16 |
|
| 17 |
@API.func("HPy HPy_GetAttr(HPyContext *ctx, HPy obj, HPy h_name)") |
| 18 |
def HPy_GetAttr(space, handles, ctx, h_obj, h_name): |
| 19 |
w_obj = handles.deref(h_obj) |
| 20 |
w_name = handles.deref(h_name) |
| 21 |
w_res = space.getattr(w_obj, w_name) |
| 22 |
return handles.new(w_res) |
| 23 |
|
| 24 |
@API.func("HPy HPy_GetAttr_s(HPyContext *ctx, HPy h_obj, const char *name)") |
| 25 |
def HPy_GetAttr_s(space, handles, ctx, h_obj, name): |
| 26 |
w_obj = handles.deref(h_obj) |
| 27 |
w_name = API.ccharp2text(space, name) |
| 28 |
w_res = space.getattr(w_obj, w_name) |
| 29 |
return handles.new(w_res) |
| 30 |
|
| 31 |
|
| 32 |
@API.func("int HPy_HasAttr(HPyContext *ctx, HPy h_obj, HPy h_name)", |
| 33 |
error_value='CANNOT_FAIL') |
| 34 |
def HPy_HasAttr(space, handles, ctx, h_obj, h_name): |
| 35 |
w_obj = handles.deref(h_obj) |
| 36 |
w_name = handles.deref(h_name) |
| 37 |
return _HasAttr(space, w_obj, w_name) |
| 38 |
|
| 39 |
@API.func("int HPy_HasAttr_s(HPyContext *ctx, HPy h_obj, const char *name)", |
| 40 |
error_value='CANNOT_FAIL') |
| 41 |
def HPy_HasAttr_s(space, handles, ctx, h_obj, name): |
| 42 |
w_obj = handles.deref(h_obj) |
| 43 |
w_name = API.ccharp2text(space, name) |
| 44 |
return _HasAttr(space, w_obj, w_name) |
| 45 |
|
| 46 |
def _HasAttr(space, w_obj, w_name): |
| 47 |
try: |
| 48 |
w_res = operation.hasattr(space, w_obj, w_name) |
| 49 |
return API.int(space.is_true(w_res)) |
| 50 |
except OperationError: |
| 51 |
return API.int(0) |
| 52 |
|
| 53 |
|
| 54 |
@API.func("int HPy_SetAttr(HPyContext *ctx, HPy h_obj, HPy h_name, HPy h_value)", |
| 55 |
error_value=API.int(-1)) |
| 56 |
def HPy_SetAttr(space, handles, ctx, h_obj, h_name, h_value): |
| 57 |
w_obj = handles.deref(h_obj) |
| 58 |
w_name = handles.deref(h_name) |
| 59 |
if not h_value: |
| 60 |
operation.delattr(space, w_obj, w_name) |
| 61 |
else: |
| 62 |
w_value = handles.deref(h_value) |
| 63 |
operation.setattr(space, w_obj, w_name, w_value) |
| 64 |
return API.int(0) |
| 65 |
|
| 66 |
@API.func("int HPy_SetAttr_s(HPyContext *ctx, HPy h_obj, const char *name, HPy h_value)", |
| 67 |
error_value=API.int(-1)) |
| 68 |
def HPy_SetAttr_s(space, handles, ctx, h_obj, name, h_value): |
| 69 |
w_obj = handles.deref(h_obj) |
| 70 |
w_name = API.ccharp2text(space, name) |
| 71 |
if not h_value: |
| 72 |
operation.delattr(space, w_obj, w_name) |
| 73 |
else: |
| 74 |
w_value = handles.deref(h_value) |
| 75 |
operation.setattr(space, w_obj, w_name, w_value) |
| 76 |
return API.int(0) |
| 77 |
|
| 78 |
|
| 79 |
@API.func("int HPyCallable_Check(HPyContext *ctx, HPy h)", error_value='CANNOT_FAIL') |
| 80 |
def HPyCallable_Check(space, handles, ctx, h_obj): |
| 81 |
w_obj = handles.deref(h_obj) |
| 82 |
return API.int(space.is_true(space.callable(w_obj))) |
| 83 |
|
| 84 |
|
| 85 |
@API.func("HPy HPy_GetItem(HPyContext *ctx, HPy h_obj, HPy h_key)") |
| 86 |
def HPy_GetItem(space, handles, ctx, h_obj, h_key): |
| 87 |
w_obj = handles.deref(h_obj) |
| 88 |
w_key = handles.deref(h_key) |
| 89 |
w_res = space.getitem(w_obj, w_key) |
| 90 |
return handles.new(w_res) |
| 91 |
|
| 92 |
@API.func("HPy HPy_GetItem_i(HPyContext *ctx, HPy h_obj, HPy_ssize_t idx)") |
| 93 |
def HPy_GetItem_i(space, handles, ctx, h_obj, idx): |
| 94 |
w_obj = handles.deref(h_obj) |
| 95 |
w_key = space.newint(idx) |
| 96 |
w_res = space.getitem(w_obj, w_key) |
| 97 |
return handles.new(w_res) |
| 98 |
|
| 99 |
@API.func("HPy HPy_GetItem_s(HPyContext *ctx, HPy h_obj, const char *key)") |
| 100 |
def HPy_GetItem_s(space, handles, ctx, h_obj, key): |
| 101 |
w_obj = handles.deref(h_obj) |
| 102 |
w_key = API.ccharp2text(space, key) |
| 103 |
w_res = space.getitem(w_obj, w_key) |
| 104 |
return handles.new(w_res) |
| 105 |
|
| 106 |
|
| 107 |
@API.func("int HPy_SetItem(HPyContext *ctx, HPy h_obj, HPy h_key, HPy h_val)", |
| 108 |
error_value=API.int(-1)) |
| 109 |
def HPy_SetItem(space, handles, ctx, h_obj, h_key, h_val): |
| 110 |
w_obj = handles.deref(h_obj) |
| 111 |
w_key = handles.deref(h_key) |
| 112 |
w_val = handles.deref(h_val) |
| 113 |
space.setitem(w_obj, w_key, w_val) |
| 114 |
return API.int(0) |
| 115 |
|
| 116 |
@API.func("int HPy_SetItem_i(HPyContext *ctx, HPy h_obj, HPy_ssize_t idx, HPy h_val)", |
| 117 |
error_value=API.int(-1)) |
| 118 |
def HPy_SetItem_i(space, handles, ctx, h_obj, idx, h_val): |
| 119 |
w_obj = handles.deref(h_obj) |
| 120 |
w_key = space.newint(idx) |
| 121 |
w_val = handles.deref(h_val) |
| 122 |
space.setitem(w_obj, w_key, w_val) |
| 123 |
return API.int(0) |
| 124 |
|
| 125 |
@API.func("int HPy_SetItem_s(HPyContext *ctx, HPy h_obj, const char *key, HPy h_val)", |
| 126 |
error_value=API.int(-1)) |
| 127 |
def HPy_SetItem_s(space, handles, ctx, h_obj, key, h_val): |
| 128 |
w_obj = handles.deref(h_obj) |
| 129 |
w_key = API.ccharp2text(space, key) |
| 130 |
w_val = handles.deref(h_val) |
| 131 |
space.setitem(w_obj, w_key, w_val) |
| 132 |
return API.int(0) |
| 133 |
|
| 134 |
@API.func("HPy HPy_Repr(HPyContext *ctx, HPy h_obj)") |
| 135 |
def HPy_Repr(space, handles, ctx, h_obj): |
| 136 |
w_obj = handles.deref(h_obj) |
| 137 |
if w_obj is None: |
| 138 |
w_res = space.newtext("<NULL>") |
| 139 |
else: |
| 140 |
w_res = space.repr(w_obj) |
| 141 |
return handles.new(w_res) |
| 142 |
|
| 143 |
@API.func("HPy HPy_Str(HPyContext *ctx, HPy h_obj)") |
| 144 |
def HPy_Str(space, handles, ctx, h_obj): |
| 145 |
w_obj = handles.deref(h_obj) |
| 146 |
if w_obj is None: |
| 147 |
w_res = space.newtext("<NULL>") |
| 148 |
else: |
| 149 |
w_res = space.str(w_obj) |
| 150 |
return handles.new(w_res) |
| 151 |
|
| 152 |
@API.func("HPy HPy_ASCII(HPyContext *ctx, HPy h_obj)") |
| 153 |
def HPy_ASCII(space, handles, ctx, h_obj): |
| 154 |
w_obj = handles.deref(h_obj) |
| 155 |
w_res = operation.ascii(space, w_obj) |
| 156 |
return handles.new(w_res) |
| 157 |
|
| 158 |
@API.func("HPy HPy_Bytes(HPyContext *ctx, HPy h_obj)") |
| 159 |
def HPy_Bytes(space, handles, ctx, h_obj): |
| 160 |
|
| 161 |
w_obj = handles.deref(h_obj) |
| 162 |
if space.type(w_obj) is space.w_bytes: |
| 163 |
|
| 164 |
return handles.dup(h_obj) |
| 165 |
w_result = invoke_bytes_method(space, w_obj) |
| 166 |
if w_result is not None: |
| 167 |
return handles.new(w_result) |
| 168 |
|
| 169 |
|
| 170 |
buffer = space.buffer_w(w_obj, space.BUF_FULL_RO) |
| 171 |
w_res = space.newbytes(buffer.as_str()) |
| 172 |
return handles.new(w_res) |
| 173 |
|
| 174 |
@API.func("HPy HPy_RichCompare(HPyContext *ctx, HPy v, HPy w, int op)") |
| 175 |
def HPy_RichCompare(space, handles, ctx, v, w, op): |
| 176 |
w_o1 = handles.deref(v) |
| 177 |
w_o2 = handles.deref(w) |
| 178 |
w_result = rich_compare(space, w_o1, w_o2, op) |
| 179 |
return handles.new(w_result) |
| 180 |
|
| 181 |
def rich_compare(space, w_o1, w_o2, opid_int): |
| 182 |
opid = rffi.cast(lltype.Signed, opid_int) |
| 183 |
if opid == HPy_RichCmpOp.HPy_LT: |
| 184 |
return space.lt(w_o1, w_o2) |
| 185 |
elif opid == HPy_RichCmpOp.HPy_LE: |
| 186 |
return space.le(w_o1, w_o2) |
| 187 |
elif opid == HPy_RichCmpOp.HPy_EQ: |
| 188 |
return space.eq(w_o1, w_o2) |
| 189 |
elif opid == HPy_RichCmpOp.HPy_NE: |
| 190 |
return space.ne(w_o1, w_o2) |
| 191 |
elif opid == HPy_RichCmpOp.HPy_GT: |
| 192 |
return space.gt(w_o1, w_o2) |
| 193 |
elif opid == HPy_RichCmpOp.HPy_GE: |
| 194 |
return space.ge(w_o1, w_o2) |
| 195 |
else: |
| 196 |
raise oefmt(space.w_SystemError, "Bad internal call!") |
| 197 |
|
| 198 |
|
| 199 |
@API.func("int HPy_RichCompareBool(HPyContext *ctx, HPy v, HPy w, int op)", |
| 200 |
error_value=API.int(-1)) |
| 201 |
def HPy_RichCompareBool(space, handles, ctx, v, w, op): |
| 202 |
w_o1 = handles.deref(v) |
| 203 |
w_o2 = handles.deref(w) |
| 204 |
|
| 205 |
|
| 206 |
if space.is_w(w_o1, w_o2): |
| 207 |
opid = rffi.cast(lltype.Signed, op) |
| 208 |
if opid == HPy_RichCmpOp.HPy_EQ: |
| 209 |
return API.int(1) |
| 210 |
if opid == HPy_RichCmpOp.HPy_NE: |
| 211 |
return API.int(0) |
| 212 |
w_result = rich_compare(space, w_o1, w_o2, op) |
| 213 |
return API.int(space.is_true(w_result)) |
| 214 |
|
| 215 |
@API.func("HPy_hash_t HPy_Hash(HPyContext *ctx, HPy obj)", error_value=-1) |
| 216 |
def HPy_Hash(space, handles, ctx, h_obj): |
| 217 |
w_obj = handles.deref(h_obj) |
| 218 |
return API.cts.cast('HPy_hash_t', space.hash_w(w_obj)) |
| 219 |
|
| 220 |
@API.func("HPy_ssize_t HPy_Length(HPyContext *ctx, HPy h)", error_value=-1) |
| 221 |
def HPy_Length(space, handles, ctx, h_obj): |
| 222 |
w_obj = handles.deref(h_obj) |
| 223 |
if not w_obj: |
| 224 |
|
| 225 |
raise oefmt(space.w_SystemError, "invalid object") |
| 226 |
return space.len_w(w_obj) |
| 227 |
|
| 228 |
@API.func("HPy HPy_Type(HPyContext *ctx, HPy obj)") |
| 229 |
def HPy_Type(space, handles, ctx, h_obj): |
| 230 |
w_obj = handles.deref(h_obj) |
| 231 |
return handles.new(space.type(w_obj)) |
| 232 |
|
| 233 |
@API.func("int HPy_TypeCheck(HPyContext *ctx, HPy obj, HPy type)", |
| 234 |
error_value=API.int(0)) |
| 235 |
def HPy_TypeCheck(space, handles, ctx, h_obj, h_type): |
| 236 |
w_obj = handles.deref(h_obj) |
| 237 |
w_type = handles.deref(h_type) |
| 238 |
ll_assert(space.isinstance_w(w_type, space.w_type), "h_type is not a type object") |
| 239 |
assert space.isinstance_w(w_type, space.w_type) |
| 240 |
try: |
| 241 |
return API.int(space.issubtype_w(space.type(w_obj), w_type)) |
| 242 |
except: |
| 243 |
|
| 244 |
pass |
| 245 |
return API.int(0) |
| 246 |
|
| 247 |
@API.func("int HPy_Is(HPyContext *ctx, HPy obj, HPy other)", |
| 248 |
error_value='CANNOT_FAIL') |
| 249 |
def HPy_Is(space, handles, ctx, h_obj, h_other): |
| 250 |
if not h_obj: |
| 251 |
return API.int(not bool(h_other)) |
| 252 |
if not h_other: |
| 253 |
return API.int(0) |
| 254 |
w_obj = handles.deref(h_obj) |
| 255 |
w_other = handles.deref(h_other) |
| 256 |
return API.int(space.is_w(w_obj, w_other)) |
| 257 |
|
| 258 |
@API.func("void _HPy_Dump(HPyContext *ctx, HPy h)") |
| 259 |
def _HPy_Dump(space, handles, ctx, h_obj): |
| 260 |
|
| 261 |
|
| 262 |
stderr = 2 |
| 263 |
w_obj = handles.deref(h_obj) |
| 264 |
w_type = space.type(w_obj) |
| 265 |
os.write(stderr, "object type : %r\n" % (w_type,)) |
| 266 |
os.write(stderr, "object type name: %s\n" % (w_type.name,)) |
| 267 |
os.write(stderr, "object rpy repr : %r\n" % (w_obj,)) |
| 268 |
try: |
| 269 |
w_repr = space.repr(w_obj) |
| 270 |
s = space.text_w(w_repr) |
| 271 |
os.write(stderr, "object repr : %s\n" % (s,)) |
| 272 |
except: |
| 273 |
os.write(stderr, "objec repr : <failed>\n") |
| 274 |
|
| 275 |
@API.func("int _HPy_Contains(HPyContext *ctx, HPy container, HPy key)", error_value=API.int(-1)) |
| 276 |
def _HPy_Contains(space, handles, ctx, h_container, h_key): |
| 277 |
w_container = handles.deref(h_container) |
| 278 |
w_key = handles.deref(h_key) |
| 279 |
w_res = space.contains(w_container, w_key) |
| 280 |
return API.int(space.int_w(w_res)) |
| 281 |
|
| 282 |
@API.func("int HPy_DelItem(HPyContext *ctx, HPy obj, HPy key)", error_value=API.int(-1)) |
| 283 |
def HPy_DelItem(space, handles, ctx, h_obj, h_key): |
| 284 |
w_obj = handles.deref(h_obj) |
| 285 |
w_key = handles.deref(h_key) |
| 286 |
space.delitem(w_obj, w_key) |
| 287 |
return API.int(0) |
| 288 |
|
| 289 |
@API.func("int HPy_DelItem_i(HPyContext *ctx, HPy obj, HPy_ssize_t key)", error_value=API.int(-1)) |
| 290 |
def HPy_DelItem_i(space, handles, ctx, h_obj, key): |
| 291 |
w_obj = handles.deref(h_obj) |
| 292 |
w_key = space.newint(key) |
| 293 |
space.delitem(w_obj, w_key) |
| 294 |
return API.int(0) |
| 295 |
|
| 296 |
@API.func("int HPy_DelItem_s(HPyContext *ctx, HPy obj, const char *key)", error_value=API.int(-1)) |
| 297 |
def HPy_DelItem_s(space, handles, ctx, h_obj, key): |
| 298 |
w_obj = handles.deref(h_obj) |
| 299 |
w_key = API.ccharp2text(space, key) |
| 300 |
space.delitem(w_obj, w_key) |
| 301 |
return API.int(0) |