| 1 | from rpython.rtyper.lltypesystem import lltype, rffi |
| 2 | from pypy.module.cpyext.api import ( |
| 3 | cpython_api, CANNOT_FAIL, CONST_STRING, Py_ssize_t) |
| 4 | from pypy.module.cpyext.pyobject import ( |
| 5 | PyObject, hack_for_result_often_existing_obj) |
| 6 | |
| 7 | |
| 8 | @cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL) |
| 9 | def PyMapping_Check(space, w_obj): |
| 10 | """Return 1 if the object provides mapping protocol, and 0 otherwise. This |
| 11 | function always succeeds.""" |
| 12 | return int(space.ismapping_w(w_obj)) |
| 13 | |
| 14 | @cpython_api([PyObject], Py_ssize_t, error=-1) |
| 15 | def PyMapping_Size(space, w_obj): |
| 16 | return space.len_w(w_obj) |
| 17 | |
| 18 | @cpython_api([PyObject], Py_ssize_t, error=-1) |
| 19 | def PyMapping_Length(space, w_obj): |
| 20 | return space.len_w(w_obj) |
| 21 | |
| 22 | @cpython_api([PyObject], PyObject) |
| 23 | def PyMapping_Keys(space, w_obj): |
| 24 | """On success, return a list of the keys in object o. On failure, return NULL. |
| 25 | This is equivalent to the Python expression o.keys().""" |
| 26 | return space.call_function(space.w_list, |
| 27 | space.call_method(w_obj, "keys")) |
| 28 | |
| 29 | @cpython_api([PyObject], PyObject) |
| 30 | def PyMapping_Values(space, w_obj): |
| 31 | """On success, return a list of the values in object o. On failure, return |
| 32 | NULL. This is equivalent to the Python expression o.values().""" |
| 33 | return space.call_function(space.w_list, |
| 34 | space.call_method(w_obj, "values")) |
| 35 | |
| 36 | @cpython_api([PyObject], PyObject) |
| 37 | def PyMapping_Items(space, w_obj): |
| 38 | """On success, return a list of the items in object o, where each item is a tuple |
| 39 | containing a key-value pair. On failure, return NULL. This is equivalent to |
| 40 | the Python expression o.items().""" |
| 41 | return space.call_function(space.w_list, |
| 42 | space.call_method(w_obj, "items")) |
| 43 | |
| 44 | @cpython_api([PyObject, CONST_STRING], PyObject, result_is_ll=True) |
| 45 | def PyMapping_GetItemString(space, w_obj, key): |
| 46 | """Return element of o corresponding to the object key or NULL on failure. |
| 47 | This is the equivalent of the Python expression o[key].""" |
| 48 | w_key = space.newtext(rffi.charp2str(key)) |
| 49 | w_res = space.getitem(w_obj, w_key) |
| 50 | return hack_for_result_often_existing_obj(space, w_res) |
| 51 | |
| 52 | @cpython_api([PyObject, CONST_STRING, PyObject], rffi.INT_real, error=-1) |
| 53 | def PyMapping_SetItemString(space, w_obj, key, w_value): |
| 54 | """Map the object key to the value v in object o. Returns -1 on failure. |
| 55 | This is the equivalent of the Python statement o[key] = v.""" |
| 56 | w_key = space.newtext(rffi.charp2str(key)) |
| 57 | space.setitem(w_obj, w_key, w_value) |
| 58 | return 0 |
| 59 | |
| 60 | @cpython_api([PyObject, PyObject], rffi.INT_real, error=CANNOT_FAIL) |
| 61 | def PyMapping_HasKey(space, w_obj, w_key): |
| 62 | """Return 1 if the mapping object has the key key and 0 otherwise. |
| 63 | This is equivalent to o[key], returning True on success and False |
| 64 | on an exception. This function always succeeds.""" |
| 65 | try: |
| 66 | space.getitem(w_obj, w_key) |
| 67 | return 1 |
| 68 | except: |
| 69 | return 0 |
| 70 | |
| 71 | @cpython_api([PyObject, CONST_STRING], rffi.INT_real, error=CANNOT_FAIL) |
| 72 | def PyMapping_HasKeyString(space, w_obj, key): |
| 73 | """Return 1 if the mapping object has the key key and 0 otherwise. |
| 74 | This is equivalent to o[key], returning True on success and False |
| 75 | on an exception. This function always succeeds.""" |
| 76 | try: |
| 77 | w_key = space.newtext(rffi.charp2str(key)) |
| 78 | space.getitem(w_obj, w_key) |
| 79 | return 1 |
| 80 | except: |
| 81 | return 0 |