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

Can we export exceptions? #805

Closed
wants to merge 1 commit into from
Closed

Conversation

kngwyu
Copy link
Member

@kngwyu kngwyu commented Mar 13, 2020

In this PR I added a new macro named export_exceptions, which inserts

#[pymethods]
fn _pyo3_exceptions(_py: pyo3::Python, m: &pyo3::types::PyModule) -> pyo3::PyResult<()> {
    use pyo3::type_object::PyTypeObject;
    m.add("PyBorrowError", pyo3::pycell::PyBorrowError::type_object())?;
    m.add(
        "PyBorrowMutError",
        pyo3::pycell::PyBorrowMutError::type_object(),
    )?;
    Ok(())
}

to exension, so that our custom exceptions can be imported to Python code.

But somehow except PyBorrowError fails oddly like

____________________________________________________________ test_catch_borrrow ____________________________________________________________

    def test_catch_borrrow():
        m = Mutable()
        try:
            m.invalid_borrow(m)
            assert False
        except PyBorrowError:
            pass
        except Exception as e:
>           raise e

tests/test_pycell_exceptions.py:13: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

    def test_catch_borrrow():
        m = Mutable()
        try:
>           m.invalid_borrow(m)
E           _pyo3_exceptions.PyBorrowError

and also isintance(e, PyBorrowError) returns False.

}

#[test]
fn catch_custom_exceptions() {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test succeeds.

@@ -0,0 +1,24 @@
from rustapi_module.cell import Mutable
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests fail.

@kngwyu kngwyu changed the title We can export exceptions? Can we export exceptions? Mar 13, 2020
@birkenfeld
Copy link
Member

Maybe it's easier to define the module in Python (some global code running once when PyO3 initializes), and use the exception class from there like other Python exceptions?

@kngwyu
Copy link
Member Author

kngwyu commented Jul 20, 2020

Closed since it's outdated now, though exceptions should eventually get able to be exported

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