Skip to content

Commit b449c80

Browse files
committed
[GR-37836] Update HPy
PullRequest: graalpython/2191
2 parents 5d6ec27 + 82a1e78 commit b449c80

File tree

93 files changed

+6160
-952
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+6160
-952
lines changed

docs/contributor/DEV_TASKS.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,32 @@ our imported sources from CPython and PyPy:
2525
It prints a fairly long help. Note that you'll need to make sure you also pushed
2626
your `python-import` branch after doing the update, so that any conflicts you
2727
resolved don't have to be resolved again by the next person.
28+
29+
### Updating hpy
30+
31+
Follow these steps to update HPy.
32+
33+
34+
1. Merge updated hpy sources. To do so, clone hpy somewhere next to
35+
graalpython. Then run the following command on a new branch of graalpython:
36+
37+
mx python-update-hpy-import --pull /path/to/clone/of/hpy
38+
39+
Follow the instructions.
40+
2. We need to fix compilation. We patch the hpy sources, and the merge may
41+
have introduced new API or types, and for these we need to apply
42+
patches. At the time of this writing, we redefine hpy types conditionally
43+
on `#ifdef GRAALVM_PYTHON_LLVM` (grep for this to find some
44+
examples). Also, we use macros to convert between the structured hpy types
45+
and plain pointers for our native interface, see the uses of the `WRAP` and
46+
`UNWRAP` macros.
47+
3. Once compilation is working, we try to run the tests and go on fixing
48+
them. If new API was added, `GraalHPyContext` needs to be adapted with the
49+
new signatures and/or types. This may include:
50+
51+
- Updating the HPyContextMember enum
52+
- Updating the HPyContextSignatureType enum
53+
- Adding `GraalHPyContextFunction` implementations for the new APIs
54+
- Updating the `createMembers` method to assign the appropriate
55+
implementations to the context members
56+

graalpython/com.oracle.graal.python.cext/hpy/hpy.c

Lines changed: 163 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -56,7 +56,10 @@ static HPyContext *g_debug_ctx;
5656
typedef HPyDef* HPyDefPtr;
5757

5858
POLYGLOT_DECLARE_TYPE(HPy)
59+
POLYGLOT_DECLARE_TYPE(HPyField)
60+
POLYGLOT_DECLARE_TYPE(HPyGlobal)
5961
POLYGLOT_DECLARE_TYPE(HPyContext)
62+
POLYGLOT_DECLARE_TYPE(HPyThreadState)
6063
POLYGLOT_DECLARE_TYPE(HPyDef)
6164
POLYGLOT_DECLARE_TYPE(HPyDef_Kind)
6265
POLYGLOT_DECLARE_TYPE(HPyDefPtr)
@@ -82,6 +85,7 @@ int graal_hpy_init(HPyContext *context, void *initObject) {
8285
// register the native type of HPy
8386
polyglot_invoke(initObject, "setHPyContextNativeType", polyglot_HPyContext_typeid());
8487
polyglot_invoke(initObject, "setHPyNativeType", polyglot_HPy_typeid());
88+
polyglot_invoke(initObject, "setHPyFieldNativeType", polyglot_HPyField_typeid());
8589
polyglot_invoke(initObject, "setHPyArrayNativeType", polyglot_array_typeid(polyglot_HPy_typeid(), 0));
8690

8791
// register size of wchar_t
@@ -142,11 +146,11 @@ void* graal_hpy_from_HPyType_SpecParam_array(HPyType_SpecParam *ptr) {
142146
}
143147

144148
void* graal_hpy_get_m_name(HPyModuleDef *moduleDef) {
145-
return polyglot_from_string(moduleDef->m_name, SRC_CS);
149+
return polyglot_from_string(moduleDef->name, SRC_CS);
146150
}
147151

148152
void* graal_hpy_get_m_doc(HPyModuleDef *moduleDef) {
149-
const char *m_doc = moduleDef->m_doc;
153+
const char *m_doc = moduleDef->doc;
150154
if (m_doc) {
151155
return polyglot_from_string(m_doc, SRC_CS);
152156
}
@@ -167,6 +171,22 @@ HPyFunc_Signature graal_hpy_meth_get_signature(HPyMeth *methodDef) {
167171
return methodDef->signature;
168172
}
169173

174+
void* graal_hpy_get_field_i(HPyField *hf) {
175+
return hf->_i;
176+
}
177+
178+
void graal_hpy_set_field_i(HPyField *hf, void* i) {
179+
hf->_i = i;
180+
}
181+
182+
void* graal_hpy_get_global_i(HPyGlobal *hg) {
183+
return hg->_i;
184+
}
185+
186+
void graal_hpy_set_global_i(HPyGlobal *hg, void* i) {
187+
hg->_i = i;
188+
}
189+
170190
/* getters for HPySlot */
171191

172192
HPySlot_Slot graal_hpy_slot_get_slot(HPySlot *slot) {
@@ -297,6 +317,17 @@ void* graal_hpy_from_string(const char *ptr) {
297317
return polyglot_from_string(ptr, SRC_CS);
298318
}
299319

320+
int graal_hpy_get_errno() {
321+
return errno;
322+
}
323+
324+
void* graal_hpy_get_strerror(int i) {
325+
if (i != 0) {
326+
return polyglot_from_string(strerror(i), SRC_CS);
327+
}
328+
return polyglot_from_string("Error", SRC_CS);
329+
}
330+
300331
uint64_t graal_hpy_strlen(const char *ptr) {
301332
return strlen(ptr);
302333
}
@@ -624,12 +655,17 @@ HPy_buffer *graal_hpy_allocate_buffer() {
624655
#define _h2py(_x) NULL
625656

626657
typedef HPy* _HPyPtr;
658+
typedef HPyField* _HPyFieldPtr;
627659
typedef HPy _HPyConst;
660+
typedef HPyGlobal* _HPyGlobalPtr;
628661

629662
#define HPy void*
630663
#define HPyListBuilder void*
631664
#define HPyTupleBuilder void*
632665
#define HPyTracker void*
666+
#define HPyField void*
667+
#define HPyThreadState void*
668+
#define HPyGlobal void*
633669

634670
#define SELECT_CTX(__ctx__) ((__ctx__) == g_universal_ctx ? g_universal_ctx : g_debug_ctx)
635671

@@ -728,6 +764,14 @@ HPyAPI_STORAGE HPy_ssize_t _HPy_IMPL_NAME(Long_AsSsize_t)(HPyContext *ctx, HPy h
728764
return (HPy_ssize_t) UPCALL_I64(ctx_Long_AsSsize_t, ctx, h);
729765
}
730766

767+
HPyAPI_STORAGE void* _HPy_IMPL_NAME(Long_AsVoidPtr)(HPyContext *ctx, HPy h) {
768+
return (void *) UPCALL_I64(ctx_Long_AsVoidPtr, ctx, h);
769+
}
770+
771+
HPyAPI_STORAGE double _HPy_IMPL_NAME(Long_AsDouble)(HPyContext *ctx, HPy h) {
772+
return UPCALL_DOUBLE(ctx_Long_AsDouble, ctx, h);
773+
}
774+
731775
HPyAPI_STORAGE HPy _HPy_IMPL_NAME(Float_FromDouble)(HPyContext *ctx, double v)
732776
{
733777
return UPCALL_HPY(ctx_Float_FromDouble, ctx, v);
@@ -943,11 +987,23 @@ HPyAPI_STORAGE void _HPy_IMPL_NAME(Err_SetObject)(HPyContext *ctx, HPy h_type, H
943987
UPCALL_VOID(ctx_Err_SetObject, ctx, h_type, h_value);
944988
}
945989

990+
HPyAPI_STORAGE HPy _HPy_IMPL_NAME(Err_SetFromErrnoWithFilename)(HPyContext *ctx, HPy h_type, const char *filename_fsencoded) {
991+
return UPCALL_HPY(ctx_Err_SetFromErrnoWithFilename, ctx, h_type, filename_fsencoded);
992+
}
993+
994+
HPyAPI_STORAGE HPy _HPy_IMPL_NAME(Err_SetFromErrnoWithFilenameObjects)(HPyContext *ctx, HPy h_type, HPy filename1, HPy filename2) {
995+
return UPCALL_HPY(ctx_Err_SetFromErrnoWithFilenameObjects, ctx, h_type, filename1, filename2);
996+
}
997+
946998
HPyAPI_STORAGE int _HPy_IMPL_NAME(Err_Occurred)(HPyContext *ctx)
947999
{
9481000
return (int) polyglot_as_i32(UPCALL_HPY0(ctx_Err_Occurred, ctx));
9491001
}
9501002

1003+
HPyAPI_STORAGE int _HPy_IMPL_NAME(Err_ExceptionMatches)(HPyContext *ctx, HPy exc) {
1004+
return (int) UPCALL_I32(ctx_Err_ExceptionMatches, ctx, exc);
1005+
}
1006+
9511007
HPyAPI_STORAGE HPy _HPy_IMPL_NAME(Err_NoMemory)(HPyContext *ctx)
9521008
{
9531009
return UPCALL_HPY0(ctx_Err_NoMemory, ctx);
@@ -966,6 +1022,18 @@ HPyAPI_STORAGE HPy _HPy_IMPL_NAME(Err_NewExceptionWithDoc)(HPyContext *ctx, cons
9661022
return UPCALL_HPY(ctx_Err_NewExceptionWithDoc, ctx, name, doc, base, dict);
9671023
}
9681024

1025+
HPyAPI_STORAGE int _HPy_IMPL_NAME(Err_WarnEx)(HPyContext *ctx, HPy category, const char *message, HPy_ssize_t stack_level) {
1026+
return (int) UPCALL_I32(ctx_Err_WarnEx, ctx, category, message, stack_level);
1027+
}
1028+
1029+
HPyAPI_STORAGE void _HPy_IMPL_NAME(Err_WriteUnraisable)(HPyContext *ctx, HPy obj) {
1030+
UPCALL_VOID(ctx_Err_WriteUnraisable, ctx, obj);
1031+
}
1032+
1033+
HPyAPI_STORAGE void _HPy_IMPL_NAME(FatalError)(HPyContext *ctx, const char *msg) {
1034+
UPCALL_VOID(ctx_FatalError, ctx, msg);
1035+
}
1036+
9691037
HPyAPI_STORAGE int _HPy_IMPL_NAME_NOPREFIX(IsTrue)(HPyContext *ctx, HPy h)
9701038
{
9711039
return UPCALL_HPY(ctx_IsTrue, ctx, h);
@@ -1026,6 +1094,10 @@ HPyAPI_STORAGE HPy _HPy_IMPL_NAME_NOPREFIX(GetItem_s)(HPyContext *ctx, HPy obj,
10261094
return UPCALL_HPY(ctx_GetItem_s, ctx, obj, key);
10271095
}
10281096

1097+
HPyAPI_STORAGE int _HPy_IMPL_NAME_NOPREFIX(Contains)(HPyContext *ctx, HPy container, HPy key) {
1098+
return (int) UPCALL_I32(ctx_Contains, ctx, container, key);
1099+
}
1100+
10291101
HPyAPI_STORAGE int _HPy_IMPL_NAME_NOPREFIX(SetItem)(HPyContext *ctx, HPy obj, HPy key, HPy value)
10301102
{
10311103
return UPCALL_HPY(ctx_SetItem, ctx, obj, key, value);
@@ -1151,6 +1223,14 @@ HPyAPI_STORAGE int _HPy_IMPL_NAME(Unicode_Check)(HPyContext *ctx, HPy h)
11511223
return (int) UPCALL_I32(ctx_Unicode_Check, ctx, h);
11521224
}
11531225

1226+
HPyAPI_STORAGE HPy _HPy_IMPL_NAME(Unicode_AsASCIIString)(HPyContext *ctx, HPy h) {
1227+
return UPCALL_HPY(ctx_Unicode_AsASCIIString, ctx, h);
1228+
}
1229+
1230+
HPyAPI_STORAGE HPy _HPy_IMPL_NAME(Unicode_AsLatin1String)(HPyContext *ctx, HPy h) {
1231+
return UPCALL_HPY(ctx_Unicode_AsLatin1String, ctx, h);
1232+
}
1233+
11541234
HPyAPI_STORAGE HPy _HPy_IMPL_NAME(Unicode_AsUTF8String)(HPyContext *ctx, HPy h)
11551235
{
11561236
return UPCALL_HPY(ctx_Unicode_AsUTF8String, ctx, h);
@@ -1170,6 +1250,26 @@ HPyAPI_STORAGE HPy _HPy_IMPL_NAME(Unicode_DecodeFSDefault)(HPyContext *ctx, cons
11701250
return UPCALL_HPY(ctx_Unicode_DecodeFSDefault, ctx, v);
11711251
}
11721252

1253+
HPyAPI_STORAGE HPy _HPy_IMPL_NAME(Unicode_DecodeFSDefaultAndSize)(HPyContext *ctx, const char *v, HPy_ssize_t size) {
1254+
return UPCALL_HPY(ctx_Unicode_DecodeFSDefaultAndSize, ctx, v, size);
1255+
}
1256+
1257+
HPyAPI_STORAGE HPy _HPy_IMPL_NAME(Unicode_EncodeFSDefault)(HPyContext *ctx, HPy h) {
1258+
return UPCALL_HPY(ctx_Unicode_EncodeFSDefault, ctx, h);
1259+
}
1260+
1261+
HPyAPI_STORAGE uint32_t _HPy_IMPL_NAME(Unicode_ReadChar)(HPyContext *ctx, HPy h, HPy_ssize_t index) {
1262+
return (uint32_t) UPCALL_I32(ctx_Unicode_ReadChar, ctx, h, index);
1263+
}
1264+
1265+
HPyAPI_STORAGE HPy _HPy_IMPL_NAME(Unicode_DecodeLatin1)(HPyContext *ctx, const char *s, HPy_ssize_t size, const char *errors) {
1266+
return UPCALL_HPY(ctx_Unicode_DecodeLatin1, ctx, s, size, errors );
1267+
}
1268+
1269+
HPyAPI_STORAGE HPy _HPy_IMPL_NAME(Unicode_DecodeASCII)(HPyContext *ctx, const char *s, HPy_ssize_t size, const char *errors) {
1270+
return UPCALL_HPY(ctx_Unicode_DecodeASCII, ctx, s, size, errors );
1271+
}
1272+
11731273
HPyAPI_STORAGE int _HPy_IMPL_NAME(List_Check)(HPyContext *ctx, HPy h)
11741274
{
11751275
return (int) UPCALL_I32(ctx_List_Check, ctx, h);
@@ -1271,6 +1371,30 @@ HPyAPI_STORAGE void _HPy_IMPL_NAME(Tracker_Close)(HPyContext *ctx, HPyTracker ht
12711371
UPCALL_VOID(ctx_Tracker_Close, ctx, ht);
12721372
}
12731373

1374+
HPyAPI_STORAGE void _HPy_IMPL_NAME(Field_Store)(HPyContext *ctx, HPy target_object, _HPyFieldPtr target_field, HPy h) {
1375+
UPCALL_VOID(ctx_Field_Store, ctx, target_object, target_field, h);
1376+
}
1377+
1378+
HPyAPI_STORAGE HPy _HPy_IMPL_NAME(Field_Load)(HPyContext *ctx, HPy source_object, HPyField source_field) {
1379+
return UPCALL_HPY(ctx_Field_Load, ctx, source_object, source_field);
1380+
}
1381+
1382+
HPyAPI_STORAGE HPyThreadState _HPy_IMPL_NAME(LeavePythonExecution)(HPyContext *ctx) {
1383+
return UPCALL_HPY0(ctx_LeavePythonExecution, ctx);
1384+
}
1385+
1386+
HPyAPI_STORAGE void _HPy_IMPL_NAME(ReenterPythonExecution)(HPyContext *ctx, HPyThreadState state) {
1387+
UPCALL_VOID(ctx_ReenterPythonExecution, ctx, state);
1388+
}
1389+
1390+
HPyAPI_STORAGE void _HPy_IMPL_NAME(Global_Store)(HPyContext *ctx, _HPyGlobalPtr global, HPy h) {
1391+
UPCALL_VOID(ctx_Global_Store, ctx, global, h);
1392+
}
1393+
1394+
HPyAPI_STORAGE HPy _HPy_IMPL_NAME(Global_Load)(HPyContext *ctx, HPyGlobal global) {
1395+
return UPCALL_HPY(ctx_Global_Load, ctx, global);
1396+
}
1397+
12741398
HPyAPI_STORAGE void _HPy_IMPL_NAME(Dump)(HPyContext *ctx, HPy h) {
12751399
UPCALL_VOID(ctx_Dump, ctx, h);
12761400
}
@@ -1279,6 +1403,9 @@ HPyAPI_STORAGE void _HPy_IMPL_NAME(Dump)(HPyContext *ctx, HPy h) {
12791403
#undef HPyListBuilder
12801404
#undef HPyTupleBuilder
12811405
#undef HPyTracker
1406+
#undef HPyField
1407+
#undef HPyThreadState
1408+
#undef HPyGlobal
12821409

12831410
#undef _HPy_IMPL_NAME_NOPREFIX
12841411
#undef _HPy_IMPL_NAME
@@ -1292,14 +1419,14 @@ HPyContext *graal_hpy_context_to_native(HPyContext *managed_context, HPyContext
12921419
HPyContext *native_context = graal_native_context_get_hpy_context(full_native_context);
12931420

12941421
#define COPY(__member) native_context->__member = managed_context->__member
1295-
COPY(name);
1296-
COPY(ctx_version);
1297-
COPY(h_None);
1298-
COPY(h_True);
1299-
COPY(h_False);
1300-
COPY(h_NotImplemented);
1301-
COPY(h_Ellipsis);
1302-
COPY(h_BaseException);
1422+
COPY(name);
1423+
COPY(ctx_version);
1424+
COPY(h_None);
1425+
COPY(h_True);
1426+
COPY(h_False);
1427+
COPY(h_NotImplemented);
1428+
COPY(h_Ellipsis);
1429+
COPY(h_BaseException);
13031430
COPY(h_Exception);
13041431
COPY(h_StopAsyncIteration);
13051432
COPY(h_StopIteration);
@@ -1365,7 +1492,9 @@ HPyContext *graal_hpy_context_to_native(HPyContext *managed_context, HPyContext
13651492
COPY(h_ResourceWarning);
13661493
COPY(h_BaseObjectType);
13671494
COPY(h_TypeType);
1495+
COPY(h_BoolType);
13681496
COPY(h_LongType);
1497+
COPY(h_FloatType);
13691498
COPY(h_UnicodeType);
13701499
COPY(h_TupleType);
13711500
COPY(h_ListType);
@@ -1449,6 +1578,7 @@ HPyContext *graal_hpy_context_to_native(HPyContext *managed_context, HPyContext
14491578
HPY_CTX_UPCALL(ctx_GetItem);
14501579
HPY_CTX_UPCALL(ctx_GetItem_i);
14511580
HPY_CTX_UPCALL(ctx_GetItem_s);
1581+
HPY_CTX_UPCALL(ctx_Contains);
14521582
HPY_CTX_UPCALL(ctx_SetItem);
14531583
HPY_CTX_UPCALL(ctx_SetItem_i);
14541584
HPY_CTX_UPCALL(ctx_SetItem_s);
@@ -1500,7 +1630,29 @@ HPyContext *graal_hpy_context_to_native(HPyContext *managed_context, HPyContext
15001630
HPY_CTX_UPCALL(ctx_Tracker_Add);
15011631
HPY_CTX_UPCALL(ctx_Tracker_ForgetAll);
15021632
HPY_CTX_UPCALL(ctx_Tracker_Close);
1633+
HPY_CTX_UPCALL(ctx_Field_Store);
1634+
HPY_CTX_UPCALL(ctx_Field_Load);
1635+
HPY_CTX_UPCALL(ctx_LeavePythonExecution);
1636+
HPY_CTX_UPCALL(ctx_ReenterPythonExecution);
1637+
HPY_CTX_UPCALL(ctx_Global_Store);
1638+
HPY_CTX_UPCALL(ctx_Global_Load);
15031639
HPY_CTX_UPCALL(ctx_Dump);
1640+
HPY_CTX_UPCALL(ctx_Long_AsVoidPtr);
1641+
HPY_CTX_UPCALL(ctx_Long_AsDouble);
1642+
HPY_CTX_UPCALL(ctx_Err_SetFromErrnoWithFilename);
1643+
HPY_CTX_UPCALL(ctx_Err_SetFromErrnoWithFilenameObjects);
1644+
HPY_CTX_UPCALL(ctx_Err_ExceptionMatches);
1645+
HPY_CTX_UPCALL(ctx_Err_WarnEx);
1646+
HPY_CTX_UPCALL(ctx_Err_WriteUnraisable);
1647+
HPY_CTX_UPCALL(ctx_FatalError);
1648+
HPY_CTX_UPCALL(ctx_Unicode_AsASCIIString);
1649+
HPY_CTX_UPCALL(ctx_Unicode_AsLatin1String);
1650+
HPY_CTX_UPCALL(ctx_Unicode_DecodeFSDefaultAndSize);
1651+
HPY_CTX_UPCALL(ctx_Unicode_EncodeFSDefault);
1652+
HPY_CTX_UPCALL(ctx_Unicode_ReadChar);
1653+
HPY_CTX_UPCALL(ctx_Unicode_DecodeLatin1);
1654+
HPY_CTX_UPCALL(ctx_Unicode_DecodeASCII);
1655+
15041656
#undef HPY_CTX_UPCALL
15051657

15061658
return native_context;

0 commit comments

Comments
 (0)