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

Enable exact type checks with HPy_TypeCheck #309

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

fangerer
Copy link
Contributor

@fangerer fangerer commented Mar 24, 2022

In CPython, there are usually macros like:

#define PySomething_Check(op) PyObject_TypeCheck(op, &PySomething_Type)
#define PySomething_CheckExact(op) (Py_TYPE(op) == &PySomething_Type)

That translates to following in HPy:

// PySomething_Check
HPy_TypeCheck(ctx, h_obj, h_type)

// PySomething_CheckExact(op)
HPy_Is(ctx, HPy_Type(ctx, h_obj), h_type)

As it turns out, the exact type check is pretty verbose in HPy and having two API calls may also be a high performance drawback.

An alternative approach would be to have a new API for that. Something like HPy_TypeCheckExact (which would come with a new context function as well).

@steve-s
Copy link
Contributor

steve-s commented Mar 28, 2022

Would it make sense to have HPy_TypeCheck and HPy_TypeCheckExact in the API, but keep the variant with the exact flag as a parameter as a context function to avoid adding another context function? From readability POV the third argument in snippets such as HPy_TypeCheck(a, b, 0) or HPy_TypeCheck(a, b, false) is not great, because it is not clear what it means unless you look at HPy_TypeCheck signature.

@fangerer
Copy link
Contributor Author

fangerer commented Mar 28, 2022

Right, let's add inline helpers to improve readability.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants