Skip to content
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

upgrade to pyo3 0.21.2 #78

Merged
merged 2 commits into from
Jun 24, 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
20 changes: 10 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"
[dependencies]
toml = {version = "0.5.9", features = ["preserve_order"]}
serde = "1.0.126"
pyo3 = "0.20.0"
pyo3 = "0.21.2"
ahash = "0.8.1"
nohash-hasher = "0.2.0"

Expand Down
86 changes: 39 additions & 47 deletions src/datetime.rs
Original file line number Diff line number Diff line change
@@ -1,52 +1,44 @@
extern crate pyo3;

use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;
use pyo3::types::{PyDate, PyDateTime, PyDelta, PyTime, PyTzInfo};

use toml::value::{Datetime as TomlDatetime, Offset as TomlOffset};

pub fn parse(py: Python, datetime: &TomlDatetime) -> PyResult<PyObject> {
let py_dt: PyObject = match &datetime.date {
Some(date) => match &datetime.time {
Some(t) => {
let py_tz: PyObject;
let tzinfo = match &datetime.offset {
Some(offset) => {
let tz_info = match offset {
TomlOffset::Z => TzInfo::new(0, 0),
TomlOffset::Custom { hours, minutes } => TzInfo::new(*hours, *minutes),
};
py_tz = Py::new(py, tz_info)?.to_object(py);
Some(py_tz.extract(py)?)
}
None => None,
};
match (&datetime.date, &datetime.time) {
(Some(date), Some(t)) => {
let tz_info: Option<Bound<'_, PyTzInfo>> = match &datetime.offset {
Some(offset) => {
let tz_info = match offset {
TomlOffset::Z => TzInfo::new(0, 0),
TomlOffset::Custom { hours, minutes } => TzInfo::new(*hours, *minutes),
};
Some(Bound::new(py, tz_info)?.into_any().downcast_into()?)
}
None => None,
};

PyDateTime::new(
py,
date.year as i32,
date.month,
date.day,
t.hour,
t.minute,
t.second,
t.nanosecond / 1000,
tzinfo,
)?
.to_object(py)
}
None => PyDate::new(py, date.year as i32, date.month, date.day)?.to_object(py),
},
None => match &datetime.time {
Some(t) => PyTime::new(py, t.hour, t.minute, t.second, t.nanosecond / 1000, None)?.to_object(py),
None => {
// AFAIK this can't actually happen
let msg = "either time or date (or both) are required)".to_string();
return Err(PyErr::new::<PyValueError, _>(PyValueError::new_err(msg)));
}
},
};
Ok(py_dt)
PyDateTime::new_bound(
py,
date.year as i32,
date.month,
date.day,
t.hour,
t.minute,
t.second,
t.nanosecond / 1000,
tz_info.as_ref(),
)
.map(|dt| dt.to_object(py))
}
(Some(date), None) => PyDate::new_bound(py, date.year as i32, date.month, date.day).map(|d| d.to_object(py)),
(None, Some(t)) => {
PyTime::new_bound(py, t.hour, t.minute, t.second, t.nanosecond / 1000, None).map(|t| t.to_object(py))
}
(None, None) => {
// AFAIK this can't actually happen
unreachable!("either time or date (or both) are required")
}
}
}

#[pyclass(module = "rtoml._rtoml", extends = PyTzInfo)]
Expand All @@ -66,15 +58,15 @@ impl TzInfo {
(self.hours as i32) * 3600 + (self.minutes as i32) * 60
}

fn utcoffset<'p>(&self, py: Python<'p>, _dt: &PyDateTime) -> PyResult<&'p PyDelta> {
PyDelta::new(py, 0, self.seconds(), 0, true)
fn utcoffset<'py>(&self, dt: &Bound<'py, PyDateTime>) -> PyResult<Bound<'py, PyDelta>> {
PyDelta::new_bound(dt.py(), 0, self.seconds(), 0, true)
}

fn tzname(&self, _dt: &PyDateTime) -> String {
fn tzname(&self, _dt: &Bound<'_, PyAny>) -> String {
self.__str__()
}

fn dst(&self, _dt: &PyDateTime) -> Option<&PyDelta> {
fn dst(&self, _dt: &Bound<'_, PyAny>) -> Option<&PyDelta> {
None
}

Expand Down
4 changes: 2 additions & 2 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl<'de, 'py> Visitor<'de> for PyDeserializer<'py> {
let mut key_set = NoHashSet::<u64>::with_hasher(BuildHasherDefault::default());
key_set.insert(hash_builder.hash_one(&first_key));

let dict = PyDict::new(self.py);
let dict = PyDict::new_bound(self.py);
dict.set_item(first_key, first_value).map_err(de::Error::custom)?;

while let Some((key, value)) =
Expand All @@ -134,7 +134,7 @@ impl<'de, 'py> Visitor<'de> for PyDeserializer<'py> {

Ok(dict.into_py(self.py))
}
None => Ok(PyDict::new(self.py).into_py(self.py)),
None => Ok(PyDict::new_bound(self.py).into_py(self.py)),
}
}
}
12 changes: 6 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;
use pyo3::{create_exception, wrap_pyfunction};
use serde::de::DeserializeSeed;

use serde::de::DeserializeSeed;
use toml::{to_string as to_toml_string, to_string_pretty as to_toml_string_pretty, Deserializer};

use crate::ser::SerializePyObject;
Expand All @@ -24,7 +24,7 @@ fn deserialize(py: Python, toml_data: String, none_value: Option<&str>) -> PyRes
}

#[pyfunction]
fn serialize(py: Python, obj: &PyAny, none_value: Option<&str>) -> PyResult<String> {
fn serialize(py: Python, obj: Bound<'_, PyAny>, none_value: Option<&str>) -> PyResult<String> {
let s = SerializePyObject::new(py, obj, none_value);
match to_toml_string(&s) {
Ok(s) => Ok(s),
Expand All @@ -33,7 +33,7 @@ fn serialize(py: Python, obj: &PyAny, none_value: Option<&str>) -> PyResult<Stri
}

#[pyfunction]
fn serialize_pretty(py: Python, obj: &PyAny, none_value: Option<&str>) -> PyResult<String> {
fn serialize_pretty(py: Python, obj: Bound<'_, PyAny>, none_value: Option<&str>) -> PyResult<String> {
let s = SerializePyObject::new(py, obj, none_value);
match to_toml_string_pretty(&s) {
Ok(s) => Ok(s),
Expand All @@ -52,9 +52,9 @@ pub fn get_version() -> String {
}

#[pymodule]
fn _rtoml(py: Python, m: &PyModule) -> PyResult<()> {
m.add("TomlParsingError", py.get_type::<TomlParsingError>())?;
m.add("TomlSerializationError", py.get_type::<TomlSerializationError>())?;
fn _rtoml(py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add("TomlParsingError", py.get_type_bound::<TomlParsingError>())?;
m.add("TomlSerializationError", py.get_type_bound::<TomlSerializationError>())?;
let version = get_version();
m.add("__version__", version.clone())?;
// keep VERSION for compatibility
Expand Down
35 changes: 18 additions & 17 deletions src/py_type.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use pyo3::once_cell::GILOnceCell;
use pyo3::prelude::*;
use pyo3::types::{PyByteArray, PyBytes, PyDate, PyDateTime, PyDict, PyList, PyString, PyTime, PyTuple};
use pyo3::sync::GILOnceCell;
use pyo3::types::{
PyBool, PyByteArray, PyBytes, PyDate, PyDateTime, PyDict, PyFloat, PyInt, PyList, PyNone, PyString, PyTime, PyTuple,
};
use pyo3::PyTypeInfo;

#[derive(Clone)]
#[cfg_attr(debug_assertions, derive(Debug))]
Expand Down Expand Up @@ -30,26 +33,24 @@ static TYPE_LOOKUP: GILOnceCell<PyTypeLookup> = GILOnceCell::new();
impl PyTypeLookup {
fn new(py: Python) -> Self {
Self {
none: py.None().as_ref(py).get_type_ptr() as usize,
none: PyNone::type_object_raw(py) as usize,
// numeric types
int: 0i32.into_py(py).as_ref(py).get_type_ptr() as usize,
bool: true.into_py(py).as_ref(py).get_type_ptr() as usize,
float: 0f32.into_py(py).as_ref(py).get_type_ptr() as usize,
int: PyInt::type_object_raw(py) as usize,
bool: PyBool::type_object_raw(py) as usize,
float: PyFloat::type_object_raw(py) as usize,
// string types
string: PyString::new(py, "s").get_type_ptr() as usize,
bytes: PyBytes::new(py, b"s").get_type_ptr() as usize,
bytearray: PyByteArray::new(py, b"s").get_type_ptr() as usize,
string: PyString::type_object_raw(py) as usize,
bytes: PyBytes::type_object_raw(py) as usize,
bytearray: PyByteArray::type_object_raw(py) as usize,
// sequence types
list: PyList::empty(py).get_type_ptr() as usize,
tuple: PyTuple::empty(py).get_type_ptr() as usize,
list: PyList::type_object_raw(py) as usize,
tuple: PyTuple::type_object_raw(py) as usize,
// mapping types
dict: PyDict::new(py).get_type_ptr() as usize,
dict: PyDict::type_object_raw(py) as usize,
// datetime types
datetime: PyDateTime::new(py, 2000, 1, 1, 0, 0, 0, 0, None)
.unwrap()
.get_type_ptr() as usize,
date: PyDate::new(py, 2000, 1, 1).unwrap().get_type_ptr() as usize,
time: PyTime::new(py, 0, 0, 0, 0, None).unwrap().get_type_ptr() as usize,
datetime: PyDateTime::type_object_raw(py) as usize,
date: PyDate::type_object_raw(py) as usize,
time: PyTime::type_object_raw(py) as usize,
}
}

Expand Down
Loading
Loading