Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dictionary type to HPy #458

Merged
merged 3 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions hpy/debug/src/autogen_debug_ctx_init.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions hpy/debug/src/debug_ctx.c
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ static const char *get_builtin_shape_name(HPyType_BuiltinShape shape)
SHAPE_NAME(HPyType_BuiltinShape_Unicode)
SHAPE_NAME(HPyType_BuiltinShape_Tuple)
SHAPE_NAME(HPyType_BuiltinShape_List)
SHAPE_NAME(HPyType_BuiltinShape_Dict)
}
return "<unknown shape>";
#undef SHAPE_NAME
Expand Down Expand Up @@ -372,6 +373,8 @@ MAKE_debug_ctx_AsStruct(Tuple)

MAKE_debug_ctx_AsStruct(List)

MAKE_debug_ctx_AsStruct(Dict)

/* ~~~ debug mode implementation of HPyTracker ~~~

This is a bit special and it's worth explaining what is going on.
Expand Down
1 change: 1 addition & 0 deletions hpy/devel/include/hpy/cpython/autogen_ctx.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions hpy/devel/include/hpy/cpython/misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ HPyAPI_FUNC HPyContext * _HPyGetContext(void) {
ctx->h_MemoryViewType = _py2h((PyObject *)&PyMemoryView_Type);
ctx->h_CapsuleType = _py2h((PyObject *)&PyCapsule_Type);
ctx->h_SliceType = _py2h((PyObject *)&PySlice_Type);
ctx->h_DictType = _py2h((PyObject *)&PyDict_Type);
/* Reflection */
ctx->h_Builtins = _py2h(PyEval_GetBuiltins());
}
Expand Down Expand Up @@ -271,6 +272,11 @@ HPyAPI_FUNC void* _HPy_AsStruct_List(HPyContext *ctx, HPy h)
return ctx_AsStruct_List(ctx, h);
}

HPyAPI_FUNC void* _HPy_AsStruct_Dict(HPyContext *ctx, HPy h)
{
return ctx_AsStruct_Dict(ctx, h);
}

HPyAPI_FUNC HPy HPy_CallTupleDict(HPyContext *ctx, HPy callable, HPy args, HPy kw)
{
return ctx_CallTupleDict(ctx, callable, args, kw);
Expand Down
7 changes: 7 additions & 0 deletions hpy/devel/include/hpy/hpytype.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ typedef enum {
* need to specify base class ``ctx->h_ListType``.
*/
HPyType_BuiltinShape_List = 6,

/**
* The type inherits from built-in type ``dict``. If using this shape, you
* need to specify base class ``ctx->h_DictType``.
*/
HPyType_BuiltinShape_Dict = 7,
} HPyType_BuiltinShape;

typedef struct {
Expand Down Expand Up @@ -283,5 +289,6 @@ _HPyType_HELPER_X(_HPyType_HELPER_FNAME(__VA_ARGS__))(HPyContext *ctx, HPy h) \
#define HPyType_BuiltinShape_Unicode_AsStruct _HPy_AsStruct_Unicode
#define HPyType_BuiltinShape_Tuple_AsStruct _HPy_AsStruct_Tuple
#define HPyType_BuiltinShape_List_AsStruct _HPy_AsStruct_List
#define HPyType_BuiltinShape_Dict_AsStruct _HPy_AsStruct_Dict

#endif /* HPY_UNIVERSAL_HPYTYPE_H */
1 change: 1 addition & 0 deletions hpy/devel/include/hpy/runtime/ctx_funcs.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ _HPy_HIDDEN void* ctx_AsStruct_Float(HPyContext *ctx, HPy h);
_HPy_HIDDEN void* ctx_AsStruct_Unicode(HPyContext *ctx, HPy h);
_HPy_HIDDEN void* ctx_AsStruct_Tuple(HPyContext *ctx, HPy h);
_HPy_HIDDEN void* ctx_AsStruct_List(HPyContext *ctx, HPy h);
_HPy_HIDDEN void* ctx_AsStruct_Dict(HPyContext *ctx, HPy h);
_HPy_HIDDEN void* ctx_AsStruct_Slow(HPyContext *ctx, HPy h);
_HPy_HIDDEN HPy ctx_Type_FromSpec(HPyContext *ctx, HPyType_Spec *hpyspec,
HPyType_SpecParam *params);
Expand Down
2 changes: 2 additions & 0 deletions hpy/devel/include/hpy/universal/autogen_ctx.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions hpy/devel/include/hpy/universal/autogen_trampolines.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions hpy/devel/src/runtime/ctx_type.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ FULLY_ALIGNED_SPACE(PyFloatObject)
FULLY_ALIGNED_SPACE(PyUnicodeObject)
FULLY_ALIGNED_SPACE(PyTupleObject)
FULLY_ALIGNED_SPACE(PyListObject)
FULLY_ALIGNED_SPACE(PyDictObject)

#define _HPy_HEAD_SIZE(HEAD) (offsetof(_HPy_FullyAlignedSpaceFor##HEAD, payload))

Expand All @@ -101,6 +102,8 @@ _HPy_GetHeaderSize(HPyType_BuiltinShape shape)
return _HPy_HEAD_SIZE(PyTupleObject);
case HPyType_BuiltinShape_List:
return _HPy_HEAD_SIZE(PyListObject);
case HPyType_BuiltinShape_Dict:
return _HPy_HEAD_SIZE(PyDictObject);
}
return -1;
}
Expand Down Expand Up @@ -1504,6 +1507,12 @@ ctx_AsStruct_List(HPyContext *ctx, HPy h)
return _HPy_Payload(_h2py(h), HPyType_BuiltinShape_List);
}

_HPy_HIDDEN void*
ctx_AsStruct_Dict(HPyContext *ctx, HPy h)
{
return _HPy_Payload(_h2py(h), HPyType_BuiltinShape_Dict);
}

_HPy_HIDDEN void*
ctx_AsStruct_Slow(HPyContext *ctx, HPy h)
{
Expand Down
1 change: 1 addition & 0 deletions hpy/tools/autogen/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
'_HPy_AsStruct_Unicode': None,
'_HPy_AsStruct_Tuple': None,
'_HPy_AsStruct_List': None,
'_HPy_AsStruct_Dict': None,
'_HPy_AsStruct_Legacy': None,
'_HPyType_GetBuiltinShape': None,
'_HPy_CallRealFunctionFromTrampoline': None,
Expand Down
1 change: 1 addition & 0 deletions hpy/tools/autogen/debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class autogen_debug_wrappers(AutoGenFile):
'_HPy_AsStruct_Unicode',
'_HPy_AsStruct_Tuple',
'_HPy_AsStruct_List',
'_HPy_AsStruct_Dict',
'HPyTracker_New',
'HPyTracker_Add',
'HPyTracker_ForgetAll',
Expand Down
2 changes: 1 addition & 1 deletion hpy/tools/autogen/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import pycparser
from pycparser import c_ast
from pycparser.c_generator import CGenerator
from distutils.sysconfig import get_config_var
from sysconfig import get_config_var
from .conf import SPECIAL_CASES, RETURN_CONSTANT

PUBLIC_API_H = py.path.local(__file__).dirpath('public_api.h')
Expand Down
3 changes: 3 additions & 0 deletions hpy/tools/autogen/public_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ HPy_ID(239) HPy h_BytesType; /* built-in 'bytes' */
HPy_ID(240) HPy h_MemoryViewType; /* built-in 'memoryview' */
HPy_ID(241) HPy h_CapsuleType; /* built-in 'capsule' */
HPy_ID(242) HPy h_SliceType; /* built-in 'slice' */
HPy_ID(263) HPy h_DictType; /* built-in 'dict' */

/* Reflection */
HPy_ID(243) HPy h_Builtins; /* dict of builtins */
Expand Down Expand Up @@ -539,6 +540,8 @@ HPy_ID(232)
void* _HPy_AsStruct_Tuple(HPyContext *ctx, HPy h);
HPy_ID(233)
void* _HPy_AsStruct_List(HPyContext *ctx, HPy h);
HPy_ID(264)
void* _HPy_AsStruct_Dict(HPyContext *ctx, HPy h);
HPy_ID(234)
HPyType_BuiltinShape _HPyType_GetBuiltinShape(HPyContext *ctx, HPy h_type);

Expand Down
7 changes: 5 additions & 2 deletions hpy/trace/src/autogen_trace_ctx_init.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions hpy/trace/src/autogen_trace_func_table.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions hpy/trace/src/autogen_trace_wrappers.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions hpy/universal/src/autogen_ctx_def.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions hpy/universal/src/hpymodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,7 @@ static void init_universal_ctx(HPyContext *ctx)
ctx->h_MemoryViewType = _py2h((PyObject *)&PyMemoryView_Type);
ctx->h_CapsuleType = _py2h((PyObject *)&PyCapsule_Type);
ctx->h_SliceType = _py2h((PyObject *)&PySlice_Type);
ctx->h_DictType = _py2h((PyObject *)&PyDict_Type);
/* Reflection */
ctx->h_Builtins = _py2h(PyEval_GetBuiltins());
}
Expand Down
3 changes: 2 additions & 1 deletion test/test_00_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ def test_builtin_handles(self):
case 20: h = ctx->h_MemoryViewType; break;
case 21: h = ctx->h_SliceType; break;
case 22: h = ctx->h_Builtins; break;
case 23: h = ctx->h_DictType; break;
case 2048: h = ctx->h_CapsuleType; break;
default:
HPyErr_SetString(ctx, ctx->h_ValueError, "invalid choice");
Expand All @@ -278,7 +279,7 @@ def test_builtin_handles(self):
'<NULL>', None, False, True, ValueError, TypeError, IndexError,
SystemError, object, type, bool, int, float, str, tuple, list,
NotImplemented, Ellipsis, complex, bytes, memoryview, slice,
builtins.__dict__
builtins.__dict__, dict
)
for i, obj in enumerate(builtin_objs):
if i == 0:
Expand Down