8000 Initial import of inlined HPy files: HPy revision cd95d87 · oracle/graalpython@7803ae8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7803ae8

Browse files
committed
Initial import of inlined HPy files: HPy revision cd95d87
0 parents  commit 7803ae8

File tree

24 files changed

+2816
-0
lines changed

24 files changed

+2816
-0
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
2+
/*
3+
DO NOT EDIT THIS FILE!
4+
5+
This file is automatically generated by tools/autogen.py from tools/public_api.h.
6+
Run this to regenerate:
7+
make autogen
8+
9+
*/
10+
11+
HPyAPI_STORAGE HPy _HPy_IMPL_NAME(Long_FromLong)(HPyContext ctx, long value)
12+
{
13+
return _py2h(PyLong_FromLong(value));
14+
}
15+
16+
HPyAPI_STORAGE HPy _HPy_IMPL_NAME(Long_FromLongLong)(HPyContext ctx, long long v)
17+
{
18+
return _py2h(PyLong_FromLongLong(v));
19+
}
20+
21+
HPyAPI_STORAGE HPy _HPy_IMPL_NAME(Long_FromUnsignedLongLong)(HPyContext ctx, unsigned long long v)
22+
{
23+
return _py2h(PyLong_FromUnsignedLongLong(v));
24+
}
25+
26+
HPyAPI_STORAGE long _HPy_IMPL_NAME(Long_AsLong)(HPyContext ctx, HPy h)
27+
{
28+
return PyLong_AsLong(_h2py(h));
29+
}
30+
31+
HPyAPI_STORAGE HPy _HPy_IMPL_NAME(Float_FromDouble)(HPyContext ctx, double v)
32+
{
33+
return _py2h(PyFloat_FromDouble(v));
34+
}
35+
36+
HPyAPI_STORAGE HPy _HPy_IMPL_NAME(Number_Add)(HPyContext ctx, HPy h1, HPy h2)
37+
{
38+
return _py2h(PyNumber_Add(_h2py(h1), _h2py(h2)));
39+
}
40+
41+
HPyAPI_STORAGE void _HPy_IMPL_NAME(Err_SetString)(HPyContext ctx, HPy h_type, const char *message)
42+
{
43+
return PyErr_SetString(_h2py(h_type), message);
44+
}
45+
46+
HPyAPI_STORAGE int _HPy_IMPL_NAME(Object_IsTrue)(HPyContext ctx, HPy h)
47+
{
48+
return PyObject_IsTrue(_h2py(h));
49+
}
50+
51+
HPyAPI_STORAGE int _HPy_IMPL_NAME(Bytes_Check)(HPyContext ctx, HPy h)
52+
{
53+
return PyBytes_Check(_h2py(h));
54+
}
55+
56+
HPyAPI_STORAGE HPy_ssize_t _HPy_IMPL_NAME(Bytes_Size)(HPyContext ctx, HPy h)
57+
{
58+
return PyBytes_Size(_h2py(h));
59+
}
60+
61+
HPyAPI_STORAGE HPy_ssize_t _HPy_IMPL_NAME(Bytes_GET_SIZE)(HPyContext ctx, HPy h)
62+
{
63+
return PyBytes_GET_SIZE(_h2py(h));
64+
}
65+
66+
HPyAPI_STORAGE char *_HPy_IMPL_NAME(Bytes_AsString)(HPyContext ctx, HPy h)
67+
{
68+
return PyBytes_AsString(_h2py(h));
69+
}
70+
71+
HPyAPI_STORAGE char *_HPy_IMPL_NAME(Bytes_AS_STRING)(HPyContext ctx, HPy h)
72+
{
73+
return PyBytes_AS_STRING(_h2py(h));
74+
}
75+
76+
HPyAPI_STORAGE HPy _HPy_IMPL_NAME(Unicode_FromString)(HPyContext ctx, const char *utf8)
77+
{
78+
return _py2h(PyUnicode_FromString(utf8));
79+
}
80+
81+
HPyAPI_STORAGE int _HPy_IMPL_NAME(Unicode_Check)(HPyContext ctx, HPy h)
82+
{
83+
return PyUnicode_Check(_h2py(h));
84+
}
85+
86+
HPyAPI_STORAGE HPy _HPy_IMPL_NAME(Unicode_AsUTF8String)(HPyContext ctx, HPy h)
87+
{
88+
return _py2h(PyUnicode_AsUTF8String(_h2py(h)));
89+
}
90+
91+
HPyAPI_STORAGE HPy _HPy_IMPL_NAME(Unicode_FromWideChar)(HPyContext ctx, const wchar_t *w, HPy_ssize_t size)
92+
{
93+
return _py2h(PyUnicode_FromWideChar(w, size));
94+
}
95+
96+
HPyAPI_STORAGE HPy _HPy_IMPL_NAME(List_New)(HPyContext ctx, HPy_ssize_t len)
97+
{
98+
return _py2h(PyList_New(len));
99+
}
100+
101+
HPyAPI_STORAGE int _HPy_IMPL_NAME(List_Append)(HPyContext ctx, HPy h_list, HPy h_item)
102+
{
103+
return PyList_Append(_h2py(h_list), _h2py(h_item));
104+
}
105+
106+
HPyAPI_STORAGE HPy _HPy_IMPL_NAME(Dict_New)(HPyContext ctx)
107+
{
108+
return _py2h(PyDict_New());
109+
}
110+
111+
HPyAPI_STORAGE int _HPy_IMPL_NAME(Dict_SetItem)(HPyContext ctx, HPy h_dict, HPy h_key, HPy h_val)
112+
{
113+
return PyDict_SetItem(_h2py(h_dict), _h2py(h_key), _h2py(h_val));
114+
}
115+
116+
HPyAPI_STORAGE HPy _HPy_IMPL_NAME(Dict_GetItem)(HPyContext ctx, HPy h_dict, HPy h_key)
117+
{
118+
return _py2h(PyDict_GetItem(_h2py(h_dict), _h2py(h_key)));
119+
}
120+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*
2+
HPy helper functions provided by the HPy runtime library.
3+
*/
4+
5+
HPyAPI_RUNTIME_FUNC(int)
6+
HPyArg_Parse(HPyContext ctx, HPy *args, HPy_ssize_t nargs, const char *fmt, ...);
7+
8+
HPyAPI_RUNTIME_FUNC(int)
9+
HPyArg_ParseKeywords(HPyContext ctx, HPy *args, HPy_ssize_t nargs, HPy kw,
10+
const char *fmt, const char *keywords[], ...);
Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
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

Comments
 (0)
0