Skip to content

Update to PyO3 0.22 #66

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 7 commits into from
Aug 3, 2024
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:
include:
- python-version: "3.12"
os: "ubuntu-latest"
rust: "1.56"
rust: "1.63"
- python-version: "3.12"
python-architecture: "arm64"
os: "macos-latest"
Expand Down
14 changes: 8 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
## Unreleased

- Bump MSRV to 1.63
- Update to PyO3 0.22
- Support `u128` / `i128` integers.
- Remove support for PyO3's `gil-refs` feature
- `pythonize()` now returns `Bound<'py, PyAny>` instead of `Py<PyAny>`
- `depythonize()` now take a `&Bound` and is no longer deprecated
- `depythonize_bound()` is now deprecated
- `Depythonizer` now contains a `&Bound` and so has an extra lifetime `'bound`
- `Depythonizer::from_object()` now takes a `&Bound` and is no longer deprecated
- 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
- `Depythonizer` now need a `&Bound` and so have extra lifetime `'bound`
- `Depythonizer::from_object()` now take a `&Bound` and is no longer depreciate
- `Depythonizer::from_object_bound()` can't be implemented so have been removed

## 0.21.1 - 2024-04-02

Expand Down
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "pythonize"
version = "0.21.1"
authors = ["David Hewitt <1939362+davidhewitt@users.noreply.github.com>"]
edition = "2021"
rust-version = "1.56"
rust-version = "1.63"
license = "MIT"
description = "Serde Serializer & Deserializer from Rust <--> Python, backed by PyO3."
homepage = "https://github.com/davidhewitt/pythonize"
Expand All @@ -13,11 +13,12 @@ documentation = "https://docs.rs/crate/pythonize/"

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

[dev-dependencies]
serde = { version = "1.0", default-features = false, features = ["derive"] }
pyo3 = { version = "0.21.1", default-features = false, features = ["auto-initialize", "macros"] }
pyo3 = { version = "0.22.0", default-features = false, features = ["auto-initialize", "macros", "py-clone"] }
serde_json = "1.0"
serde_bytes = "0.11"
maplit = "1.0.2"
serde_path_to_error = "0.1.15"
25 changes: 3 additions & 22 deletions src/de.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use pyo3::{types::*, Bound, PyNativeType};
use pyo3::{types::*, Bound};
use serde::de::{self, IntoDeserializer};
use serde::Deserialize;

Expand All @@ -14,10 +14,7 @@ where
}

/// Attempt to convert a Python object to an instance of `T`
#[deprecated(
since = "0.21.1",
note = "will be replaced by `depythonize` in a future release"
)]
#[deprecated(since = "0.22.0", note = "use `depythonize` instead")]
pub fn depythonize_bound<'py, T>(obj: Bound<'py, PyAny>) -> Result<T>
where
T: for<'a> Deserialize<'a>,
Expand All @@ -26,30 +23,14 @@ where
T::deserialize(&mut depythonizer)
}

/// Attempt to convert a Python object to an instance of `T`
#[deprecated(
since = "0.21.1",
note = "will be replaced by `depythonize` in a future release"
)]
pub fn depythonize_object<'de, T>(obj: &'de PyAny) -> Result<T>
where
T: Deserialize<'de>,
{
let obj = obj.as_borrowed().to_owned();
let mut depythonizer = Depythonizer::from_object(&obj);
T::deserialize(&mut depythonizer)
}

/// A structure that deserializes Python objects into Rust values
pub struct Depythonizer<'py, 'bound> {
input: &'bound Bound<'py, PyAny>,
}

impl<'py, 'bound> Depythonizer<'py, 'bound> {
/// Create a deserializer from a Python object
pub fn from_object<'input, 'gil>(
input: &'input Bound<'gil, PyAny>,
) -> Depythonizer<'gil, 'input> {
pub fn from_object<'input>(input: &'input Bound<'py, PyAny>) -> Depythonizer<'py, 'input> {
Depythonizer { input }
}

Expand Down
11 changes: 1 addition & 10 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use pyo3::PyErr;
use pyo3::{exceptions::*, DowncastError, DowncastIntoError};
use pyo3::{PyDowncastError, PyErr};
use serde::{de, ser};
use std::error;
use std::fmt::{self, Debug, Display};
Expand Down Expand Up @@ -145,15 +145,6 @@ impl From<PyErr> for PythonizeError {
}
}

/// Handle errors that occur when attempting to use `PyAny::cast_as`
impl<'a> From<PyDowncastError<'a>> for PythonizeError {
fn from(other: PyDowncastError) -> Self {
Self {
inner: Box::new(ErrorImpl::UnexpectedType(other.to_string())),
}
}
}

/// Handle errors that occur when attempting to use `PyAny::cast_as`
impl<'a, 'py> From<DowncastError<'a, 'py>> for PythonizeError {
fn from(other: DowncastError<'a, 'py>) -> Self {
Expand Down
10 changes: 5 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
//! # Examples
//! ```
//! use serde::{Serialize, Deserialize};
//! use pyo3::Python;
//! use pyo3::{types::PyAnyMethods, Python};
//! use pythonize::{depythonize, pythonize};
//!
//! #[derive(Debug, Serialize, Deserialize, PartialEq)]
Expand All @@ -27,10 +27,10 @@
//! // Rust -> Python
//! let obj = pythonize(py, &sample).unwrap();
//!
//! assert_eq!("{'foo': 'Foo', 'bar': None}", &format!("{}", obj.as_ref(py).repr().unwrap()));
//! assert_eq!("{'foo': 'Foo', 'bar': None}", &format!("{}", obj.repr().unwrap()));
//!
//! // Python -> Rust
//! let new_sample: Sample = depythonize(&obj.into_bound(py)).unwrap();
//! let new_sample: Sample = depythonize(&obj).unwrap();
//!
//! assert_eq!(new_sample, sample);
//! });
Expand All @@ -40,9 +40,9 @@ mod de;
mod error;
mod ser;

pub use crate::de::{depythonize, Depythonizer};
#[allow(deprecated)]
pub use crate::de::{depythonize_bound, depythonize_object};
pub use crate::de::depythonize_bound;
pub use crate::de::{depythonize, Depythonizer};
pub use crate::error::{PythonizeError, Result};
pub use crate::ser::{
pythonize, pythonize_custom, PythonizeDefault, PythonizeDictType, PythonizeListType,
Expand Down
Loading