Skip to content

fix support for custom types #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ documentation = "https://docs.rs/crate/pythonize/"

[dependencies]
serde = { version = "1.0", default-features = false, features = ["std"] }
pyo3 = { version = "0.16.0", default-features = false }
pyo3 = { version = "0.16.3", default-features = false }

[dev-dependencies]
serde = { version = "1.0", default-features = false, features = ["derive"] }
pyo3 = { version = "0.16.0", default-features = false, features = ["auto-initialize", "macros", "pyproto"] }
pyo3 = { version = "0.16.3", default-features = false, features = ["auto-initialize", "macros", "pyproto"] }
serde_json = "1.0"
maplit = "1.0.2"
16 changes: 3 additions & 13 deletions src/de.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use pyo3::types::*;
use pyo3::{ffi, AsPyPointer, PyErr};
use serde::de::{self, IntoDeserializer};
use serde::Deserialize;

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

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

match expected_len {
Some(expected) if expected != len => {
Expand All @@ -51,15 +50,6 @@ macro_rules! deserialize_type {
};
}

fn seq_len(s: &PySequence) -> pyo3::PyResult<usize> {
let v = unsafe { ffi::PySequence_Size(s.as_ptr()) };
if v == -1 {
Err(PyErr::fetch(s.py()))
} else {
Ok(v as usize)
}
}

impl<'a, 'de> de::Deserializer<'de> for &'a mut Depythonizer<'de> {
type Error = PythonizeError;

Expand Down Expand Up @@ -95,8 +85,8 @@ impl<'a, 'de> de::Deserializer<'de> for &'a mut Depythonizer<'de> {
self.deserialize_tuple(obj.len()?, visitor)
} else if obj.is_instance_of::<PyUnicode>()? {
self.deserialize_str(visitor)
} else if let Ok(seq) = obj.downcast::<PySequence>() {
self.deserialize_tuple(seq_len(seq)?, visitor)
} else if let Ok(_) = obj.downcast::<PySequence>() {
self.deserialize_tuple(obj.len()?, visitor)
} else if obj.downcast::<PyMapping>().is_ok() {
self.deserialize_map(visitor)
} else {
Expand Down
17 changes: 5 additions & 12 deletions tests/test_custom_types.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
#![allow(deprecated)]

use std::collections::HashMap;

use pyo3::{
exceptions::{PyIndexError, PyKeyError},
prelude::*,
types::{PyDict, PyList, PyMapping, PySequence},
PyMappingProtocol, PySequenceProtocol,
};
use pythonize::{
depythonize, pythonize_custom, PythonizeDictType, PythonizeListType, PythonizeTypes,
Expand All @@ -18,8 +15,8 @@ struct CustomList {
items: Vec<PyObject>,
}

#[pyproto]
impl PySequenceProtocol for CustomList {
#[pymethods]
impl CustomList {
fn __len__(&self) -> usize {
self.items.len()
}
Expand Down Expand Up @@ -75,13 +72,13 @@ fn test_custom_list() {
})
}

#[pyclass]
#[pyclass(mapping)]
struct CustomDict {
items: HashMap<String, PyObject>,
}

#[pyproto]
impl PyMappingProtocol for CustomDict {
#[pymethods]
impl CustomDict {
fn __len__(&self) -> usize {
self.items.len()
}
Expand All @@ -96,10 +93,7 @@ impl PyMappingProtocol for CustomDict {
fn __setitem__(&mut self, key: String, value: PyObject) {
self.items.insert(key, value);
}
}

#[pymethods]
impl CustomDict {
fn keys(&self) -> Vec<&String> {
self.items.keys().collect()
}
Expand Down Expand Up @@ -129,7 +123,6 @@ impl PythonizeTypes for PythonizeCustomDict {
}

#[test]
#[ignore]
fn test_custom_dict() {
Python::with_gil(|py| {
let serialized =
Expand Down