Skip to content
Closed
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
28 changes: 24 additions & 4 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl<'de> Depythonizer<'de> {
}

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

match expected_len {
Expand Down Expand Up @@ -289,13 +289,13 @@ impl<'a, 'de> de::Deserializer<'de> for &'a mut Depythonizer<'de> {
}

struct PySequenceAccess<'a> {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be appropriate to rename this too, perhaps to PyIteratorAccess.

seq: &'a PySequence,
seq: &'a PyIterator,
index: usize,
len: usize,
}

impl<'a> PySequenceAccess<'a> {
fn new(seq: &'a PySequence, len: usize) -> Self {
fn new(seq: &'a PyIterator, len: usize) -> Self {
Self { seq, index: 0, len }
}
}
Expand All @@ -308,7 +308,11 @@ impl<'de> de::SeqAccess<'de> for PySequenceAccess<'de> {
T: de::DeserializeSeed<'de>,
{
if self.index < self.len {
let mut item_de = Depythonizer::from_object(self.seq.get_item(self.index)?);
let item = match self.seq.next() {
Some(item) => item?,
None => return Ok(None),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This branch shouldn't happen for set or frozenset, since the length is known.

It might not be necessary to have the index checks any more, as long as we still check the length is finite.

};
let mut item_de = Depythonizer::from_object(item);
self.index += 1;
seed.deserialize(&mut item_de).map(Some)
} else {
Expand Down Expand Up @@ -558,6 +562,22 @@ mod test {
test_de(code, &expected, &expected_json);
}

#[test]
fn test_tuple_from_pyset() {
let expected = ("foo".to_string(), 5);
let expected_json = json!(["foo", 5]);
let code = "{'foo', 5}";
test_de(code, &expected, &expected_json);
}

#[test]
fn test_tuple_from_pyfrozenset() {
let expected = ("foo".to_string(), 5);
let expected_json = json!(["foo", 5]);
let code = "frozenset({'foo', 5})";
test_de(code, &expected, &expected_json);
}

#[test]
fn test_vec() {
let expected = vec![3, 2, 1];
Expand Down