Skip to content

Commit

Permalink
Fix PyModule::dict
Browse files Browse the repository at this point in the history
  • Loading branch information
kngwyu committed Mar 16, 2020
1 parent 3f030d4 commit f795040
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/types/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl PyModule {
pub fn dict(&self) -> &PyDict {
unsafe {
self.py()
.from_owned_ptr::<PyDict>(ffi::PyModule_GetDict(self.as_ptr()))
.from_borrowed_ptr::<PyDict>(ffi::PyModule_GetDict(self.as_ptr()))
}
}

Expand Down
19 changes: 14 additions & 5 deletions tests/test_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit f795040

Please sign in to comment.