Skip to content

Commit 20a7d5a

Browse files
committed
Update HPy inlined files: 5ebc33c
1 parent 7e841e2 commit 20a7d5a

File tree

26 files changed

+1815
-20
lines changed

26 files changed

+1815
-20
lines changed

graalpython/com.oracle.graal.python.cext/include/hpy.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ typedef struct { intptr_t _i; } HPyThreadState;
123123
#define HPy_NULL _hconv(0)
124124
#define HPy_IsNull(h) ((h)._i == 0)
125125

126+
#define HPyListBuilder_IsNull(h) ((h)._lst == 0)
127+
#define HPyTupleBuilder_IsNull(h) ((h)._tup == 0)
128+
126129
#define HPyField_NULL _hfconv(0)
127130
#define HPyField_IsNull(f) ((f)._i == 0)
128131

@@ -141,12 +144,23 @@ typedef struct _HPyContext_s HPyContext;
141144
typedef intptr_t HPy_ssize_t;
142145
typedef intptr_t HPy_hash_t;
143146
typedef uint32_t HPy_UCS4;
147+
148+
/* HPyCapsule field keys */
149+
typedef enum {
150+
HPyCapsule_key_Pointer = 0,
151+
HPyCapsule_key_Name = 1,
152+
HPyCapsule_key_Context = 2,
153+
HPyCapsule_key_Destructor = 3,
154+
} _HPyCapsule_key;
155+
144156
#else
145157
typedef Py_ssize_t HPy_ssize_t;
146158
typedef Py_hash_t HPy_hash_t;
147159
typedef Py_UCS4 HPy_UCS4;
148160
#endif
149161

162+
typedef void (*HPyCapsule_Destructor)(const char *name, void *pointer, void *context);
163+
150164

151165
/* ~~~~~~~~~~~~~~~~ Additional #includes ~~~~~~~~~~~~~~~~ */
152166

@@ -159,6 +173,7 @@ typedef struct _HPyContext_s HPyContext;
159173
#include "hpy/runtime/argparse.h"
160174
#include "hpy/runtime/buildvalue.h"
161175
#include "hpy/runtime/helpers.h"
176+
#include "hpy/runtime/structseq.h"
162177

163178
#ifdef HPY_UNIVERSAL_ABI
164179
# include "hpy/universal/autogen_ctx.h"

