Skip to content

Commit

Permalink
Merge pull request #809 from kngwyu/fix-module-dict
Browse files Browse the repository at this point in the history
Fix PyModule::dict
  • Loading branch information
kngwyu authored Mar 16, 2020
2 parents e09873b + 4007724 commit d4281a0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
* Usage of `PyObject` with `#[pyo3(get)]`. [#760](https://github.com/PyO3/pyo3/pull/760)
* `#[pymethods]` used in conjunction with `#[cfg]`. #[769](https://github.com/PyO3/pyo3/pull/769)
* `"*"` in a `#[pyfunction()]` argument list incorrectly accepting any number of positional arguments (use `args = "*"` when this behaviour is desired). #[792](https://github.com/PyO3/pyo3/pull/792)
* `PyModule::dict` #[809](https://github.com/PyO3/pyo3/pull/809)

### Removed

Expand Down
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 d4281a0

Please sign in to comment.