| 1 | from rpython.rtyper.lltypesystem import rffi, lltype |
| 2 | from pypy.interpreter.error import OperationError, oefmt |
| 3 | from pypy.objspace.std.dictmultiobject import W_DictMultiObject |
| 4 | from pypy.module._hpy_universal.apiset import API |
| 5 | from pypy.module._hpy_universal import llapi |
| 6 | |
| 7 | @API.func("HPy HPyDict_New(HPyContext *ctx)") |
| 8 | def HPyDict_New(space, handles, ctx): |
| 9 | w_dict = space.newdict() |
| 10 | return handles.new(w_dict) |
| 11 | |
| 12 | @API.func("int HPyDict_Check(HPyContext *ctx, HPy h)", error_value='CANNOT_FAIL') |
| 13 | def HPyDict_Check(space, handles, ctx, h): |
| 14 | w_obj = handles.deref(h) |
| 15 | w_obj_type = space.type(w_obj) |
| 16 | res = (space.is_w(w_obj_type, space.w_dict) or |
| 17 | space.issubtype_w(w_obj_type, space.w_dict)) |
| 18 | return API.int(res) |
| 19 | |
| 20 | @API.func("HPy HPyDict_Copy(HPyContext *ctx, HPy h)") |
| 21 | def HPyDict_Copy(space, handles, ctx, h): |
| 22 | if not h: |
| 23 | raise oefmt(space.w_SystemError, "NULL handle passed to HPyDict_Copy") |
| 24 | w_obj = handles.deref(h) |
| 25 | w_obj_type = space.type(w_obj) |
| 26 | if not (space.is_w(w_obj_type, space.w_dict) or |
| 27 | space.issubtype_w(w_obj_type, space.w_dict)): |
| 28 | raise oefmt(space.w_SystemError, "non-dict passed to HPyDict_Copy") |
| 29 | w_ret = space.call_method(space.w_dict, "copy", w_obj) |
| 30 | return handles.new(w_ret) |
| 31 | |
| 32 | @API.func("HPy HPyDict_Keys(HPyContext *ctx, HPy h)") |
| 33 | def HPyDict_Keys(space, handles, ctx, h): |
| 34 | if not h: |
| 35 | raise oefmt(space.w_SystemError, "NULL handle passed to HPyDict_Keys") |
| 36 | w_obj = handles.deref(h) |
| 37 | w_obj_type = space.type(w_obj) |
| 38 | if not (space.is_w(w_obj_type, space.w_dict) or |
| 39 | space.issubtype_w(w_obj_type, space.w_dict)): |
| 40 | raise oefmt(space.w_SystemError, "non-dict passed to HPyDict_Keys") |
| 41 | w_k = space.call_method(space.w_dict, "keys", w_obj) |
| 42 | w_ret = space.call_function(space.w_list, w_k) |
| 43 | return handles.new(w_ret) |