diff --git a/CHANGELOG.md b/CHANGELOG.md index e8e2dd1a..5b21c807 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,7 +27,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - `IntoPy` is no longer implied by `FromPy`. [#1063](https://github.com/PyO3/pyo3/pull/1063) - `PyObject` is now just a type alias for `Py`. [#1063](https://github.com/PyO3/pyo3/pull/1063) - Implement `Send + Sync` for `PyErr`. `PyErr::new`, `PyErr::from_type`, `PyException::py_err` and `PyException::into` have had these bounds added to their arguments. [#1067](https://github.com/PyO3/pyo3/pull/1067) -- Change `#[pyproto]` to return NotImplemented for operators for which Python can try a reversed operation. [1072](https://github.com/PyO3/pyo3/pull/1072) +- Change `#[pyproto]` to return NotImplemented for operators for which Python can try a reversed operation. #[1072](https://github.com/PyO3/pyo3/pull/1072) +- `PyModule::add` now uses `IntoPy` instead of `ToPyObject`. #[1124](https://github.com/PyO3/pyo3/pull/1124) ### Removed - Remove `PyString::as_bytes`. [#1023](https://github.com/PyO3/pyo3/pull/1023) diff --git a/src/types/module.rs b/src/types/module.rs index 1260d802..b345fcab 100644 --- a/src/types/module.rs +++ b/src/types/module.rs @@ -10,7 +10,7 @@ use crate::pyclass::PyClass; use crate::type_object::PyTypeObject; use crate::types::PyTuple; use crate::types::{PyAny, PyDict, PyList}; -use crate::{AsPyPointer, IntoPy, Py, PyObject, Python, ToPyObject}; +use crate::{AsPyPointer, IntoPy, Py, PyObject, Python}; use std::ffi::{CStr, CString}; use std::os::raw::c_char; use std::str; @@ -159,12 +159,12 @@ impl PyModule { /// This is a convenience function which can be used from the module's initialization function. pub fn add(&self, name: &str, value: V) -> PyResult<()> where - V: ToPyObject, + V: IntoPy, { self.index()? .append(name) .expect("could not append __name__ to __all__"); - self.setattr(name, value) + self.setattr(name, value.into_py(self.py())) } /// Adds a new extension type to the module. diff --git a/tests/test_module.rs b/tests/test_module.rs index c4727a69..0746fb8f 100644 --- a/tests/test_module.rs +++ b/tests/test_module.rs @@ -285,3 +285,23 @@ fn test_vararg_module() { py_assert!(py, m, "m.int_vararg_fn() == [5, ()]"); py_assert!(py, m, "m.int_vararg_fn(1, 2) == [1, (2,)]"); } + +#[test] +fn test_module_with_constant() { + // Regression test for #1102 + + #[pymodule] + fn module_with_constant(_py: Python, m: &PyModule) -> PyResult<()> { + const ANON: AnonClass = AnonClass {}; + + m.add("ANON", ANON)?; + m.add_class::()?; + + Ok(()) + } + + Python::with_gil(|py| { + let m = pyo3::wrap_pymodule!(module_with_constant)(py); + py_assert!(py, m, "isinstance(m.ANON, m.AnonClass)"); + }); +}