Skip to content
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
9 changes: 7 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ jobs:
strategy:
fail-fast: false # If one platform fails, allow the rest to keep testing.
matrix:
python-architecture: ["x64"]
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
os: [
"macos-latest",
"macos-13",
"ubuntu-latest",
"windows-latest",
]
Expand All @@ -47,6 +48,10 @@ jobs:
- python-version: "3.12"
os: "ubuntu-latest"
rust: "1.56"
- python-version: "3.12"
python-architecture: "arm64"
os: "macos-latest"
rust: "stable"

steps:
- uses: actions/checkout@v4
Expand All @@ -55,7 +60,7 @@ jobs:
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
architecture: x64
architecture: ${{ matrix.python-python-architecture }}

- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@master
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Unreleased

- Support `u128` / `i128` integers.
- Fix overflow error attempting to depythonize `u64` values greater than `i64::MAX` to types like `serde_json::Value`
- `depythonize()` now take a `&Bound` and is no longer depreciate
- `depythonize_object()` replace the old `depythonize()` and is depreciated
- `depythonize_bound()` is depreciated
Expand Down
72 changes: 69 additions & 3 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,38 @@ impl<'py, 'bound> Depythonizer<'py, 'bound> {
fn dict_access(&self) -> Result<PyMappingAccess<'py>> {
PyMappingAccess::new(self.input.downcast()?)
}

fn deserialize_any_int<'de, V>(&self, int: &Bound<'_, PyInt>, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
if let Ok(x) = int.extract::<u128>() {
if let Ok(x) = u8::try_from(x) {
visitor.visit_u8(x)
} else if let Ok(x) = u16::try_from(x) {
visitor.visit_u16(x)
} else if let Ok(x) = u32::try_from(x) {
visitor.visit_u32(x)
} else if let Ok(x) = u64::try_from(x) {
visitor.visit_u64(x)
} else {
visitor.visit_u128(x)
}
} else {
let x: i128 = int.extract()?;
if let Ok(x) = i8::try_from(x) {
visitor.visit_i8(x)
} else if let Ok(x) = i16::try_from(x) {
visitor.visit_i16(x)
} else if let Ok(x) = i32::try_from(x) {
visitor.visit_i32(x)
} else if let Ok(x) = i64::try_from(x) {
visitor.visit_i64(x)
} else {
visitor.visit_i128(x)
}
}
}
}

macro_rules! deserialize_type {
Expand Down Expand Up @@ -99,8 +131,8 @@ impl<'a, 'py, 'de, 'bound> de::Deserializer<'de> for &'a mut Depythonizer<'py, '
self.deserialize_unit(visitor)
} else if obj.is_instance_of::<PyBool>() {
self.deserialize_bool(visitor)
} else if obj.is_instance_of::<PyInt>() {
self.deserialize_i64(visitor)
} else if let Ok(x) = obj.downcast::<PyInt>() {
self.deserialize_any_int(x, visitor)
} else if obj.is_instance_of::<PyList>() || obj.is_instance_of::<PyTuple>() {
self.deserialize_tuple(obj.len()?, visitor)
} else if obj.is_instance_of::<PyDict>() {
Expand Down Expand Up @@ -151,10 +183,12 @@ impl<'a, 'py, 'de, 'bound> de::Deserializer<'de> for &'a mut Depythonizer<'py, '
deserialize_type!(deserialize_i16 => visit_i16);
deserialize_type!(deserialize_i32 => visit_i32);
deserialize_type!(deserialize_i64 => visit_i64);
deserialize_type!(deserialize_i128 => visit_i128);
deserialize_type!(deserialize_u8 => visit_u8);
deserialize_type!(deserialize_u16 => visit_u16);
deserialize_type!(deserialize_u32 => visit_u32);
deserialize_type!(deserialize_u64 => visit_u64);
deserialize_type!(deserialize_u128 => visit_u128);
deserialize_type!(deserialize_f32 => visit_f32);
deserialize_type!(deserialize_f64 => visit_f64);

Expand Down Expand Up @@ -459,7 +493,7 @@ mod test {
use super::*;
use crate::error::ErrorImpl;
use maplit::hashmap;
use pyo3::Python;
use pyo3::{IntoPy, Python};
use serde_json::{json, Value as JsonValue};

fn test_de<T>(code: &str, expected: &T, expected_json: &JsonValue)
Expand Down Expand Up @@ -749,4 +783,36 @@ mod test {
let code = "{'name': 'SomeFoo', 'bar': {'value': 13, 'variant': {'Tuple': [-1.5, 8]}}}";
test_de(code, &expected, &expected_json);
}

#[test]
fn test_int_limits() {
Python::with_gil(|py| {
// serde_json::Value supports u64 and i64 as maxiumum sizes
let _: serde_json::Value = depythonize(&u8::MAX.into_py(py).into_bound(py)).unwrap();
let _: serde_json::Value = depythonize(&u8::MIN.into_py(py).into_bound(py)).unwrap();
let _: serde_json::Value = depythonize(&i8::MAX.into_py(py).into_bound(py)).unwrap();
let _: serde_json::Value = depythonize(&i8::MIN.into_py(py).into_bound(py)).unwrap();

let _: serde_json::Value = depythonize(&u16::MAX.into_py(py).into_bound(py)).unwrap();
let _: serde_json::Value = depythonize(&u16::MIN.into_py(py).into_bound(py)).unwrap();
let _: serde_json::Value = depythonize(&i16::MAX.into_py(py).into_bound(py)).unwrap();
let _: serde_json::Value = depythonize(&i16::MIN.into_py(py).into_bound(py)).unwrap();

let _: serde_json::Value = depythonize(&u32::MAX.into_py(py).into_bound(py)).unwrap();
let _: serde_json::Value = depythonize(&u32::MIN.into_py(py).into_bound(py)).unwrap();
let _: serde_json::Value = depythonize(&i32::MAX.into_py(py).into_bound(py)).unwrap();
let _: serde_json::Value = depythonize(&i32::MIN.into_py(py).into_bound(py)).unwrap();

let _: serde_json::Value = depythonize(&u64::MAX.into_py(py).into_bound(py)).unwrap();
let _: serde_json::Value = depythonize(&u64::MIN.into_py(py).into_bound(py)).unwrap();
let _: serde_json::Value = depythonize(&i64::MAX.into_py(py).into_bound(py)).unwrap();
let _: serde_json::Value = depythonize(&i64::MIN.into_py(py).into_bound(py)).unwrap();

let _: u128 = depythonize(&u128::MAX.into_py(py).into_bound(py)).unwrap();
let _: i128 = depythonize(&u128::MIN.into_py(py).into_bound(py)).unwrap();

let _: i128 = depythonize(&i128::MAX.into_py(py).into_bound(py)).unwrap();
let _: i128 = depythonize(&i128::MIN.into_py(py).into_bound(py)).unwrap();
});
}
}