Skip to content

Commit 7b34c22

Browse files
authored
Merge pull request #123 from projectsyn/renovate/pyo3-0.x
Update Rust crate pyo3 to v0.22.2
2 parents 4416be1 + a95ace4 commit 7b34c22

File tree

10 files changed

+35
-34
lines changed

10 files changed

+35
-34
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ anyhow = "1.0.82"
2020
chrono = "0.4.38"
2121
indexmap = "2.2.6"
2222
nom = "7.1.3"
23-
pyo3 = { version = "=0.20.3", features = ["chrono"] }
23+
pyo3 = { version = "=0.22.2", features = ["chrono"] }
2424
rayon = "1.10.0"
2525
regex = "1.10.4"
2626
serde = { version = "1.0.200", features = ["derive"] }

src/config.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use std::path::PathBuf;
1111
use crate::fsutil::to_lexical_normal;
1212

1313
/// Flags to change reclass-rs behavior to be compaible with Python reclass
14-
#[pyclass]
14+
#[pyclass(eq, eq_int)]
1515
#[derive(Clone, Debug, Hash, Eq, PartialEq)]
1616
pub enum CompatFlag {
1717
/// This flag enables Python Reclass-compatible rendering of fields `path` and `parts` in
@@ -137,19 +137,18 @@ impl Config {
137137
let vstr = vstr.trim();
138138
match k {
139139
"nodes_uri" => {
140-
let npath = cfg_path
140+
cfg_path
141141
.with_file_name(vstr)
142142
.to_str()
143143
.ok_or(anyhow!("Can't create nodes path from config file"))?
144-
.to_owned();
145-
self.nodes_path = npath;
144+
.clone_into(&mut self.nodes_path);
146145
}
147146
"classes_uri" => {
148-
self.classes_path = cfg_path
147+
cfg_path
149148
.with_file_name(vstr)
150149
.to_str()
151150
.ok_or(anyhow!("Can't create nodes path from config file"))?
152-
.to_owned();
151+
.clone_into(&mut self.classes_path);
153152
}
154153
"ignore_class_notfound" => {
155154
self.ignore_class_notfound = v.as_bool().ok_or(anyhow!(
@@ -281,9 +280,9 @@ impl Config {
281280
#[classmethod]
282281
#[pyo3(signature = (inventory_path, config, verbose=false))]
283282
fn from_dict(
284-
_cls: &PyType,
283+
_cls: &Bound<'_, PyType>,
285284
inventory_path: &str,
286-
config: &PyDict,
285+
config: &Bound<'_, PyDict>,
287286
verbose: bool,
288287
) -> PyResult<Self> {
289288
let mut cfg = Config::new(Some(inventory_path), None, None, None).map_err(|e| {

src/inventory.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,16 @@ impl Inventory {
7777
/// The structure of the returned dict should match Python reclass the structure of the dict
7878
/// returned by Python reclass's `inventory()` method.
7979
fn as_dict(&self, py: Python<'_>) -> PyResult<Py<PyDict>> {
80-
let dict = PyDict::new(py);
80+
let dict = PyDict::new_bound(py);
8181
dict.set_item("applications", self.applications.clone().into_py(py))?;
8282
dict.set_item("classes", self.classes.clone().into_py(py))?;
83-
let nodes_dict = PyDict::new(py);
83+
let nodes_dict = PyDict::new_bound(py);
8484
for (name, info) in &self.nodes {
8585
nodes_dict.set_item(name, info.as_dict(py)?)?;
8686
}
8787
dict.set_item("nodes", nodes_dict)?;
8888

89-
let reclass_dict = PyDict::new(py);
89+
let reclass_dict = PyDict::new_bound(py);
9090
let ts = Local::now();
9191
reclass_dict.set_item("timestamp", ts.format("%c").to_string())?;
9292
dict.set_item("__reclass__", reclass_dict)?;

src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ impl Reclass {
295295
#[classmethod]
296296
#[pyo3(signature = (inventory_path, config_file, verbose=false))]
297297
fn from_config_file(
298-
cls: &PyType,
298+
cls: &Bound<'_, PyType>,
299299
inventory_path: &str,
300300
config_file: &str,
301301
verbose: bool,
@@ -311,7 +311,7 @@ impl Reclass {
311311
///
312312
/// Returns a `Reclass` instance or raises a `ValueError`
313313
#[classmethod]
314-
fn from_config(_cls: &PyType, config: Config) -> PyResult<Self> {
314+
fn from_config(_cls: &Bound<'_, PyType>, config: Config) -> PyResult<Self> {
315315
let r = Self::new_from_config(config).map_err(|e| PyValueError::new_err(format!("{e}")))?;
316316
Ok(r)
317317
}
@@ -339,7 +339,7 @@ impl Reclass {
339339
/// Note that this method should only be called once and will print a diagnostic message if
340340
/// called again.
341341
#[classmethod]
342-
pub fn set_thread_count(_cls: &PyType, count: usize) {
342+
pub fn set_thread_count(_cls: &Bound<'_, PyType>, count: usize) {
343343
if let Err(e) = ThreadPoolBuilder::new().num_threads(count).build_global() {
344344
eprintln!("While initializing global thread pool: {e}");
345345
}
@@ -408,7 +408,7 @@ impl Default for Reclass {
408408
}
409409

410410
#[pymodule]
411-
fn reclass_rs(_py: Python, m: &PyModule) -> PyResult<()> {
411+
fn reclass_rs(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
412412
// Register the top-level `Reclass` Python class which is used to configure the library
413413
m.add_class::<Reclass>()?;
414414
// Register the `Config` class and `CompatFlag` enum

src/list/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ mod unique;
44
/// Defines the shared interface between the unique list (which is effectively an insert-ordered
55
/// Set) and the unique list which supports removals.
66
pub trait List {
7+
#[allow(dead_code)]
78
fn new() -> Self;
89
fn with_capacity(capacity: usize) -> Self;
910
fn len(&self) -> usize;
1011
fn shrink_to_fit(&mut self);
1112
fn append_if_new(&mut self, item: String);
1213
fn merge(&mut self, other: Self);
14+
#[allow(dead_code)]
1315
fn merge_from(&mut self, other: &Self);
1416
}
1517

src/node/nodeinfo.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ impl NodeInfo {
177177
/// This method generates a PyDict which should be structured identically to Python Reclass's
178178
/// `nodeinfo` return value.
179179
pub(crate) fn as_dict(&self, py: Python<'_>) -> PyResult<Py<PyDict>> {
180-
let dict = PyDict::new(py);
180+
let dict = PyDict::new_bound(py);
181181
dict.set_item("__reclass__", self.reclass_as_dict(py)?)?;
182182
dict.set_item("applications", self.applications.clone().into_py(py))?;
183183
dict.set_item("classes", self.classes.clone().into_py(py))?;
@@ -189,7 +189,7 @@ impl NodeInfo {
189189

190190
/// Returns the NodeInfo `meta` field as a PyDict
191191
fn reclass_as_dict(&self, py: Python<'_>) -> PyResult<Py<PyDict>> {
192-
let dict = PyDict::new(py);
192+
let dict = PyDict::new_bound(py);
193193
dict.set_item("node", self.reclass.node.clone().into_py(py))?;
194194
dict.set_item("name", self.reclass.name.clone().into_py(py))?;
195195
dict.set_item("uri", self.reclass.uri.clone().into_py(py))?;

src/types/from.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,18 +120,18 @@ impl<'a, T: Clone + Into<Value>> From<&'a [T]> for Value {
120120
}
121121
}
122122

123-
impl TryFrom<&PyAny> for Value {
123+
impl TryFrom<Bound<'_, PyAny>> for Value {
124124
type Error = PyErr;
125125

126-
fn try_from(value: &PyAny) -> PyResult<Self> {
127-
match value.get_type().name()? {
126+
fn try_from(value: Bound<'_, PyAny>) -> PyResult<Self> {
127+
match value.get_type().name()?.to_str()? {
128128
"str" => {
129129
let v = value.extract::<&str>()?;
130130
Ok(Self::String(v.to_string()))
131131
}
132132
"list" => {
133133
let v = value.downcast::<PySequence>()?;
134-
let mut items = vec![];
134+
let mut items: Vec<Value> = vec![];
135135
for it in v.iter()? {
136136
items.push(TryInto::try_into(it?)?);
137137
}

src/types/mapping.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ impl Mapping {
356356

357357
/// Converts the `Mapping` into a `PyDict`.
358358
pub fn as_py_dict(&self, py: Python<'_>) -> PyResult<Py<PyDict>> {
359-
let dict = PyDict::new(py);
359+
let dict = PyDict::new_bound(py);
360360

361361
for (k, v) in self {
362362
let pyk = k.as_py_obj(py)?;
@@ -741,7 +741,7 @@ mod mapping_tests {
741741
pyo3::prepare_freethreaded_python();
742742
Python::with_gil(|py| {
743743
let pym = m.as_py_dict(py).unwrap();
744-
let m = pym.as_ref(py);
744+
let m = pym.bind(py);
745745
assert_eq!(m.len(), 6);
746746
assert_eq!(format!("{:?}", m.keys()), "['a', 'b', 'c', 'd', 'e', 'f']");
747747
let a = m.get_item(&"a").unwrap().unwrap();

src/types/value/value_as_py_obj_tests.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ fn test_as_py_obj_null() {
44
pyo3::prepare_freethreaded_python();
55
Python::with_gil(|py| {
66
let pyv = Value::Null.as_py_obj(py).unwrap();
7-
let v = pyv.as_ref(py);
7+
let v = pyv.bind(py);
88
assert!(v.is_none());
99
});
1010
}
@@ -14,7 +14,7 @@ fn test_as_py_obj_bool() {
1414
pyo3::prepare_freethreaded_python();
1515
Python::with_gil(|py| {
1616
let pyb = Value::Bool(true).as_py_obj(py).unwrap();
17-
let b = pyb.as_ref(py);
17+
let b = pyb.bind(py);
1818
assert!(b.is_instance_of::<pyo3::types::PyBool>());
1919
assert!(b.downcast_exact::<pyo3::types::PyBool>().unwrap().is_true());
2020
});
@@ -27,7 +27,7 @@ fn test_as_py_obj_int() {
2727
let nums: Vec<Value> = vec![5.into(), (-2i64).into()];
2828
for n in nums {
2929
let pyn = n.as_py_obj(py).unwrap();
30-
let n = pyn.as_ref(py);
30+
let n = pyn.bind(py);
3131
assert!(n.is_instance_of::<pyo3::types::PyInt>());
3232
assert!(n
3333
.downcast_exact::<pyo3::types::PyInt>()
@@ -44,7 +44,7 @@ fn test_as_py_obj_float() {
4444
Python::with_gil(|py| {
4545
let n: Value = 3.14.into();
4646
let pyn = n.as_py_obj(py).unwrap();
47-
let n = pyn.as_ref(py);
47+
let n = pyn.bind(py);
4848
assert!(n.is_instance_of::<pyo3::types::PyFloat>());
4949
assert!(n
5050
.downcast_exact::<pyo3::types::PyFloat>()
@@ -60,12 +60,12 @@ fn test_as_py_obj_sequence() {
6060
Python::with_gil(|py| {
6161
let s: Value = vec![1, 2, 3].into();
6262
let pys = s.as_py_obj(py).unwrap();
63-
let s = pys.as_ref(py);
63+
let s = pys.bind(py);
6464
assert!(s.is_instance_of::<pyo3::types::PyList>());
6565
assert!(s
6666
.downcast_exact::<pyo3::types::PyList>()
6767
.unwrap()
68-
.eq(pyo3::types::PyList::new(py, vec![1, 2, 3]))
68+
.eq(pyo3::types::PyList::new_bound(py, vec![1, 2, 3]))
6969
.unwrap());
7070
});
7171
}
@@ -77,7 +77,7 @@ fn test_as_py_obj_string() {
7777
let pys = std::convert::Into::<Value>::into("hello, world")
7878
.as_py_obj(py)
7979
.unwrap();
80-
let s = pys.as_ref(py);
80+
let s = pys.bind(py);
8181
assert!(s.is_instance_of::<pyo3::types::PyString>());
8282
assert_eq!(
8383
s.downcast_exact::<pyo3::types::PyString>()

tests/test_py.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ fn test_reclass() {
1010
let r = Reclass::new("./tests/inventory", "nodes", "classes", false)
1111
.unwrap()
1212
.into_py(py);
13-
let locals = PyDict::new(py);
13+
let locals = PyDict::new_bound(py);
1414
locals.set_item("r", r).unwrap();
15-
py.run(
15+
py.run_bound(
1616
r#"assert r and "Reclass" in str(type(r))"#,
1717
None,
18-
Some(locals),
18+
Some(&locals),
1919
)
2020
.unwrap();
2121
});

0 commit comments

Comments
 (0)