-
-
Notifications
You must be signed in to change notification settings - Fork 53
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
Equivalent of PyObject_TypeCheck #160
Comments
…emantics is still under discussion, see issue #160
In HPy one can't do something like: #define PyArray_Check(op) PyObject_TypeCheck(op, &PyArray_Type) because |
yes I know, but at some point we need to invent a way to have per-module global variables anyway |
This might be addressed in #156 (or at least it is being discussed there). |
We discussed this issue in February 2023's dev call and decided to go for option 1:
Since Concerning:
@hodgestar is right, one cannot do that in HPy like this. What you can do is:
Which is exactly what we are currently using in our numpy port. This is more expensive since we first need to load the global, do the check, and close the global. |
This is the signature of
PyObject_TypeCheck
:The obvious HPy equivalent would be:
But note that
type
is now a genericHPy
instead of a specificPyTypeObject *
. However, this poses some design issues:type
is actually a type object?true
, with all kinds of funny results.Note that there is also
PyObject_IsInstance
which has the "right" signature but has a much more complex logic.Possible solutions:
type
, similar to how they already need to ensure that you don't passNULL
. We can introduce the extra checks in debug mode, though.HPy_FatalError
insteadHPy_CheckType
?HPy_TryTypeCheck
?HPy_TypeCheckMaybeFail
?I think my preferred solution is (1), especially considered what is the typical usage of the API. I suspect that the in the vast majority of cases it is used to define
PyXXX_Check
for custom types. For example, numpy does the following:So from this POV, the probability of passing something which is not a type is very low, and a fatal error in debug mode could be enough.
Moreover, there is also an additional alternative which is a much bigger redesign, linked to this comment on #83: when you define a custom type, you almost always need helper functions such as
XXX_Check
,XXX_Cast
,XXX_TryCast
, etc.If we go in that direction we can introduce a macro which automatically defines all the
XXX_*
helpers:XXX_Check
will use_HPy_TypeCheck
which can be made semi-private because it will not be needed anywhere else.The text was updated successfully, but these errors were encountered: