Skip to content

Commit

Permalink
Merge pull request #471 from hpyproject/fa/cpy_impl_improvements
Browse files Browse the repository at this point in the history
Improve implementation of HPy_(Type | Is | GetItem_i) in CPython ABI mode.
  • Loading branch information
fangerer authored Jan 30, 2024
2 parents 928c94d + 8b2618f commit ef1eef1
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 13 deletions.
5 changes: 0 additions & 5 deletions hpy/devel/include/hpy/cpython/autogen_api_impl.h

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

9 changes: 8 additions & 1 deletion hpy/devel/include/hpy/cpython/misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -311,14 +311,21 @@ HPyAPI_FUNC void _HPy_Dump(HPyContext *ctx, HPy h)
ctx_Dump(ctx, h);
}

HPyAPI_FUNC HPy HPy_Type(HPyContext *ctx, HPy h_obj)
{
PyTypeObject *tp = Py_TYPE(_h2py(h_obj));
Py_INCREF(tp);
return _py2h((PyObject *)tp);
}

HPyAPI_FUNC int HPy_TypeCheck(HPyContext *ctx, HPy h_obj, HPy h_type)
{
return ctx_TypeCheck(ctx, h_obj, h_type);
}

HPyAPI_FUNC int HPy_Is(HPyContext *ctx, HPy h_obj, HPy h_other)
{
return ctx_Is(ctx, h_obj, h_other);
return _h2py(h_obj) == _h2py(h_other);
}

HPyAPI_FUNC HPyListBuilder HPyListBuilder_New(HPyContext *ctx, HPy_ssize_t initial_size)
Expand Down
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 @@ -31,6 +31,7 @@ _HPy_HIDDEN HPy ctx_Module_Create(HPyContext *ctx, HPyModuleDef *hpydef);

// ctx_object.c
_HPy_HIDDEN void ctx_Dump(HPyContext *ctx, HPy h);
_HPy_HIDDEN HPy ctx_Type(HPyContext *ctx, HPy h_obj);
_HPy_HIDDEN int ctx_TypeCheck(HPyContext *ctx, HPy h_obj, HPy h_type);
_HPy_HIDDEN int ctx_Is(HPyContext *ctx, HPy h_obj, HPy h_other);
_HPy_HIDDEN HPy ctx_GetItem_i(HPyContext *ctx, HPy obj, HPy_ssize_t idx);
Expand Down
14 changes: 13 additions & 1 deletion hpy/devel/src/runtime/ctx_object.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ ctx_Dump(HPyContext *ctx, HPy h)
_PyObject_Dump(_h2py(h));
}

_HPy_HIDDEN HPy
ctx_Type(HPyContext *ctx, HPy obj)
{
PyTypeObject *tp = Py_TYPE(_h2py(obj));
Py_INCREF(tp);
return _py2h((PyObject *)tp);
}

/* NOTE: In contrast to CPython, HPy has to check that 'h_type' is a type. This
is not necessary on CPython because it requires C type 'PyTypeObject *' but
here we can only receive an HPy handle. Appropriate checking of the argument
Expand All @@ -34,10 +42,14 @@ ctx_Is(HPyContext *ctx, HPy h_obj, HPy h_other)

_HPy_HIDDEN HPy
ctx_GetItem_i(HPyContext *ctx, HPy obj, HPy_ssize_t idx) {
PyObject *py_obj = _h2py(obj);
if (PySequence_Check(py_obj)) {
return _py2h(PySequence_GetItem(py_obj, idx));
}
PyObject* key = PyLong_FromSsize_t(idx);
if (key == NULL)
return HPy_NULL;
HPy result = _py2h(PyObject_GetItem(_h2py(obj), key));
HPy result = _py2h(PyObject_GetItem(py_obj, key));
Py_DECREF(key);
return result;
}
Expand Down
3 changes: 2 additions & 1 deletion hpy/tools/autogen/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
'HPyTracker_ForgetAll': None,
'HPyTracker_Close': None,
'_HPy_Dump': None,
'HPy_Type': 'PyObject_Type',
'HPy_Type': None,
'HPy_TypeCheck': None,
'HPy_Is': None,
'HPyBytes_FromStringAndSize': None,
Expand Down Expand Up @@ -182,6 +182,7 @@
'PySlice_AdjustIndices': 'HPySlice_AdjustIndices',
'PyType_IsSubtype': 'HPyType_IsSubtype',
'PyObject_Call': 'HPy_CallTupleDict',
'PyObject_Type': 'HPy_Type',
'PyObject_Vectorcall': 'HPy_Call',
'PyObject_VectorcallMethod': 'HPy_CallMethod',
}
Expand Down
5 changes: 0 additions & 5 deletions hpy/universal/src/autogen_ctx_impl.h

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

0 comments on commit ef1eef1

Please sign in to comment.