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

Add exceptions to modules #732

Open
fubuloubu opened this issue Jan 14, 2020 · 9 comments
Open

Add exceptions to modules #732

fubuloubu opened this issue Jan 14, 2020 · 9 comments

Comments

@fubuloubu
Copy link

After creating a new exception using:

create_exception!(my_module, MyException, ExceptionClass);

It is natural that I would want to make it available as an import within my python module:

from my_module import MyException

However, in order to do this, I had to do some deep Kung Foo action:

#[pymodule]
fn my_module(py: Python, m: PyModule) {
    m.add("MyException", py.get_type::<MyException>())?;
}

It would be nice to have an easier API for this:

#[pymodule]
fn my_module(_py: Python, m: PyModule) {
    m.add_exception::<MyException>()?;
}
@davidhewitt
Copy link
Member

davidhewitt commented Jan 15, 2020

Possibly one for the new syntax proposed in #694 . (Seems like use MyException could be possible in that syntax.)

@fubuloubu
Copy link
Author

@davidhewitt you mean something like:

#[pymodule]
mod my_mod {
    #[pyexception]
    enum MyException { ... }

    ...
}

@davidhewitt
Copy link
Member

Quite possibly, yes. Or also something along the lines of

#[pyexception]
enum MyException {  }

#[pymodule]
mod my_mod {
    use super::MyException;
}

@fubuloubu
Copy link
Author

Oh! I really like that!

@hwchen
Copy link

hwchen commented Jul 30, 2020

Just wanted to mention that I needed this exact use case, and it would have been more straightforward for me if m.add() was documented right in the Exceptions page. If you also feel it would be an improvement, I can make a PR.

@davidhewitt
Copy link
Member

@hwchen thanks for the offer but I'm about to rework create_exception to allow adding custom fields (similar to #[pyclass]) , so I think your effort might be overwritten almost immediately. I agree we should improve this for 0.12 though.

@davidhewitt davidhewitt added this to the 0.12 milestone Jul 30, 2020
@davidhewitt
Copy link
Member

(In the end @birkenfeld did end up submitting this exact PR... many thanks! Also, I've suggested we push the #[pyexception] macro back to 0.13, so I'm going to do the same here.)

@davidhewitt davidhewitt modified the milestones: 0.12, 0.13 Sep 1, 2020
@davidhewitt davidhewitt modified the milestones: 0.13, 0.14 Dec 22, 2020
@davidhewitt davidhewitt modified the milestones: 0.14, 0.15 Apr 13, 2021
@davidhewitt davidhewitt modified the milestones: 0.15, 0.16 Sep 18, 2021
@jnicholls
Copy link

jnicholls commented Sep 25, 2021

Noticing this continues to get pushed to further releases. Let us know if help is desired, we can follow the design you have envisioned. And officially, <bump/>

@davidhewitt
Copy link
Member

@jnicholls thanks for the interest, help is very welcome.

The design which I prefer at the moment is to make #[pyclass] and PyModule::add_class the correct way to make custom exception classes and add them to modules rather than having a different API.

This is already possible today, but it's not the most user-friendly or well documented. For the remaining work, see #295 (comment)

If you're interested in coming along with ideas and implementation, I'd be really happy to see this complete! I've been busy working on other pieces of pyo3 😅

@davidhewitt davidhewitt modified the milestones: 0.16, 0.17 Feb 26, 2022
@davidhewitt davidhewitt modified the milestones: 0.17, 0.18 Jul 3, 2022
@davidhewitt davidhewitt modified the milestones: 0.18, 0.19 Dec 27, 2022
@davidhewitt davidhewitt removed this from the 0.19 milestone Jun 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants