Skip to content

Commit cc7e785

Browse files
authored
deps: upgrade PyO3 to 0.21 (#275)
1 parent 14827d9 commit cc7e785

File tree

9 files changed

+96
-68
lines changed

9 files changed

+96
-68
lines changed

.github/workflows/ci.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,20 @@ jobs:
4242
run: cargo fmt --check
4343

4444
Test:
45+
continue-on-error: ${{ matrix.python-version == '3.13' }}
46+
env:
47+
UNSAFE_PYO3_SKIP_VERSION_CHECK: ${{ matrix.unsafe-pyo3-skip-version-check }}
4548
strategy:
4649
matrix:
4750
os: [ubuntu-latest, macos-latest, windows-latest]
4851
python-version: ["3.12"]
4952
allow-prereleases: [false]
53+
unsafe-pyo3-skip-version-check: [0]
5054
include:
5155
- os: ubuntu-latest
5256
python-version: "3.13"
5357
allow-prereleases: true
58+
unsafe-pyo3-skip-version-check: 1
5459
- os: ubuntu-latest
5560
python-version: "3.11"
5661
allow-prereleases: false

Cargo.lock

Lines changed: 20 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,18 @@ name = "tantivy"
1111
crate-type = ["cdylib"]
1212

1313
[build-dependencies]
14-
pyo3-build-config = "0.20.0"
14+
pyo3-build-config = "0.21.0"
1515

1616
[dependencies]
1717
base64 = "0.22"
1818
chrono = "0.4.38"
1919
tantivy = "0.22.0"
2020
itertools = "0.12.1"
2121
futures = "0.3.30"
22-
pythonize = "0.20.0"
22+
pythonize = "0.21.0"
2323
serde = "1.0"
2424
serde_json = "1.0.116"
2525

2626
[dependencies.pyo3]
27-
version = "0.20.0"
27+
version = "0.21.0"
2828
features = ["chrono", "extension-module"]

src/document.rs

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use std::{
2727
str::FromStr,
2828
};
2929

30-
pub(crate) fn extract_value(any: &PyAny) -> PyResult<Value> {
30+
pub(crate) fn extract_value(any: &Bound<PyAny>) -> PyResult<Value> {
3131
if let Ok(s) = any.extract::<String>() {
3232
return Ok(Value::Str(s));
3333
}
@@ -42,7 +42,7 @@ pub(crate) fn extract_value(any: &PyAny) -> PyResult<Value> {
4242
}
4343
if let Ok(datetime) = any.extract::<NaiveDateTime>() {
4444
return Ok(Value::Date(tv::DateTime::from_timestamp_secs(
45-
datetime.timestamp(),
45+
datetime.and_utc().timestamp(),
4646
)));
4747
}
4848
if let Ok(facet) = any.extract::<Facet>() {
@@ -52,23 +52,24 @@ pub(crate) fn extract_value(any: &PyAny) -> PyResult<Value> {
5252
return Ok(Value::Bytes(b));
5353
}
5454
if let Ok(dict) = any.downcast::<PyDict>() {
55-
if let Ok(json) = pythonize::depythonize(dict) {
55+
if let Ok(json) = pythonize::depythonize_bound(dict.clone().into_any())
56+
{
5657
return Ok(Value::Object(json));
5758
}
5859
}
5960
Err(to_pyerr(format!("Value unsupported {any:?}")))
6061
}
6162

6263
pub(crate) fn extract_value_for_type(
63-
any: &PyAny,
64+
any: &Bound<PyAny>,
6465
tv_type: tv::schema::Type,
6566
field_name: &str,
6667
) -> PyResult<Value> {
6768
// Helper function to create `PyErr`s returned by this function.
6869
fn to_pyerr_for_type<'a, E: std::error::Error>(
6970
type_name: &'a str,
7071
field_name: &'a str,
71-
any: &'a PyAny,
72+
any: &'a Bound<PyAny>,
7273
) -> impl Fn(E) -> PyErr + 'a {
7374
move |_| {
7475
to_pyerr(format!(
@@ -104,7 +105,9 @@ pub(crate) fn extract_value_for_type(
104105
.extract::<NaiveDateTime>()
105106
.map_err(to_pyerr_for_type("DateTime", field_name, any))?;
106107

107-
Value::Date(tv::DateTime::from_timestamp_secs(datetime.timestamp()))
108+
Value::Date(tv::DateTime::from_timestamp_secs(
109+
datetime.and_utc().timestamp(),
110+
))
108111
}
109112
tv::schema::Type::Facet => Value::Facet(
110113
any.extract::<Facet>()
@@ -124,9 +127,11 @@ pub(crate) fn extract_value_for_type(
124127

125128
Value::Object(
126129
any.downcast::<PyDict>()
127-
.map(|dict| pythonize::depythonize(dict))
128-
.map_err(to_pyerr_for_type("Json", field_name, any))?
129-
.map_err(to_pyerr_for_type("Json", field_name, any))?,
130+
.map_err(to_pyerr_for_type("Json", field_name, any))
131+
.and_then(|dict| {
132+
pythonize::depythonize_bound(dict.clone().into_any())
133+
.map_err(to_pyerr_for_type("Json", field_name, any))
134+
})?,
130135
)
131136
}
132137
tv::schema::Type::IpAddr => {
@@ -147,16 +152,16 @@ pub(crate) fn extract_value_for_type(
147152
Ok(value)
148153
}
149154

150-
fn extract_value_single_or_list(any: &PyAny) -> PyResult<Vec<Value>> {
155+
fn extract_value_single_or_list(any: &Bound<PyAny>) -> PyResult<Vec<Value>> {
151156
if let Ok(values) = any.downcast::<PyList>() {
152-
values.iter().map(extract_value).collect()
157+
values.iter().map(|v| extract_value(&v)).collect()
153158
} else {
154159
Ok(vec![extract_value(any)?])
155160
}
156161
}
157162

158163
fn extract_value_single_or_list_for_type(
159-
any: &PyAny,
164+
any: &Bound<PyAny>,
160165
field_type: &tv::schema::FieldType,
161166
field_name: &str,
162167
) -> PyResult<Vec<Value>> {
@@ -179,7 +184,11 @@ fn extract_value_single_or_list_for_type(
179184
values
180185
.iter()
181186
.map(|any| {
182-
extract_value_for_type(any, field_type.value_type(), field_name)
187+
extract_value_for_type(
188+
&any,
189+
field_type.value_type(),
190+
field_name,
191+
)
183192
})
184193
.collect()
185194
} else {
@@ -195,7 +204,7 @@ fn object_to_py(
195204
py: Python,
196205
obj: &BTreeMap<String, Value>,
197206
) -> PyResult<PyObject> {
198-
let dict = PyDict::new(py);
207+
let dict = PyDict::new_bound(py);
199208
for (k, v) in obj.iter() {
200209
dict.set_item(k, value_to_py(py, v)?)?;
201210
}
@@ -216,7 +225,7 @@ fn value_to_py(py: Python, value: &Value) -> PyResult<PyObject> {
216225
}
217226
Value::Date(d) => {
218227
let utc = d.into_utc();
219-
PyDateTime::new(
228+
PyDateTime::new_bound(
220229
py,
221230
utc.year(),
222231
utc.month() as u8,
@@ -538,7 +547,7 @@ impl Document {
538547
/// [`extend()`], or `add_<type>()` functions.
539548
#[new]
540549
#[pyo3(signature = (**kwargs))]
541-
fn new(kwargs: Option<&PyDict>) -> PyResult<Self> {
550+
fn new(kwargs: Option<&Bound<PyDict>>) -> PyResult<Self> {
542551
let mut document = Document::default();
543552
if let Some(field_dict) = kwargs {
544553
document.extend(field_dict, None)?;
@@ -548,7 +557,7 @@ impl Document {
548557

549558
fn extend(
550559
&mut self,
551-
py_dict: &PyDict,
560+
py_dict: &Bound<PyDict>,
552561
schema: Option<&Schema>,
553562
) -> PyResult<()> {
554563
Document::extract_py_values_from_dict(
@@ -560,7 +569,7 @@ impl Document {
560569

561570
#[staticmethod]
562571
fn from_dict(
563-
py_dict: &PyDict,
572+
py_dict: &Bound<PyDict>,
564573
schema: Option<&Schema>,
565574
) -> PyResult<Document> {
566575
let mut field_values: BTreeMap<String, Vec<Value>> = BTreeMap::new();
@@ -581,7 +590,7 @@ impl Document {
581590
/// For this reason, the dictionary, will associate
582591
/// a list of value for every field.
583592
fn to_dict(&self, py: Python) -> PyResult<PyObject> {
584-
let dict = PyDict::new(py);
593+
let dict = PyDict::new_bound(py);
585594
for (key, values) in &self.field_values {
586595
let values_py: Vec<PyObject> = values
587596
.iter()
@@ -642,7 +651,7 @@ impl Document {
642651
/// Args:
643652
/// field_name (str): The field name for which we are adding the date.
644653
/// value (datetime): The date that will be added to the document.
645-
fn add_date(&mut self, field_name: String, value: &PyDateTime) {
654+
fn add_date(&mut self, field_name: String, value: &Bound<PyDateTime>) {
646655
let datetime = Utc
647656
.with_ymd_and_hms(
648657
value.get_year(),
@@ -685,15 +694,21 @@ impl Document {
685694
/// to the document.
686695
///
687696
/// Raises a ValueError if the JSON is invalid.
688-
fn add_json(&mut self, field_name: String, value: &PyAny) -> PyResult<()> {
697+
fn add_json(
698+
&mut self,
699+
field_name: String,
700+
value: &Bound<PyAny>,
701+
) -> PyResult<()> {
689702
type JsonMap = serde_json::Map<String, serde_json::Value>;
690703

691704
if let Ok(json_str) = value.extract::<&str>() {
692705
let json_map: JsonMap =
693706
serde_json::from_str(json_str).map_err(to_pyerr)?;
694707
self.add_value(field_name, json_map);
695708
Ok(())
696-
} else if let Ok(json_map) = pythonize::depythonize::<JsonMap>(value) {
709+
} else if let Ok(json_map) =
710+
pythonize::depythonize_bound::<JsonMap>(value.clone())
711+
{
697712
self.add_value(field_name, json_map);
698713
Ok(())
699714
} else {
@@ -760,7 +775,7 @@ impl Document {
760775
self.clone()
761776
}
762777

763-
fn __deepcopy__(&self, _memo: &PyDict) -> Self {
778+
fn __deepcopy__(&self, _memo: &Bound<PyDict>) -> Self {
764779
self.clone()
765780
}
766781

@@ -778,21 +793,21 @@ impl Document {
778793
}
779794

780795
#[staticmethod]
781-
fn _internal_from_pythonized(serialized: &PyAny) -> PyResult<Self> {
782-
pythonize::depythonize(serialized).map_err(to_pyerr)
796+
fn _internal_from_pythonized(serialized: &Bound<PyAny>) -> PyResult<Self> {
797+
pythonize::depythonize_bound(serialized.clone()).map_err(to_pyerr)
783798
}
784799

785800
fn __reduce__<'a>(
786801
slf: PyRef<'a, Self>,
787802
py: Python<'a>,
788-
) -> PyResult<&'a PyTuple> {
803+
) -> PyResult<Bound<'a, PyTuple>> {
789804
let serialized = pythonize::pythonize(py, &*slf).map_err(to_pyerr)?;
790805

791-
Ok(PyTuple::new(
806+
Ok(PyTuple::new_bound(
792807
py,
793808
[
794809
slf.into_py(py).getattr(py, "_internal_from_pythonized")?,
795-
PyTuple::new(py, [serialized]).to_object(py),
810+
PyTuple::new_bound(py, [serialized]).to_object(py),
796811
],
797812
))
798813
}
@@ -810,7 +825,7 @@ impl Document {
810825
}
811826

812827
fn extract_py_values_from_dict(
813-
py_dict: &PyDict,
828+
py_dict: &Bound<PyDict>,
814829
schema: Option<&Schema>,
815830
out_field_values: &mut BTreeMap<String, Vec<Value>>,
816831
) -> PyResult<()> {
@@ -847,12 +862,12 @@ impl Document {
847862

848863
let value_list = if let Some(field_type) = field_type {
849864
extract_value_single_or_list_for_type(
850-
key_value.get_item(1)?,
865+
&key_value.get_item(1)?,
851866
field_type,
852867
key.as_str(),
853868
)?
854869
} else {
855-
extract_value_single_or_list(key_value.get_item(1)?)?
870+
extract_value_single_or_list(&key_value.get_item(1)?)?
856871
};
857872

858873
out_field_values.insert(key, value_list);

0 commit comments

Comments
 (0)