Pyo3: Add a proc macro for exceptions

Created on 1 Dec 2018  路  5Comments  路  Source: PyO3/pyo3

I would be great to have a custom derive for exceptions, similar but more powerful than create_exception. This would e.g. allow reimplementing JSONDecodeError:

#[pyexception(module = json)]
struct JSONDecodeError {
    msg: String,
    doc: String,
    pos: usize,
    lineno: Option<usize>,
    colno: Option<usize>,
}

The steps required are roughly:

  1. Look at examples in syn and pyo3's existing proc macros to learn how to implement a proc macro. wasm-bindgen could be another useful resource.
  2. Implement the macro using the existing impl_exception_boilerplate and create_exception_type_object macros. The #[proc_macro_attribute] function should go in pyo3cls/src/lib.rs, while the actual implementation should be in pyo3-derive-backend.
  3. Write tests. Most important is one for a struct without members and one with at least 2 members.
  4. Add a usage example to the guide. You can just copy one of the tests for that.

If you want to work on implementing this or have any questions, feel free ask here or on gitter!

enhancement needs-implemeter

Most helpful comment

I'm nominating this for 0.12 - I actually already have a PR almost ready for #682 which would lay the groundwork for this.

All 5 comments

I'd like to try my hand at this. Would that be fine?

If so, I'm not super familiar with the details here, so would this issue be a good place to ask questions?

Thanks, please don't hesitate to ask us!

Yes, just ask here!

I'm nominating this for 0.12 - I actually already have a PR almost ready for #682 which would lay the groundwork for this.

Nearly everything else in 0.12 is close to being ready to go, and this is a potentially complex feature but not yet started. As much as I'm looking forward to having this, I'm going to delay this to the 0.13 release so we have time to design and implement properly.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jothan picture jothan  路  3Comments

davidhewitt picture davidhewitt  路  5Comments

fcangialosi picture fcangialosi  路  6Comments

fubuloubu picture fubuloubu  路  7Comments

c410-f3r picture c410-f3r  路  5Comments