Use IntoPy<PyObject> for PyModule::add
This commit is contained in:
parent
bd12d89a3e
commit
82cb815afa
|
@ -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<PyAny>`. [#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<PyObject>` instead of `ToPyObject`. #[1124](https://github.com/PyO3/pyo3/pull/1124)
|
||||
|
||||
### Removed
|
||||
- Remove `PyString::as_bytes`. [#1023](https://github.com/PyO3/pyo3/pull/1023)
|
||||
|
|
|
@ -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<V>(&self, name: &str, value: V) -> PyResult<()>
|
||||
where
|
||||
V: ToPyObject,
|
||||
V: IntoPy<PyObject>,
|
||||
{
|
||||
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.
|
||||
|
|
|
@ -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::<AnonClass>()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Python::with_gil(|py| {
|
||||
let m = pyo3::wrap_pymodule!(module_with_constant)(py);
|
||||
py_assert!(py, m, "isinstance(m.ANON, m.AnonClass)");
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue