Retrieve FromPyObject implementation for &PySequence
This commit is contained in:
parent
d43a34986d
commit
1e39071c04
|
@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
|
||||||
|
|
||||||
## Unreleased
|
## Unreleased
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
* `FromPyObject` implementation for `PySequence`. [#827](https://github.com/PyO3/pyo3/pull/827)
|
||||||
|
|
||||||
## [0.9.0]
|
## [0.9.0]
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
|
|
@ -16,6 +16,7 @@ use crate::{FromPyObject, PyTryFrom, ToBorrowedObject};
|
||||||
#[repr(transparent)]
|
#[repr(transparent)]
|
||||||
pub struct PySequence(PyObject, Unsendable);
|
pub struct PySequence(PyObject, Unsendable);
|
||||||
pyobject_native_type_named!(PySequence);
|
pyobject_native_type_named!(PySequence);
|
||||||
|
pyobject_native_type_extract!(PySequence);
|
||||||
|
|
||||||
impl PySequence {
|
impl PySequence {
|
||||||
/// Returns the number of objects in sequence.
|
/// Returns the number of objects in sequence.
|
||||||
|
|
|
@ -499,3 +499,32 @@ fn test_cfg_attrs() {
|
||||||
|
|
||||||
py_assert!(py, inst, "not hasattr(inst, 'never_compiled_method')");
|
py_assert!(py, inst, "not hasattr(inst, 'never_compiled_method')");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[pyclass]
|
||||||
|
#[derive(Default)]
|
||||||
|
struct FromSequence {
|
||||||
|
#[pyo3(get)]
|
||||||
|
numbers: Vec<i64>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[pymethods]
|
||||||
|
impl FromSequence {
|
||||||
|
#[new]
|
||||||
|
fn new(seq: Option<&pyo3::types::PySequence>) -> PyResult<Self> {
|
||||||
|
if let Some(seq) = seq {
|
||||||
|
Ok(FromSequence {
|
||||||
|
numbers: seq.as_ref().extract::<Vec<_>>()?,
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
Ok(FromSequence::default())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_from_sequence() {
|
||||||
|
let gil = Python::acquire_gil();
|
||||||
|
let py = gil.python();
|
||||||
|
let typeobj = py.get_type::<FromSequence>();
|
||||||
|
py_assert!(py, typeobj, "typeobj(range(0, 4)).numbers == [0, 1, 2, 3]")
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue