Skip to content

Commit ba1d0ec

Browse files
authored
Merge pull request #27 from davidhewitt/pyclass-mapping
fix support for custom types
2 parents 62593ec + d1e629d commit ba1d0ec

File tree

3 files changed

+10
-27
lines changed

3 files changed

+10
-27
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ documentation = "https://docs.rs/crate/pythonize/"
1212

1313
[dependencies]
1414
serde = { version = "1.0", default-features = false, features = ["std"] }
15-
pyo3 = { version = "0.16.0", default-features = false }
15+
pyo3 = { version = "0.16.3", default-features = false }
1616

1717
[dev-dependencies]
1818
serde = { version = "1.0", default-features = false, features = ["derive"] }
19-
pyo3 = { version = "0.16.0", default-features = false, features = ["auto-initialize", "macros", "pyproto"] }
19+
pyo3 = { version = "0.16.3", default-features = false, features = ["auto-initialize", "macros", "pyproto"] }
2020
serde_json = "1.0"
2121
maplit = "1.0.2"

src/de.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use pyo3::types::*;
2-
use pyo3::{ffi, AsPyPointer, PyErr};
32
use serde::de::{self, IntoDeserializer};
43
use serde::Deserialize;
54

@@ -25,7 +24,7 @@ impl<'de> Depythonizer<'de> {
2524

2625
fn sequence_access(&self, expected_len: Option<usize>) -> Result<PySequenceAccess<'de>> {
2726
let seq: &PySequence = self.input.downcast()?;
28-
let len = seq.len()?;
27+
let len = self.input.len()?;
2928

3029
match expected_len {
3130
Some(expected) if expected != len => {
@@ -51,15 +50,6 @@ macro_rules! deserialize_type {
5150
};
5251
}
5352

54-
fn seq_len(s: &PySequence) -> pyo3::PyResult<usize> {
55-
let v = unsafe { ffi::PySequence_Size(s.as_ptr()) };
56-
if v == -1 {
57-
Err(PyErr::fetch(s.py()))
58-
} else {
59-
Ok(v as usize)
60-
}
61-
}
62-
6353
impl<'a, 'de> de::Deserializer<'de> for &'a mut Depythonizer<'de> {
6454
type Error = PythonizeError;
6555

@@ -95,8 +85,8 @@ impl<'a, 'de> de::Deserializer<'de> for &'a mut Depythonizer<'de> {
9585
self.deserialize_tuple(obj.len()?, visitor)
9686
} else if obj.is_instance_of::<PyUnicode>()? {
9787
self.deserialize_str(visitor)
98-
} else if let Ok(seq) = obj.downcast::<PySequence>() {
99-
self.deserialize_tuple(seq_len(seq)?, visitor)
88+
} else if let Ok(_) = obj.downcast::<PySequence>() {
89+
self.deserialize_tuple(obj.len()?, visitor)
10090
} else if obj.downcast::<PyMapping>().is_ok() {
10191
self.deserialize_map(visitor)
10292
} else {

tests/test_custom_types.rs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
#![allow(deprecated)]
2-
31
use std::collections::HashMap;
42

53
use pyo3::{
64
exceptions::{PyIndexError, PyKeyError},
75
prelude::*,
86
types::{PyDict, PyList, PyMapping, PySequence},
9-
PyMappingProtocol, PySequenceProtocol,
107
};
118
use pythonize::{
129
depythonize, pythonize_custom, PythonizeDictType, PythonizeListType, PythonizeTypes,
@@ -18,8 +15,8 @@ struct CustomList {
1815
items: Vec<PyObject>,
1916
}
2017

21-
#[pyproto]
22-
impl PySequenceProtocol for CustomList {
18+
#[pymethods]
19+
impl CustomList {
2320
fn __len__(&self) -> usize {
2421
self.items.len()
2522
}
@@ -75,13 +72,13 @@ fn test_custom_list() {
7572
})
7673
}
7774

78-
#[pyclass]
75+
#[pyclass(mapping)]
7976
struct CustomDict {
8077
items: HashMap<String, PyObject>,
8178
}
8279

83-
#[pyproto]
84-
impl PyMappingProtocol for CustomDict {
80+
#[pymethods]
81+
impl CustomDict {
8582
fn __len__(&self) -> usize {
8683
self.items.len()
8784
}
@@ -96,10 +93,7 @@ impl PyMappingProtocol for CustomDict {
9693
fn __setitem__(&mut self, key: String, value: PyObject) {
9794
self.items.insert(key, value);
9895
}
99-
}
10096

101-
#[pymethods]
102-
impl CustomDict {
10397
fn keys(&self) -> Vec<&String> {
10498
self.items.keys().collect()
10599
}
@@ -129,7 +123,6 @@ impl PythonizeTypes for PythonizeCustomDict {
129123
}
130124

131125
#[test]
132-
#[ignore]
133126
fn test_custom_dict() {
134127
Python::with_gil(|py| {
135128
let serialized =

0 commit comments

Comments
 (0)