Skip to content

Commit

Permalink
Merge pull request #827 from kngwyu/retrieve-frompyobj-for-seq
Browse files Browse the repository at this point in the history
Retrieve FromPyObject implementation for &PySequence
  • Loading branch information
kngwyu authored Mar 22, 2020
2 parents d43a349 + 1e39071 commit 00a277c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/types/sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
29 changes: 29 additions & 0 deletions tests/test_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]")
}

0 comments on commit 00a277c

Please sign in to comment.