graalpython/com.oracle.graal.python.cext/include/hpy/autogen_hpyslot.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ typedef enum {
5757
HPy_sq_item = 44,
5858
HPy_sq_length = 45,
5959
HPy_sq_repeat = 46,
60+
HPy_tp_call = 50,
6061
HPy_tp_init = 60,
62+
HPy_tp_iter = 62,
6163
HPy_tp_new = 65,
6264
HPy_tp_repr = 66,
6365
HPy_tp_richcompare = 67,
@@ -114,7 +116,9 @@ typedef enum {
114116
#define _HPySlot_SIG__HPy_sq_item HPyFunc_SSIZEARGFUNC
115117
#define _HPySlot_SIG__HPy_sq_length HPyFunc_LENFUNC
116118
#define _HPySlot_SIG__HPy_sq_repeat HPyFunc_SSIZEARGFUNC
119+
#define _HPySlot_SIG__HPy_tp_call HPyFunc_KEYWORDS
117120
#define _HPySlot_SIG__HPy_tp_init HPyFunc_INITPROC
121+
#define _HPySlot_SIG__HPy_tp_iter HPyFunc_GETITERFUNC
118122
#define _HPySlot_SIG__HPy_tp_new HPyFunc_KEYWORDS
119123
#define _HPySlot_SIG__HPy_tp_repr HPyFunc_REPRFUNC
120124
#define _HPySlot_SIG__HPy_tp_richcompare HPyFunc_RICHCMPFUNC

graalpython/com.oracle.graal.python.cext/include/hpy/cpython/autogen_api_impl.h

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ HPyAPI_FUNC HPy_ssize_t HPy_Length(HPyContext *ctx, HPy h)
110110
return PyObject_Length(_h2py(h));
111111
}
112112

113+
HPyAPI_FUNC int HPySequence_Check(HPyContext *ctx, HPy h)
114+
{
115+
return PySequence_Check(_h2py(h));
116+
}
117+
113118
HPyAPI_FUNC int HPyNumber_Check(HPyContext *ctx, HPy h)
114119
{
115120
return PyNumber_Check(_h2py(h));
@@ -404,6 +409,19 @@ HPyAPI_FUNC HPy HPy_Type(HPyContext *ctx, HPy obj)
404409
return _py2h(PyObject_Type(_h2py(obj)));
405410
}
406411

412+
HPyAPI_FUNC int HPy_SetType(HPyContext *ctx, HPy obj, HPy type)
413+
{
414+
assert(PyType_Check(_h2py(type)));
415+
_h2py(obj)->ob_type = (PyTypeObject*) _h2py(type);
416+
return 0;
417+
}
418+
419+
HPyAPI_FUNC const char *HPyType_GetName(HPyContext *ctx, HPy type)
420+
{
421+
assert(PyType_Check(_h2py(type)));
422+
return ((PyTypeObject*) _h2py(type))->tp_name;
423+
}
424+
407425
HPyAPI_FUNC HPy HPy_Repr(HPyContext *ctx, HPy obj)
408426
{
409427
return _py2h(PyObject_Repr(_h2py(obj)));
@@ -439,6 +457,11 @@ HPyAPI_FUNC HPy_hash_t HPy_Hash(HPyContext *ctx, HPy obj)
439457
return PyObject_Hash(_h2py(obj));
440458
}
441459

460+
HPyAPI_FUNC HPy HPySeqIter_New(HPyContext *ctx, HPy seq)
461+
{
462+
return _py2h(PySeqIter_New(_h2py(seq)));
463+
}
464+
442465
HPyAPI_FUNC int HPyBytes_Check(HPyContext *ctx, HPy h)
443466
{
444467
return PyBytes_Check(_h2py(h));
@@ -534,6 +557,21 @@ HPyAPI_FUNC HPy HPyUnicode_DecodeLatin1(HPyContext *ctx, const char *s, HPy_ssiz
534557
return _py2h(PyUnicode_DecodeLatin1(s, size, errors));
535558
}
536559

560+
HPyAPI_FUNC HPy HPyUnicode_FromEncodedObject(HPyContext *ctx, HPy obj, const char *encoding, const char *errors)
561+
{
562+
return _py2h(PyUnicode_FromEncodedObject(_h2py(obj), encoding, errors));
563+
}
564+
565+
HPyAPI_FUNC HPy HPyUnicode_InternFromString(HPyContext *ctx, const char *str)
566+
{
567+
return _py2h(PyUnicode_InternFromString(str));
568+
}
569+
570+
HPyAPI_FUNC HPy HPyUnicode_Substring(HPyContext *ctx, HPy obj, HPy_ssize_t start, HPy_ssize_t end)
571+
{
572+
return _py2h(PyUnicode_Substring(_h2py(obj), start, end));
573+
}
574+
537575
HPyAPI_FUNC int HPyList_Check(HPyContext *ctx, HPy h)
538576
{
539577
return PyList_Check(_h2py(h));
@@ -559,16 +597,41 @@ HPyAPI_FUNC HPy HPyDict_New(HPyContext *ctx)
559597
return _py2h(PyDict_New());
560598
}
561599

600+
HPyAPI_FUNC HPy HPyDict_Keys(HPyContext *ctx, HPy h)
601+
{
602+
return _py2h(PyDict_Keys(_h2py(h)));
603+
}
604+
562605
HPyAPI_FUNC int HPyTuple_Check(HPyContext *ctx, HPy h)
563606
{
564607
return PyTuple_Check(_h2py(h));
565608
}
566609

610+
HPyAPI_FUNC int HPySlice_Unpack(HPyContext *ctx, HPy slice, HPy_ssize_t *start, HPy_ssize_t *stop, HPy_ssize_t *step)
611+
{
612+
return PySlice_Unpack(_h2py(slice), start, stop, step);
613+
}
614+
615+
HPyAPI_FUNC HPy HPyContextVar_New(HPyContext *ctx, const char *name, HPy default_value)
616+
{
617+
return _py2h(PyContextVar_New(name, _h2py(default_value)));
618+
}
619+
620+
HPyAPI_FUNC HPy HPyContextVar_Set(HPyContext *ctx, HPy context_var, HPy value)
621+
{
622+
return _py2h(PyContextVar_Set(_h2py(context_var), _h2py(value)));
623+
}
624+
567625
HPyAPI_FUNC HPy HPyImport_ImportModule(HPyContext *ctx, const char *name)
568626
{
569627
return _py2h(PyImport_ImportModule(name));
570628
}
571629

630+
HPyAPI_FUNC int HPyCapsule_IsValid(HPyContext *ctx, HPy capsule, const char *name)
631+
{
632+
return PyCapsule_IsValid(_h2py(capsule), name);
633+
}
634+
572635
HPyAPI_FUNC void HPy_ReenterPythonExecution(HPyContext *ctx, HPyThreadState state)
573636
{
574637
PyEval_RestoreThread(_h2threads(state));
@@ -579,3 +642,32 @@ HPyAPI_FUNC HPyThreadState HPy_LeavePythonExecution(HPyContext *ctx)
579642
return _threads2h(PyEval_SaveThread());
580643
}
581644

645+
HPyAPI_FUNC int HPyType_CheckSlot(HPyContext *ctx, HPy type, HPyDef *value)
646+
{
647+
PyObject *result = _h2py(type);
648+
struct _typeobject* t = ((struct _typeobject*) result);
649+
char msg[256];
650+
switch (value->slot.slot) {
651+
case HPy_nb_inplace_add:
652+
return t->tp_as_number != NULL && t->tp_as_number->nb_inplace_add == value->slot.cpy_trampoline;
653+
case HPy_nb_power:
654+
// TODO: this needs proper cast for C++
655+
return t->tp_as_number != NULL && (void*) t->tp_as_number->nb_power == (void*) value->slot.cpy_trampoline;
656+
case HPy_nb_subtract:
657+
return t->tp_as_number != NULL && (void*) t->tp_as_number->nb_subtract == (void*) value->slot.cpy_trampoline;
658+
case HPy_nb_true_divide:
659+
return t->tp_as_number != NULL && (void*) t->tp_as_number->nb_true_divide == (void*) value->slot.cpy_trampoline;
660+
case HPy_nb_add:
661+
return t->tp_as_number != NULL && (void*) t->tp_as_number->nb_add == (void*) value->slot.cpy_trampoline;
662+
case HPy_nb_and:
663+
return t->tp_as_number != NULL && (void*) t->tp_as_number->nb_and == (void*) value->slot.cpy_trampoline;
664+
case HPy_nb_or:
665+
return t->tp_as_number != NULL && (void*) t->tp_as_number->nb_or == (void*) value->slot.cpy_trampoline;
666+
case HPy_nb_xor:
667+
return t->tp_as_number != NULL && (void*) t->tp_as_number->nb_xor == (void*) value->slot.cpy_trampoline;
668+
default:
669+
snprintf(msg, 256, "Unsupported slot in HPyTypeSlot_Is: %d", value->slot.slot);
670+
Py_FatalError(msg);
671+
}
672+
}
673+

graalpython/com.oracle.graal.python.cext/include/hpy/cpython/misc.h

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,14 @@ struct _HPyContext_s {
126126
HPy h_BoolType;
127127
HPy h_LongType;
128128
HPy h_FloatType;
129+
HPy h_ComplexType;
129130
HPy h_UnicodeType;
131+
HPy h_BytesType;
130132
HPy h_TupleType;
131133
HPy h_ListType;
134+
HPy h_MemoryViewType;
135+
HPy h_CapsuleType;
136+
HPy h_SliceType;
132137
};
133138

134139
/* XXX! should be defined only once, not once for every .c! */
@@ -216,9 +221,14 @@ HPyAPI_FUNC HPyContext * _HPyGetContext(void) {
216221
ctx->h_BoolType = _py2h((PyObject *)&PyBool_Type);
217222
ctx->h_LongType = _py2h((PyObject *)&PyLong_Type);
218223
ctx->h_FloatType = _py2h((PyObject *)&PyFloat_Type);
224+
ctx->h_ComplexType = _py2h((PyObject *)&PyComplex_Type);
219225
ctx->h_UnicodeType = _py2h((PyObject *)&PyUnicode_Type);
226+
ctx->h_BytesType = _py2h((PyObject *)&PyBytes_Type);
220227
ctx->h_TupleType = _py2h((PyObject *)&PyTuple_Type);
221228
ctx->h_ListType = _py2h((PyObject *)&PyList_Type);
229+
ctx->h_MemoryViewType = _py2h((PyObject *)&PyMemoryView_Type);
230+
ctx->h_CapsuleType = _py2h((PyObject *)&PyCapsule_Type);
231+
ctx->h_SliceType = _py2h((PyObject *)&PySlice_Type);
222232
}
223233
return ctx;
224234
}
@@ -329,6 +339,11 @@ HPyAPI_FUNC int HPy_TypeCheck(HPyContext *ctx, HPy h_obj, HPy h_type)
329339
return ctx_TypeCheck(ctx, h_obj, h_type);
330340
}
331341

