Retrieve FromPyObject implementation for &PySequence

This commit is contained in:
kngwyu 2020-03-22 19:58:17 +09:00
parent d43a34986d
commit 1e39071c04
3 changed files with 34 additions and 0 deletions

View File

@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
## Unreleased
### Fixed
* `FromPyObject` implementation for `PySequence`. [#827](https://github.com/PyO3/pyo3/pull/827)
## [0.9.0]
### Changed

View File

@ -16,6 +16,7 @@ use crate::{FromPyObject, PyTryFrom, ToBorrowedObject};
#[repr(transparent)]
pub struct PySequence(PyObject, Unsendable);
pyobject_native_type_named!(PySequence);
pyobject_native_type_extract!(PySequence);
impl PySequence {
/// Returns the number of objects in sequence.

View File

@ -499,3 +499,32 @@ fn test_cfg_attrs() {
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]")
}