|
| 1 | +#ifndef HPy_CPYTHON_H |
| 2 | +#define HPy_CPYTHON_H |
| 3 | + |
| 4 | +/* XXX: it would be nice if we could include hpy.h WITHOUT bringing in all the |
| 5 | + stuff from Python.h, to make sure that people don't use the CPython API by |
| 6 | + mistake. How to achieve it, though? Is defining Py_LIMITED_API enough? */ |
| 7 | + |
| 8 | +/* XXX: should we: |
| 9 | + * - enforce PY_SSIZE_T_CLEAN in hpy |
| 10 | + * - make it optional |
| 11 | + * - make it the default but give a way to disable it?
F42D
|
| 12 | + */ |
| 13 | +#define PY_SSIZE_T_CLEAN |
| 14 | +#include <Python.h> |
| 15 | + |
| 16 | +#ifdef __GNUC__ |
| 17 | +#define HPyAPI_STORAGE __attribute__((unused)) static inline |
| 18 | +#else |
| 19 | +#define HPyAPI_STORAGE static inline |
| 20 | +#endif /* __GNUC__ */ |
| 21 | + |
| 22 | +#define HPyAPI_FUNC(restype) HPyAPI_STORAGE restype |
| 23 | + |
| 24 | +#ifdef __GNUC__ |
| 25 | +#define _HPy_HIDDEN __attribute__((visibility("hidden"))) |
| 26 | +#else |
| 27 | +#define _HPy_HIDDEN |
| 28 | +#endif /* __GNUC__ */ |
| 29 | + |
| 30 | +#define HPyAPI_RUNTIME_FUNC(restype) _HPy_HIDDEN restype |
| 31 | + |
| 32 | +typedef struct { PyObject *_o; } HPy; |
| 33 | +typedef Py_ssize_t HPy_ssize_t; |
| 34 | + |
| 35 | +/* For internal usage only. These should be #undef at the end of this header. |
| 36 | + If you need to convert HPy to PyObject* and vice-versa, you should use the |
| 37 | + official way to do it (not implemented yet :) |
| 38 | +*/ |
| 39 | +#define _h2py(x) (x._o) |
| 40 | +#define _py2h(o) ((HPy){o}) |
| 41 | + |
| 42 | +#include "meth.h" |
| 43 | + |
| 44 | +typedef struct _HPyContext_s { |
| 45 | + HPy h_None; |
| 46 | + HPy h_True; |
| 47 | + HPy h_False; |
| 48 | + HPy h_ValueError; |
| 49 | + HPy h_TypeError; |
| 50 | +} *HPyContext; |
| 51 | + |
| 52 | +/* XXX! should be defined only once, not once for every .c! */ |
| 53 | +static struct _HPyContext_s _global_ctx; |
| 54 | + <
179B
/td> |
| 55 | +#define HPy_NULL ((HPy){NULL}) |
| 56 | +#define HPy_IsNull(x) ((x)._o == NULL) |
| 57 | + |
| 58 | +// XXX: we need to decide whether these are part of the official API or not, |
| 59 | +// and maybe introduce a better naming convetion. For now, they are needed for |
| 60 | +// ujson |
| 61 | +static inline HPy HPy_FromVoidP(void *p) { return (HPy){(PyObject*)p}; } |
| 62 | +static inline void* HPy_AsVoidP(HPy h) { return (void*)h._o; } |
| 63 | + |
| 64 | + |
| 65 | +HPyAPI_FUNC(HPyContext) |
| 66 | +_HPyGetContext(void) { |
| 67 | + HPyContext ctx = &_global_ctx; |
| 68 | + if (HPy_IsNull(ctx->h_None)) { |
| 69 | + // XXX: we need to find a better way to check whether the ctx is |
| 70 | + // initialized or not |
| 71 | + ctx->h_None = _py2h(Py_None); |
| 72 | + ctx->h_True = _py2h(Py_True); |
| 73 | + ctx->h_False = _py2h(Py_False); |
| 74 | + ctx->h_ValueError = _py2h(PyExc_ValueError); |
| 75 | + ctx->h_TypeError = _py2h(PyExc_TypeError); |
| 76 | + } |
| 77 | + return ctx; |
| 78 | +} |
| 79 | + |
| 80 | + |
| 81 | +HPyAPI_FUNC(HPy) |
| 82 | +HPy_Dup(HPyContext ctx, HPy handle) |
| 83 | +{ |
| 84 | + Py_XINCREF(_h2py(handle)); |
| 85 | + return handle; |
| 86 | +} |
| 87 | + |
| 88 | +HPyAPI_FUNC(void) |
| 89 | +HPy_Close(HPyContext ctx, HPy handle) |
| 90 | +{ |
| 91 | + Py_XDECREF(_h2py(handle)); |
| 92 | +} |
| 93 | + |
| 94 | +/* object.h */ |
| 95 | + |
| 96 | +HPyAPI_FUNC(HPy) |
| 97 | +HPy_GetAttr(HPyContext ctx, HPy obj, HPy name) { |
| 98 | + return _py2h(PyObject_GetAttr(_h2py(obj), _h2py(name))); |
| 99 | +} |
| 100 | + |
| 101 | +HPyAPI_FUNC(HPy) |
| 102 | +HPy_GetAttr_s(HPyContext ctx, HPy obj, const char *name) { |
| 103 | + return _py2h(PyObject_GetAttrString(_h2py(obj), name)); |
| 104 | +} |
| 105 | + |
| 106 | +HPyAPI_FUNC(int) |
| 107 | +HPy_HasAttr(HPyContext ctx, HPy obj, HPy name) { |
| 108 | + return PyObject_HasAttr(_h2py(obj), _h2py(name)); |
| 109 | +} |
| 110 | + |
| 111 | +HPyAPI_FUNC(int) |
| 112 | +HPy_HasAttr_s(HPyContext ctx, HPy obj, const char *name) { |
| 113 | + return PyObject_HasAttrString(_h2py(obj), name); |
| 114 | +} |
| 115 | + |
| 116 | +HPyAPI_FUNC(int) |
| 117 | +HPy_SetAttr(HPyContext ctx, HPy obj, HPy name, HPy value) { |
| 118 | + return PyObject_SetAttr(_h2py(obj), _h2py(name), _h2py(value)); |
| 119 | +} |
| 120 | + |
| 121 | +HPyAPI_FUNC(int) |
| 122 | +HPy_SetAttr_s(HPyContext ctx, HPy obj, const char *name, HPy value) { |
| 123 | + return PyObject_SetAttrString(_h2py(obj), name, _h2py(value)); |
| 124 | +} |
| 125 | + |
| 126 | +HPyAPI_FUNC(HPy) |
| 127 | +HPy_GetItem(HPyContext ctx, HPy obj, HPy key) { |
| 128 | + return _py2h(PyObject_GetItem(_h2py(obj), _h2py(key))); |
| 129 | +} |
| 130 | + |
| 131 | +HPyAPI_FUNC(HPy) |
| 132 | +HPy_GetItem_i(HPyContext ctx, HPy obj, HPy_ssize_t idx) { |
| 133 | + PyObject* key = PyLong_FromSsize_t(idx); |
| 134 | + if (key == NULL) |
| 135 | + return HPy_NULL; |
| 136 | + HPy result = _py2h(PyObject_GetItem(_h2py(obj), key)); |
| 137 | + Py_DECREF(key); |
| 138 | + return result; |
| 139 | +} |
| 140 | + |
| 141 | +HPyAPI_FUNC(HPy) |
| 142 | +HPy_GetItem_s(HPyContext ctx, HPy obj, const char *key) { |
| 143 | + PyObject* key_o = PyUnicode_FromString(key); |
| 144 | + if (key_o == NULL) |
| 145 | + return HPy_NULL; |
| 146 | + HPy result = _py2h(PyObject_GetItem(_h2py(obj), key_o)); |
| 147 | + Py_DECREF(key_o); |
| 148 | + return result; |
| 149 | +} |
| 150 | + |
| 151 | +HPyAPI_FUNC(int) |
| 152 | +HPy_SetItem(HPyContext ctx, HPy obj, HPy key, HPy value) { |
| 153 | + return PyObject_SetItem(_h2py(obj), _h2py(key), _h2py(value)); |
| 154 | +} |
| 155 | + |
| 156 | +HPyAPI_FUNC(int) |
| 157 | +HPy_SetItem_i(HPyContext ctx, HPy obj, HPy_ssize_t idx, HPy value) { |
| 158 | + PyObject* key = PyLong_FromSsize_t(idx); |
| 159 | + if (key == NULL) |
| 160 | + return -1; |
| 161 | + int result = PyObject_SetItem(_h2py(obj), key, _h2py(value)); |
| 162 | + Py_DECREF(key); |
| 163 | + return result; |
| 164 | +} |
| 165 | + |
| 166 | +HPyAPI_FUNC(int) |
| 167 | +HPy_SetItem_s(HPyContext ctx, HPy obj, const char *key, HPy value) { |
| 168 | + PyObject* key_o = PyUnicode_FromString(key); |
| 169 | + if (key_o == NULL) |
| 170 | + return -1; |
| 171 | + int result = PyObject_SetItem(_h2py(obj), key_o, _h2py(value)); |
| 172 | + Py_DECREF(key_o); |
| 173 | + return result; |
| 174 | +} |
| 175 | + |
| 176 | +HPyAPI_FUNC(int) |
| 177 | +HPyErr_Occurred(HPyContext ctx) { |
| 178 | + return PyErr_Occurred() ? 1 : 0; |
| 179 | +} |
| 180 | + |
| 181 | +/* moduleobject.h */ |
| 182 | +typedef PyModuleDef HPyModuleDef; |
| 183 | + |
| 184 | +#define HPyModuleDef_HEAD_INIT PyModuleDef_HEAD_INIT |
| 185 | + |
| 186 | +HPyAPI_FUNC(HPy) |
| 187 | +HPyModule_Create(HPyContext ctx, HPyModuleDef *mdef) { |
| 188 | + return _py2h(PyModule_Create(mdef)); |
| 189 | +} |
| 190 | + |
| 191 | +#define HPy_MODINIT(modname) \ |
| 192 | + static HPy init_##modname##_impl(HPyContext ctx); \ |
| 193 | + PyMODINIT_FUNC \ |
| 194 | + PyInit_##modname(void) \ |
| 195 | + { \ |
| 196 | + return _h2py(init_##modname##_impl(_HPyGetContext())); \ |
| 197 | + } |
| 198 | + |
| 199 | +HPyAPI_FUNC(HPy) |
| 200 | +HPy_FromPyObject(HPyContext ctx, PyObject *obj) |
| 201 | +{ |
| 202 | + Py_XINCREF(obj); |
| 203 | + return _py2h(obj); |
| 204 | +} |
| 205 | + |
| 206 | +HPyAPI_FUNC(PyObject *) |
| 207 | +HPy_AsPyObject(HPyContext ctx, HPy h) |
| 208 | +{ |
| 209 | + PyObject *result = _h2py(h); |
| 210 | + Py_XINCREF(result); |
| 211 | + return result; |
| 212 | +} |
| 213 | + |
| 214 | +/* expand impl functions as: |
| 215 | + * static inline HPyLong_FromLong(...); |
| 216 | + * |
| 217 | + */ |
| 218 | +#define _HPy_IMPL_NAME(name) HPy##name |
| 219 | +#include "../common/autogen_impl.h" |
| 220 | +#undef _HPy_IMPL_NAME |
| 221 | + |
| 222 | +// include runtime functions |
| 223 | +#include "../common/runtime.h" |
| 224 | + |
| 225 | +#endif /* !HPy_CPYTHON_H */ |
0 commit comments