Debian Codegraph @

lib_pypy/hpy/devel/src/runtime/ctx_object.c

raw symbols SCIP
1 #include <Python.h>
2 #include "hpy.h"
3
4 #ifndef HPY_ABI_CPYTHON
5 // for _h2py and _py2h
6 # include "handles.h"
7 #endif
8
9
10 _HPy_HIDDEN void
11 ctx_Dump(HPyContext *ctx, HPy h)
12 {
13 // just use _PyObject_Dump for now, but we might want to add more info
14 // about the handle itself in the future.
15 _PyObject_Dump(_h2py(h));
16 }
17
18 _HPy_HIDDEN HPy
19 ctx_Type(HPyContext *ctx, HPy obj)
20 {
21 PyTypeObject *tp = Py_TYPE(_h2py(obj));
22 Py_INCREF(tp);
23 return _py2h((PyObject *)tp);
24 }
25
26 /* NOTE: In contrast to CPython, HPy has to check that 'h_type' is a type. This
27 is not necessary on CPython because it requires C type 'PyTypeObject *' but
28 here we can only receive an HPy handle. Appropriate checking of the argument
29 will be done in the debug mode.
30 */
31 _HPy_HIDDEN int
32 ctx_TypeCheck(HPyContext *ctx, HPy h_obj, HPy h_type)
33 {
34 return PyObject_TypeCheck(_h2py(h_obj), (PyTypeObject*)_h2py(h_type));
35 }
36
37 _HPy_HIDDEN int
38 ctx_Is(HPyContext *ctx, HPy h_obj, HPy h_other)
39 {
40 return _h2py(h_obj) == _h2py(h_other);
41 }
42
43 _HPy_HIDDEN HPy
44 ctx_GetItem_i(HPyContext *ctx, HPy obj, HPy_ssize_t idx) {
45 PyObject *py_obj = _h2py(obj);
46 if (PySequence_Check(py_obj)) {
47 return _py2h(PySequence_GetItem(py_obj, idx));
48 }
49 PyObject* key = PyLong_FromSsize_t(idx);
50 if (key == NULL)
51 return HPy_NULL;
52 HPy result = _py2h(PyObject_GetItem(py_obj, key));
53 Py_DECREF(key);
54 return result;
55 }
56
57 _HPy_HIDDEN HPy
58 ctx_GetItem_s(HPyContext *ctx, HPy obj, const char *key) {
59 PyObject* key_o = PyUnicode_FromString(key);
60 if (key_o == NULL)
61 return HPy_NULL;
62 HPy result = _py2h(PyObject_GetItem(_h2py(obj), key_o));
63 Py_DECREF(key_o);
64 return result;
65 }
66
67 _HPy_HIDDEN int
68 ctx_SetItem_i(HPyContext *ctx, HPy obj, HPy_ssize_t idx, HPy value) {
69 PyObject* key = PyLong_FromSsize_t(idx);
70 if (key == NULL)
71 return -1;
72 int result = PyObject_SetItem(_h2py(obj), key, _h2py(value));
73 Py_DECREF(key);
74 return result;
75 }
76
77 _HPy_HIDDEN int
78 ctx_SetItem_s(HPyContext *ctx, HPy obj, const char *key, HPy value) {
79 PyObject* key_o = PyUnicode_FromString(key);
80 if (key_o == NULL)
81 return -1;
82 int result = PyObject_SetItem(_h2py(obj), key_o, _h2py(value));
83 Py_DECREF(key_o);
84 return result;
85 }
86
87 _HPy_HIDDEN int
88 ctx_DelItem_i(HPyContext *ctx, HPy obj, HPy_ssize_t idx) {
89 PyObject* key = PyLong_FromSsize_t(idx);
90 if (key == NULL)
91 return -1;
92 int result = PyObject_DelItem(_h2py(obj), key);
93 Py_DECREF(key);
94 return result;
95 }
96
97 _HPy_HIDDEN int
98 ctx_DelItem_s(HPyContext *ctx, HPy obj, const char *key) {
99 PyObject* key_o = PyUnicode_FromString(key);
100 if (key_o == NULL)
101 return -1;
102 int result = PyObject_DelItem(_h2py(obj), key_o);
103 Py_DECREF(key_o);
104 return result;
105 }