Skip to content

Commit ef1eef1

Browse files
authored
Merge pull request #471 from hpyproject/fa/cpy_impl_improvements
Improve implementation of HPy_(Type | Is | GetItem_i) in CPython ABI mode.
2 parents 928c94d + 8b2618f commit ef1eef1

File tree

6 files changed

+24
-13
lines changed

6 files changed

+24
-13
lines changed

hpy/devel/include/hpy/cpython/autogen_api_impl.h

Lines changed: 0 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

hpy/devel/include/hpy/cpython/misc.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,14 +311,21 @@ HPyAPI_FUNC void _HPy_Dump(HPyContext *ctx, HPy h)
311311
ctx_Dump(ctx, h);
312312
}
313313

314+
HPyAPI_FUNC HPy HPy_Type(HPyContext *ctx, HPy h_obj)
315+
{
316+
PyTypeObject *tp = Py_TYPE(_h2py(h_obj));
317+
Py_INCREF(tp);
318+
return _py2h((PyObject *)tp);
319+
}
320+
314321
HPyAPI_FUNC int HPy_TypeCheck(HPyContext *ctx, HPy h_obj, HPy h_type)
315322
{
316323
return ctx_TypeCheck(ctx, h_obj, h_type);
317324
}
318325

319326
HPyAPI_FUNC int HPy_Is(HPyContext *ctx, HPy h_obj, HPy h_other)
320327
{
321-
return ctx_Is(ctx, h_obj, h_other);
328+
return _h2py(h_obj) == _h2py(h_other);
322329
}
323330

324331
HPyAPI_FUNC HPyListBuilder HPyListBuilder_New(HPyContext *ctx, HPy_ssize_t initial_size)

hpy/devel/include/hpy/runtime/ctx_funcs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ _HPy_HIDDEN HPy ctx_Module_Create(HPyContext *ctx, HPyModuleDef *hpydef);
3131

3232
// ctx_object.c
3333
_HPy_HIDDEN void ctx_Dump(HPyContext *ctx, HPy h);
34+
_HPy_HIDDEN HPy ctx_Type(HPyContext *ctx, HPy h_obj);
3435
_HPy_HIDDEN int ctx_TypeCheck(HPyContext *ctx, HPy h_obj, HPy h_type);
3536
_HPy_HIDDEN int ctx_Is(HPyContext *ctx, HPy h_obj, HPy h_other);
3637
_HPy_HIDDEN HPy ctx_GetItem_i(HPyContext *ctx, HPy obj, HPy_ssize_t idx);

hpy/devel/src/runtime/ctx_object.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ ctx_Dump(HPyContext *ctx, HPy h)
1515
_PyObject_Dump(_h2py(h));
1616
}
1717

18+
_HPy_HIDDEN HPy
19+
ctx_Type(HPyContext *ctx, HPy obj)
20+
{
21+
PyTypeObject *tp = Py_TYPE(_h2py(obj));
22+
Py_INCREF(tp);
23+
return _py2h((PyObject *)tp);
24+
}
25+
1826
/* NOTE: In contrast to CPython, HPy has to check that 'h_type' is a type. This
1927
is not necessary on CPython because it requires C type 'PyTypeObject *' but
2028
here we can only receive an HPy handle. Appropriate checking of the argument
@@ -34,10 +42,14 @@ ctx_Is(HPyContext *ctx, HPy h_obj, HPy h_other)
3442

3543
_HPy_HIDDEN HPy
3644
ctx_GetItem_i(HPyContext *ctx, HPy obj, HPy_ssize_t idx) {
45+
PyObject *py_obj = _h2py(obj);
46+
if (PySequence_Check(py_obj)) {
47+
return _py2h(PySequence_GetItem(py_obj, idx));
48+
}
3749
PyObject* key = PyLong_FromSsize_t(idx);
3850
if (key == NULL)
3951
return HPy_NULL;
40-
HPy result = _py2h(PyObject_GetItem(_h2py(obj), key));
52+
HPy result = _py2h(PyObject_GetItem(py_obj, key));
4153
Py_DECREF(key);
4254
return result;
4355
}

hpy/tools/autogen/conf.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@
121121
'HPyTracker_ForgetAll': None,
122122
'HPyTracker_Close': None,
123123
'_HPy_Dump': None,
124-
'HPy_Type': 'PyObject_Type',
124+
'HPy_Type': None,
125125
'HPy_TypeCheck': None,
126126
'HPy_Is': None,
127127
'HPyBytes_FromStringAndSize': None,
@@ -182,6 +182,7 @@
182182
'PySlice_AdjustIndices': 'HPySlice_AdjustIndices',
183183
'PyType_IsSubtype': 'HPyType_IsSubtype',
184184
'PyObject_Call': 'HPy_CallTupleDict',
185+
'PyObject_Type': 'HPy_Type',
185186
'PyObject_Vectorcall': 'HPy_Call',
186187
'PyObject_VectorcallMethod': 'HPy_CallMethod',
187188
}

hpy/universal/src/autogen_ctx_impl.h

Lines changed: 0 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)