From f7950407417052fca355661962092d2fceb3d961 Mon Sep 17 00:00:00 2001 From: kngwyu Date: Mon, 16 Mar 2020 15:34:02 +0900 Subject: [PATCH] Fix PyModule::dict --- src/types/module.rs | 2 +- tests/test_module.rs | 19 ++++++++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/types/module.rs b/src/types/module.rs index 5f27ea036bd..f4e2dc36061 100644 --- a/src/types/module.rs +++ b/src/types/module.rs @@ -72,7 +72,7 @@ impl PyModule { pub fn dict(&self) -> &PyDict { unsafe { self.py() - .from_owned_ptr::(ffi::PyModule_GetDict(self.as_ptr())) + .from_borrowed_ptr::(ffi::PyModule_GetDict(self.as_ptr())) } } diff --git a/tests/test_module.rs b/tests/test_module.rs index 535a74c943e..6539bdf9797 100644 --- a/tests/test_module.rs +++ b/tests/test_module.rs @@ -179,25 +179,34 @@ fn custom_named_fn() -> usize { } #[pymodule] -fn foobar_module(_py: Python, module: &PyModule) -> PyResult<()> { +fn foobar_module(_py: Python, m: &PyModule) -> PyResult<()> { use pyo3::wrap_pyfunction; - module.add_wrapped(wrap_pyfunction!(custom_named_fn)) + m.add_wrapped(wrap_pyfunction!(custom_named_fn))?; + m.dict().set_item("yay", "me")?; + Ok(()) } #[test] fn test_custom_names() { - use pyo3::wrap_pymodule; - let gil = Python::acquire_gil(); let py = gil.python(); - let module = wrap_pymodule!(foobar_module)(py); + let module = pyo3::wrap_pymodule!(foobar_module)(py); py_assert!(py, module, "not hasattr(module, 'custom_named_fn')"); py_assert!(py, module, "module.foobar() == 42"); } +#[test] +fn test_module_dict() { + let gil = Python::acquire_gil(); + let py = gil.python(); + let module = pyo3::wrap_pymodule!(foobar_module)(py); + + py_assert!(py, module, "module.yay == 'me'"); +} + #[pyfunction] fn subfunction() -> String { "Subfunction".to_string()