Skip to content

Commit afcfab6

Browse files
committed
Update HPy inlined files: 42a80d4c
1 parent 039e8a1 commit afcfab6

File tree

11 files changed

+193
-17
lines changed

11 files changed

+193
-17
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,11 @@ HPyAPI_FUNC int HPy_TypeCheck(HPyContext *ctx, HPy h_obj, HPy h_type)
339339
return ctx_TypeCheck(ctx, h_obj, h_type);
340340
}
341341

342+
HPyAPI_FUNC int HPy_TypeCheck_g(HPyContext *ctx, HPy h_obj, HPyGlobal g_type)
343+
{
344+
return ctx_TypeCheck_g(ctx, h_obj, g_type);
345+
}
346+
342347
HPyAPI_FUNC int HPyType_IsSubtype(HPyContext *ctx, HPy h_sub, HPy h_type)
343348
{
344349
return ctx_Type_IsSubtype(ctx, h_sub, h_type);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ _HPy_HIDDEN HPy ctx_Module_Create(HPyContext *ctx, HPyModuleDef *hpydef);
3333
// ctx_object.c
3434
_HPy_HIDDEN void ctx_Dump(HPyContext *ctx, HPy h);
3535
_HPy_HIDDEN int ctx_TypeCheck(HPyContext *ctx, HPy h_obj, HPy h_type);
36+
_HPy_HIDDEN int ctx_TypeCheck_g(HPyContext *ctx, HPy h_obj, HPyGlobal g_type);
3637
_HPy_HIDDEN int ctx_Type_IsSubtype(HPyContext *ctx, HPy h_sub, HPy h_type);
3738
_HPy_HIDDEN int ctx_Is(HPyContext *ctx, HPy h_obj, HPy h_other);
39+
_HPy_HIDDEN int ctx_Is_g(HPyContext *ctx, HPy h_obj, HPyGlobal g_other);
3840
_HPy_HIDDEN HPy ctx_GetItem_i(HPyContext *ctx, HPy obj, HPy_ssize_t idx);
3941
_HPy_HIDDEN HPy ctx_GetItem_s(HPyContext *ctx, HPy obj, const char *key);
4042
_HPy_HIDDEN int ctx_SetItem_i(HPyContext *ctx, HPy obj, HPy_ssize_t idx, HPy value);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,4 +268,6 @@ struct _HPyContext_s {
268268
const char *(*ctx_Type_GetName)(HPyContext *ctx, HPy type);
269269
HPy (*ctx_SeqIter_New)(HPyContext *ctx, HPy seq);
270270
int (*ctx_Type_CheckSlot)(HPyContext *ctx, HPy type, HPyDef *value);
271+
int (*ctx_TypeCheck_g)(HPyContext *ctx, HPy obj, HPyGlobal type);
272+
int (*ctx_Is_g)(HPyContext *ctx, HPy obj, HPyGlobal other);
271273
};

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,10 @@ HPyAPI_FUNC int HPy_TypeCheck(HPyContext *ctx, HPy obj, HPy type) {
378378
return ctx->ctx_TypeCheck ( ctx, obj, type );
379379
}
380380

381+
HPyAPI_FUNC int HPy_TypeCheck_g(HPyContext *ctx, HPy obj, HPyGlobal type) {
382+
return ctx->ctx_TypeCheck_g ( ctx, obj, type );
383+
}
384+
381385
HPyAPI_FUNC int HPy_SetType(HPyContext *ctx, HPy obj, HPy type) {
382386
return ctx->ctx_SetType ( ctx, obj, type );
383387
}
@@ -394,6 +398,10 @@ HPyAPI_FUNC int HPy_Is(HPyContext *ctx, HPy obj, HPy other) {
394398
return ctx->ctx_Is ( ctx, obj, other );
395399
}
396400

401+
HPyAPI_FUNC int HPy_Is_g(HPyContext *ctx, HPy obj, HPyGlobal other) {
402+
return ctx->ctx_Is_g ( ctx, obj, other );
403+
}
404+
397405
HPyAPI_FUNC void *HPy_AsStruct(HPyContext *ctx, HPy h) {
398406
return ctx->ctx_AsStruct ( ctx, h );
399407
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11

22
// automatically generated by setup.py:get_scm_config()
3-
#define HPY_VERSION "0.0.5.dev71+g5ebc33c"
4-
#define HPY_GIT_REVISION "5ebc33c"
3+
#define HPY_VERSION "0.0.5.dev85+g42a80d4c"
4+
#define HPY_GIT_REVISION "42a80d4c"

graalpython/com.oracle.graal.python.jni/src/debug/autogen_debug_ctx_init.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,12 @@ int debug_ctx_SetItem_i(HPyContext *dctx, DHPy obj, HPy_ssize_t idx, DHPy value)
103103
int debug_ctx_SetItem_s(HPyContext *dctx, DHPy obj, const char *key, DHPy value);
104104
DHPy debug_ctx_Type(HPyContext *dctx, DHPy obj);
105105
int debug_ctx_TypeCheck(HPyContext *dctx, DHPy obj, DHPy type);
106+
int debug_ctx_TypeCheck_g(HPyContext *dctx, DHPy obj, HPyGlobal type);
106107
int debug_ctx_SetType(HPyContext *dctx, DHPy obj, DHPy type);
107108
int debug_ctx_Type_IsSubtype(HPyContext *dctx, DHPy sub, DHPy type);
108109
const char *debug_ctx_Type_GetName(HPyContext *dctx, DHPy type);
109110
int debug_ctx_Is(HPyContext *dctx, DHPy obj, DHPy other);
111+
int debug_ctx_Is_g(HPyContext *dctx, DHPy obj, HPyGlobal other);
110112
void *debug_ctx_AsStruct(HPyContext *dctx, DHPy h);
111113
void *debug_ctx_AsStructLegacy(HPyContext *dctx, DHPy h);
112114
DHPy debug_ctx_New(HPyContext *dctx, DHPy h_type, void **data);
@@ -360,10 +362,12 @@ static inline void debug_ctx_init_fields(HPyContext *dctx, HPyContext *uctx)
360362
dctx->ctx_SetItem_s = &debug_ctx_SetItem_s;
361363
dctx->ctx_Type = &debug_ctx_Type;
362364
dctx->ctx_TypeCheck = &debug_ctx_TypeCheck;
365+
dctx->ctx_TypeCheck_g = &debug_ctx_TypeCheck_g;
363366
dctx->ctx_SetType = &debug_ctx_SetType;
364367
dctx->ctx_Type_IsSubtype = &debug_ctx_Type_IsSubtype;
365368
dctx->ctx_Type_GetName = &debug_ctx_Type_GetName;
366369
dctx->ctx_Is = &debug_ctx_Is;
370+
dctx->ctx_Is_g = &debug_ctx_Is_g;
367371
dctx->ctx_AsStruct = &debug_ctx_AsStruct;
368372
dctx->ctx_AsStructLegacy = &debug_ctx_AsStructLegacy;
369373
dctx->ctx_New = &debug_ctx_New;

graalpython/com.oracle.graal.python.jni/src/debug/autogen_debug_wrappers.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,11 @@ int debug_ctx_TypeCheck(HPyContext *dctx, DHPy obj, DHPy type)
462462
return HPy_TypeCheck(get_info(dctx)->uctx, DHPy_unwrap(dctx, obj), DHPy_unwrap(dctx, type));
463463
}
464464

465+
int debug_ctx_TypeCheck_g(HPyContext *dctx, DHPy obj, HPyGlobal type)
466+
{
467+
return HPy_TypeCheck_g(get_info(dctx)->uctx, DHPy_unwrap(dctx, obj), type);
468+
}
469+
465470
int debug_ctx_SetType(HPyContext *dctx, DHPy obj, DHPy type)
466471
{
467472
return HPy_SetType(get_info(dctx)->uctx, DHPy_unwrap(dctx, obj), DHPy_unwrap(dctx, type));
@@ -482,6 +487,11 @@ int debug_ctx_Is(HPyContext *dctx, DHPy obj, DHPy other)
482487
return HPy_Is(get_info(dctx)->uctx, DHPy_unwrap(dctx, obj), DHPy_unwrap(dctx, other));
483488
}
484489

490+
int debug_ctx_Is_g(HPyContext *dctx, DHPy obj, HPyGlobal other)
491+
{
492+
return HPy_Is_g(get_info(dctx)->uctx, DHPy_unwrap(dctx, obj), other);
493+
}
494+
485495
void *debug_ctx_AsStruct(HPyContext *dctx, DHPy h)
486496
{
487497
return HPy_AsStruct(get_info(dctx)->uctx, DHPy_unwrap(dctx, h));

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ ctx_TypeCheck(HPyContext *ctx, HPy h_obj, HPy h_type)
3939
return PyObject_TypeCheck(_h2py(h_obj), (PyTypeObject*)type);
4040
}
4141

42+
_HPy_HIDDEN int
43+
ctx_TypeCheck_g(HPyContext *ctx, HPy h_obj, HPyGlobal g_type)
44+
{
45+
PyObject *type = _hg2py(g_type);
46+
assert(type != NULL);
47+
if (!PyType_Check(type)) {
48+
Py_FatalError("HPy_TypeCheck arg 2 must be a type");
49+
}
50+
return PyObject_TypeCheck(_h2py(h_obj), (PyTypeObject*)type);
51+
}
52+
4253
_HPy_HIDDEN int
4354
ctx_Type_IsSubtype(HPyContext *ctx, HPy h_sub, HPy h_type)
4455
{
@@ -61,6 +72,12 @@ ctx_Is(HPyContext *ctx, HPy h_obj, HPy h_other)
6172
return _h2py(h_obj) == _h2py(h_other);
6273
}
6374

75+
_HPy_HIDDEN int
76+
ctx_Is_g(HPyContext *ctx, HPy h_obj, HPyGlobal g_other)
77+
{
78+
return _h2py(h_obj) == _hg2py(g_other);
79+
}
80+
6481
_HPy_HIDDEN HPy
6582
ctx_GetItem_i(HPyContext *ctx, HPy obj, HPy_ssize_t idx) {
6683
PyObject* key = PyLong_FromSsize_t(idx);

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

Lines changed: 130 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,135 @@ _HPy_HIDDEN struct _typeobject *get_metatype(HPyType_SpecParam *params) {
710710
}
711711
}
712712
}
713-
return &PyType_Type;
713+
return NULL;
714+
}
715+
716+
static PyObject *
717+
_PyType_FromMetaclass(PyType_Spec *spec, PyObject *bases, struct _typeobject *meta)
718+
{
719+
#if PY_VERSION_HEX >= 0x030C0000
720+
/* On Python 3.11 an newer, we can just use 'PyType_FromMetaclass'. */
721+
return PyType_FromMetaclass(meta, NULL, spec, bases);
722+
#else
723+
/* On older Python versions, we need to workaround the missing support for
724+
metaclasses. We create a temporary heap type using
725+
'PyType_FromSpecWithBases' and if a metaclass was provided, we use it to
726+
allocate the appropriate type object and memcpy most of the contents
727+
from the heap type to the manually allocated one. Then we clear some key
728+
slots and call 'PyType_Ready' on it to re-initialize everything. The
729+
temporary heap type is then expired. */
730+
731+
PyObject *temp = PyType_FromSpecWithBases(spec, bases);
732+
if (!temp)
733+
return NULL;
734+
735+
if (meta)
736+
{
737+
PyHeapTypeObject *temp_ht = (PyHeapTypeObject *) temp;
738+
PyTypeObject *temp_tp = &temp_ht->ht_type;
739+
740+
Py_INCREF(temp_ht->ht_name);
741+
Py_INCREF(temp_ht->ht_qualname);
742+
Py_XINCREF(temp_ht->ht_slots);
743+
Py_INCREF(temp_tp->tp_base);
744+
745+
/* Count the members as 'PyType_FromSpecWithBases' does such that we
746+
can properly allocate the size later when allocating the type. */
747+
Py_ssize_t nmembers = 0;
748+
PyType_Slot *slot;
749+
PyMemberDef *memb;
750+
for (slot = spec->slots; slot->slot; slot++) {
751+
if (slot->slot == Py_tp_members) {
752+
nmembers = 0;
753+
for (memb = slot->pfunc; memb->name != NULL; memb++) {
754+
nmembers++;
755+
}
756+
}
757+
}
758+
759+
PyObject *result = PyType_GenericAlloc(meta, nmembers);
760+
if (!result)
761+
goto fail;
762+
763+
PyHeapTypeObject *ht = (PyHeapTypeObject *) result;
764+
PyTypeObject *tp = &ht->ht_type;
765+
766+
/* Set the type name and qualname */
767+
const char *s = strrchr(spec->name, '.');
768+
if (s == NULL)
769+
s = (char*)spec->name;
770+
else
771+
s++;
772+
773+
memcpy(ht, temp_ht, sizeof(PyHeapTypeObject));
774+
775+
tp->ob_base.ob_base.ob_type = meta;
776+
tp->ob_base.ob_base.ob_refcnt = 1;
777+
tp->ob_base.ob_size = 0;
778+
tp->tp_as_async = &ht->as_async;
779+
tp->tp_as_number = &ht->as_number;
780+
tp->tp_as_sequence = &ht->as_sequence;
781+
tp->tp_as_mapping = &ht->as_mapping;
782+
tp->tp_as_buffer = &ht->as_buffer;
783+
tp->tp_flags = spec->flags | Py_TPFLAGS_HEAPTYPE;
784+
785+
tp->tp_dict = NULL;
786+
tp->tp_bases = NULL;
787+
tp->tp_mro = NULL;
788+
tp->tp_cache = NULL;
789+
tp->tp_subclasses = NULL;
790+
tp->tp_weaklist = NULL;
791+
ht->ht_cached_keys = NULL;
792+
tp->tp_version_tag = 0;
793+
794+
/* Refresh 'tp_doc'. This is necessary because
795+
'PyType_FromSpecWithBases' allocates its own buffer which will be
796+
free'd. */
797+
if (temp_tp->tp_doc)
798+
{
799+
size_t len = strlen(temp_tp->tp_doc)+1;
800+
char *tp_doc = PyObject_MALLOC(len);
801+
if (!tp_doc)
802+
goto fail;
803+
memcpy(tp_doc, temp_tp->tp_doc, len);
804+
tp->tp_doc = tp_doc;
805+
}
806+
807+
/* Sanity check: GC objects need to provide 'tp_traverse' and
808+
'tp_clear'. */
809+
assert(!PyType_IS_GC(tp) || tp->tp_traverse != NULL || tp->tp_clear != NULL);
810+
811+
PyType_Ready(tp);
812+
813+
/* The following is the tail of 'PyType_FromSpecWithBases'. */
814+
815+
if (tp->tp_dictoffset) {
816+
ht->ht_cached_keys = _PyDict_NewKeysForClass();
817+
}
818+
819+
/* Set type.__module__ */
820+
s = strrchr(spec->name, '.');
821+
if (s != NULL) {
822+
int err;
823+
PyObject *modname = PyUnicode_FromStringAndSize(
824+
spec->name, (Py_ssize_t)(s - spec->name));
825+
if (!modname) {
826+
goto fail;
827+
}
828+
err = PyDict_SetItemString(tp->tp_dict, "__module__", modname);
829+
Py_DECREF(modname);
830+
if (err != 0)
831+
goto fail;
832+
}
833+
return result;
834+
835+
fail:
836+
Py_DECREF(temp);
837+
Py_XDECREF(result);
838+
return NULL;
839+
}
840+
return temp;
841+
#endif
714842
}
715843

716844
HPy
@@ -783,7 +911,7 @@ ctx_Type_FromSpec(HPyContext *ctx, HPyType_Spec *hpyspec,
783911
// PyType_FromSpecWithBases does not support passing a metaclass,
784912
// so we have to use a patched CPython with PyType_FromSpecWithBasesAndMeta
785913
// See also: https://bugs.python.org/issue15870
786-
PyObject *result = PyType_FromSpecWithBasesAndMeta(spec, bases, metatype);
914+
PyObject *result = _PyType_FromMetaclass(spec, bases, metatype);
787915

788916
/* note that we do NOT free the memory which was allocated by
789917
create_method_defs, because that one is referenced internally by
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11

22
# automatically generated by setup.py:get_scm_config()
3-
__version__ = "0.0.5.dev71+g5ebc33c"
4-
__git_revision__ = "5ebc33c"
3+
__version__ = "0.0.5.dev85+g42a80d4c"
4+
__git_revision__ = "42a80d4c"

0 commit comments

Comments
 (0)