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

[ENH] Improve support for C++ custom exception handlers #5388

Open
da-woods opened this issue Apr 16, 2023 · 1 comment
Open

[ENH] Improve support for C++ custom exception handlers #5388

da-woods opened this issue Apr 16, 2023 · 1 comment
Labels

Comments

@da-woods
Copy link
Contributor

Is your feature request related to a problem? Please describe.

The C++ custom exception handler is almost certainly an advanced feature to help a small number of users, but it doesn't seem like it's particularly easy to use (see discussion in #5334).

One of the obvious issues is that it's hard to extend the default Cython behaviour - you basically need to replace it or duplicate a bunch of Cython utility code yourself.

Describe the solution you'd like.

My proposal was:

  1. make it so that __Pyx_CppExn2PyErr is always generated for users generating custom exception handlers, so that they're able to call it manually themselves if they want.
  2. expose __Pyx_CppExn2PyErr as a callable attribute of the cython module, so it's usable from Cython code too. I'm not sure how useful that's likely to be, but it gives options.

(we'd obviously also want to generate something so that it doesn't give unused code errors).

This at least would make it easier to add to the default Cython behaviour.

Describe alternatives you've considered.

I haven't really considered... to be honest, I don't have a huge understanding of how people are using these custom exception handlers, so suggestions are welcome.

One thing I could imagine being useful (but don't know how/if we'd want to expose it) is the C++11 standard library https://en.cppreference.com/w/cpp/error/make_exception_ptr. Potentially that'd give a way for someone to stash a C++ exception in a cdef class so that it could be converted back to a C++ exception later.

Additional context

No response

@da-woods da-woods added the C++ label Apr 16, 2023
@vyasr
Copy link
Contributor

vyasr commented Apr 19, 2023

  1. I definitely like the idea of that function always being generated so that custom exception handlers can call it. That can only be a helpful addition IMO.
  2. I'm having a bit of trouble wrapping my head around how exactly this would get used since I feel like any practical use case of a custom exception handler would require custom C++ code anyway in order to add new exceptions, but maybe there is a way to make use of this.

I agree, I don't have a great idea of how people are currently using this feature, if at all. The one other case that is familiar to me is something that @pentschev worked on for ucxx, which can be seen here, but that is ultimately very similar to what I'm doing myself. It would certainly be nice to know what others are doing, if anything. @shwina may have more ideas.

When I first started implementing a prototype of this, I don't even think the possibility of introducing a custom handler was in the documentation, and I found it by trawling through the Cython source code, so at least the situation has already improved since then even prior to #5334!

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

No branches or pull requests

2 participants