Merge pull request #809 from kngwyu/fix-module-dict

Fix PyModule::dict
This commit is contained in:
Yuji Kanagawa 2020-03-16 22:56:13 +09:00 committed by GitHub
commit d4281a0cd3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 6 deletions

View File

@ -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) * 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) * `#[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) * `"*"` 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 ### Removed

View File

@ -72,7 +72,7 @@ impl PyModule {
pub fn dict(&self) -> &PyDict { pub fn dict(&self) -> &PyDict {
unsafe { unsafe {
self.py() self.py()
.from_owned_ptr::<PyDict>(ffi::PyModule_GetDict(self.as_ptr())) .from_borrowed_ptr::<PyDict>(ffi::PyModule_GetDict(self.as_ptr()))
} }
} }

View File

@ -179,25 +179,34 @@ fn custom_named_fn() -> usize {
} }
#[pymodule] #[pymodule]
fn foobar_module(_py: Python, module: &PyModule) -> PyResult<()> { fn foobar_module(_py: Python, m: &PyModule) -> PyResult<()> {
use pyo3::wrap_pyfunction; 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] #[test]
fn test_custom_names() { fn test_custom_names() {
use pyo3::wrap_pymodule;
let gil = Python::acquire_gil(); let gil = Python::acquire_gil();
let py = gil.python(); 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, "not hasattr(module, 'custom_named_fn')");
py_assert!(py, module, "module.foobar() == 42"); 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] #[pyfunction]
fn subfunction() -> String { fn subfunction() -> String {
"Subfunction".to_string() "Subfunction".to_string()