-
Notifications
You must be signed in to change notification settings - Fork 784
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
Segfault when adding a module to itself #1149
Comments
Importing the module causes recursion because |
Interesting find. If we wanted to fix this, I guess we could add a check for recursion in the module initialisation? OTOH, this is a classic infinite recursion caused by malformed user code, there's probably endless ways users can write these and I don't believe any of them are unsound? |
This could be fixed by changing
fn submodule(module: &PyModule) -> PyResult<()> {
module.add("foo", "bar")
}
#[pymodule]
fn submodule_can_be_instantiated(_py: Python, module: &PyModule) -> PyResult<()> {
module.add("foo", "bar")
}
#[pymodule]
fn module_with_functions(_py: Python<'_>, _mod: &PyModule) -> PyResult<()> {
let submod = PyModule::new(_py, "submod")?;
submodule(submod)?;
_mod.add_submodule(&|py| submod.into_py(py))?;
let submod = PyModule::new(_py, "another_module")?;
submodule_can_be_instantiated(_py,submod)?;
_mod.add_submodule(&|py| submod.into_py(py))?;
Ok(())
} Maybe we can take it a step further and provide |
Maybe as an alternative, we could work on the |
I guess the downside of Also, it creates more work for users to migrate from But I don't feel too strongly. I'm just not sure whether it's worth fixing this edge case right now when maybe we can just keep it in mind for an upcoming (bigger) redesign? |
Although it changes existing patterns, I think dumping the I think it's pretty straight forward to say, if you want a submodule in your module:
Done! It's clear where the module is created, it's straight forward what you're adding to it and it enables adding already existing modules, thus making the API more flexible. E.g. imagine this fn: #[pyfunction(pass_module)]
fn add_some_module_to_slf(slf: &PyModule, other: &PyModule) {
slf.add_submodule(other)
} Of course it's also possible to go through
I was hoping to get a stronger typed API, it's possible to add any The same holds for edit: I'm pushing for this now since we're now introducing these new APIs. We intentionally left in |
👍 ok, you've convinced me 😅 One final question: should |
Maybe while what I propose is nice, it means submodules can't be imported directly? Not sure if that's an issue. |
From my tests they all behave the same, but I haven't implemented the changes to |
Is this now to be closed with #1143 merged? |
Yup, should be done! |
Problem is not fixed through #1143 either
The text was updated successfully, but these errors were encountered: