Skip to content

Commit 0d5fd5d

Browse files
fangerertimfel
authored andcommitted
Correctly create HPyStructSequenceBuilder
1 parent cfa56dc commit 0d5fd5d

File tree

2 files changed

+96
-0
lines changed
  • graalpython
    • com.oracle.graal.python.cext/include
    • lib-graalpython/modules/hpy/devel/src/runtime

2 files changed

+96
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#ifndef Py_CONTEXT_H
2+
#define Py_CONTEXT_H
3+
#ifdef __cplusplus
4+
extern "C" {
5+
#endif
6+
7+
#ifndef Py_LIMITED_API
8+
9+
10+
PyAPI_DATA(PyTypeObject) PyContext_Type;
11+
typedef struct _pycontextobject PyContext;
12+
13+
PyAPI_DATA(PyTypeObject) PyContextVar_Type;
14+
typedef struct _pycontextvarobject PyContextVar;
15+
16+
PyAPI_DATA(PyTypeObject) PyContextToken_Type;
17+
typedef struct _pycontexttokenobject PyContextToken;
18+
19+
20+
#define PyContext_CheckExact(o) (Py_TYPE(o) == &PyContext_Type)
21+
#define PyContextVar_CheckExact(o) (Py_TYPE(o) == &PyContextVar_Type)
22+
#define PyContextToken_CheckExact(o) (Py_TYPE(o) == &PyContextToken_Type)
23+
24+
25+
PyAPI_FUNC(PyObject *) PyContext_New(void);
26+
PyAPI_FUNC(PyObject *) PyContext_Copy(PyObject *);
27+
PyAPI_FUNC(PyObject *) PyContext_CopyCurrent(void);
28+
29+
PyAPI_FUNC(int) PyContext_Enter(PyObject *);
30+
PyAPI_FUNC(int) PyContext_Exit(PyObject *);
31+
32+
33+
/* Create a new context variable.
34+
35+
default_value can be NULL.
36+
*/
37+
PyAPI_FUNC(PyObject *) PyContextVar_New(
38+
const char *name, PyObject *default_value);
39+
40+
41+
/* Get a value for the variable.
42+
43+
Returns -1 if an error occurred during lookup.
44+
45+
Returns 0 if value either was or was not found.
46+
47+
If value was found, *value will point to it.
48+
If not, it will point to:
49+
50+
- default_value, if not NULL;
51+
- the default value of "var", if not NULL;
52+
- NULL.
53+
54+
'*value' will be a new ref, if not NULL.
55+
*/
56+
PyAPI_FUNC(int) PyContextVar_Get(
57+
PyObject *var, PyObject *default_value, PyObject **value);
58+
59+
60+
/* Set a new value for the variable.
61+
Returns NULL if an error occurs.
62+
*/
63+
PyAPI_FUNC(PyObject *) PyContextVar_Set(PyObject *var, PyObject *value);
64+
65+
66+
/* Reset a variable to its previous value.
67+
Returns 0 on success, -1 on error.
68+
*/
69+
PyAPI_FUNC(int) PyContextVar_Reset(PyObject *var, PyObject *token);
70+
71+
72+
/* This method is exposed only for CPython tests. Don not use it. */
73+
PyAPI_FUNC(PyObject *) _PyContext_NewHamtForTests(void);
74+
75+
76+
PyAPI_FUNC(int) PyContext_ClearFreeList(void);
77+
78+
79+
#endif /* !Py_LIMITED_API */
80+
81+
#ifdef __cplusplus
82+
}
83+
#endif
84+
#endif /* !Py_CONTEXT_H */

graalpython/lib-graalpython/modules/hpy/devel/src/runtime/structseq.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,18 +138,30 @@ HPyStructSequenceBuilder_New(HPyContext *ctx, HPy type)
138138
#ifdef HPY_UNIVERSAL_ABI
139139
HPy fields = HPy_GetAttr_s(ctx, type, "_fields");
140140
if (HPy_IsNull(fields)) {
141+
#ifdef GRAALVM_PYTHON_LLVM
142+
return (HPyTupleBuilder){NULL};
143+
#else
141144
return (HPyTupleBuilder){(HPy_ssize_t)0};
145+
#endif
142146
}
143147
HPy_ssize_t n = HPy_Length(ctx, fields);
144148
if (n < 0) {
145149
HPy_Close(ctx, fields);
150+
#ifdef GRAALVM_PYTHON_LLVM
151+
return (HPyTupleBuilder){NULL};
152+
#else
146153
return (HPyTupleBuilder){(HPy_ssize_t)0};
154+
#endif
147155
}
148156
return HPyTupleBuilder_New(ctx, n);
149157
#else
150158
PyObject *seq = PyStructSequence_New((PyTypeObject *)_h2py(type));
159+
#ifdef GRAALVM_PYTHON_LLVM
160+
return (HPyStructSequenceBuilder){(void *)seq};
161+
#else
151162
return (HPyStructSequenceBuilder){(HPy_ssize_t)seq};
152163
#endif
164+
#endif
153165
}
154166

155167
HPyAPI_HELPER void

0 commit comments

Comments
 (0)