342+
HPyAPI_FUNC int HPyType_IsSubtype(HPyContext *ctx, HPy h_sub, HPy h_type)
343+
{
344+
return ctx_Type_IsSubtype(ctx, h_sub, h_type);
345+
}
346+
332347
HPyAPI_FUNC int HPy_Is(HPyContext *ctx, HPy h_obj, HPy h_other)
333348
{
334349
return ctx_Is(ctx, h_obj, h_other);
@@ -425,4 +440,73 @@ HPyAPI_FUNC int HPyErr_Occurred(HPyContext *ctx) {
425440
return ctx_Err_Occurred(ctx);
426441
}
427442

443+
HPyAPI_FUNC int HPyContextVar_Get(HPyContext *ctx, HPy context_var, HPy default_value, HPy *result) {
444+
PyObject *py_result;
445+
int ret = PyContextVar_Get(_h2py(context_var), _h2py(default_value), &py_result);
446+
*result = _py2h(py_result);
447+
return ret;
448+
}
449+
450+
HPyAPI_FUNC HPy HPyCapsule_New(HPyContext *ctx, void *pointer, const char *name, HPyCapsule_Destructor destructor)
451+
{
452+
return ctx_Capsule_New(ctx, pointer, name, destructor);
453+
}
454+
455+
HPyAPI_FUNC void * HPyCapsule_GetPointer(HPyContext *ctx, HPy capsule, const char *name)
456+
{
457+
return PyCapsule_GetPointer(_h2py(capsule), name);
458+
}
459+
460+
HPyAPI_FUNC const char * HPyCapsule_GetName(HPyContext *ctx, HPy capsule)
461+
{
462+
return PyCapsule_GetName(_h2py(capsule));
463+
}
464+
465+
HPyAPI_FUNC void * HPyCapsule_GetContext(HPyContext *ctx, HPy capsule)
466+
{
467+
return PyCapsule_GetContext(_h2py(capsule));
468+
}
469+
470+
HPyAPI_FUNC HPyCapsule_Destructor HPyCapsule_GetDestructor(HPyContext *ctx, HPy capsule)
471+
{
472+
return ctx_Capsule_GetDestructor(ctx, capsule);
473+
}
474+
475+
HPyAPI_FUNC int HPyCapsule_SetPointer(HPyContext *ctx, HPy capsule, void *pointer)
476+
{
477+
return PyCapsule_SetPointer(_h2py(capsule), pointer);
478+
}
479+
480+
HPyAPI_FUNC int HPyCapsule_SetName(HPyContext *ctx, HPy capsule, const char *name)
481+
{
482+
return PyCapsule_SetName(_h2py(capsule), name);
483+
}
484+
485+
HPyAPI_FUNC int HPyCapsule_SetContext(HPyContext *ctx, HPy capsule, void *context)
486+
{
487+
return PyCapsule_SetContext(_h2py(capsule), context);
488+
}
489+
490+
HPyAPI_FUNC int HPyCapsule_SetDestructor(HPyContext *ctx, HPy capsule, HPyCapsule_Destructor destructor)
491+
{
492+
return ctx_Capsule_SetDestructor(ctx, capsule, destructor);
493+
}
494+
495+
HPyAPI_FUNC HPy HPy_MaybeGetAttr_s(HPyContext *ctx, HPy obj, const char *name) {
496+
PyObject *pyobj = _h2py(obj);
497+
struct _typeobject* t = Py_TYPE(pyobj);
498+
if (t->tp_getattr == NULL && t->tp_getattro == NULL) {
499+
return HPy_NULL;
500+
}
501+
PyObject *res = PyObject_GetAttrString(pyobj, name);
502+
if (res == NULL && PyErr_Occurred()) {
503+
PyErr_Clear();
504+
}
505+
return _py2h(res);
506+
}
507+
508+
HPyAPI_FUNC HPy HPyDict_GetItem(HPyContext *ctx, HPy op, HPy key) {
509+
return ctx_Dict_GetItem(ctx, op, key);
510+
}
511+
428512
#endif /* !HPY_CPYTHON_MISC_H */

graalpython/com.oracle.graal.python.cext/include/hpy/hpytype.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ typedef struct {
4646
typedef enum {
4747
HPyType_SpecParam_Base = 1,
4848
HPyType_SpecParam_BasesTuple = 2,
49-
//HPyType_SpecParam_Metaclass = 3,
49+
HPyType_SpecParam_Metaclass = 3,
5050
//HPyType_SpecParam_Module = 4,
5151
} HPyType_SpecParam_Kind;
5252

graalpython/com.oracle.graal.python.cext/include/hpy/inline_helpers.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef HPY_INLINE_HELPERS_H
22
#define HPY_INLINE_HELPERS_H
33

4+
#include <assert.h>
45
#if defined(_MSC_VER)
56
# include <malloc.h> /* for alloca() */
67
#endif
@@ -15,6 +16,45 @@ HPyAPI_FUNC HPy HPyErr_SetFromErrnoWithFilenameObject(HPyContext *ctx, HPy h_typ
1516
return HPyErr_SetFromErrnoWithFilenameObjects(ctx, h_type, filename, HPy_NULL);
1617
}
1718

19+
HPyAPI_FUNC int HPySlice_AdjustIndices(HPy_ssize_t length, HPy_ssize_t *start, HPy_ssize_t *stop, HPy_ssize_t step) {
20+
/* Taken from CPython: Written by Jim Hugunin and Chris Chase. */
21+
/* this is harder to get right than you might think */
22+
assert(step != 0);
23+
// assert(step >= -PY_SSIZE_T_MAX); TODO: add the macro for HPy_ssize_t
24+
25+
if (*start < 0) {
26+
*start += length;
27+
if (*start < 0) {
28+
*start = (step < 0) ? -1 : 0;
29+
}
30+
}
31+
else if (*start >= length) {
32+
*start = (step < 0) ? length - 1 : length;
33+
}
34+
35+
if (*stop < 0) {
36+
*stop += length;
37+
if (*stop < 0) {
38+
*stop = (step < 0) ? -1 : 0;
39+
}
40+
}
41+
else if (*stop >= length) {
42+
*stop = (step < 0) ? length - 1 : length;
43+
}
44+
45+
if (step < 0) {
46+
if (*stop < *start) {
47+
return (*start - *stop - 1) / (-step) + 1;
48+
}
49+
}
50+
else {
51+
if (*start < *stop) {
52+
return (*stop - *start - 1) / step + 1;
53+
}
54+
}
55+
return 0;
56+
}
57+
1858
HPyAPI_FUNC HPy HPyTuple_Pack(HPyContext *ctx, HPy_ssize_t n, ...) {
1959
va_list vargs;
2060
HPy_ssize_t i;

0 commit comments

Comments
 (0)