rust: Remove use of `py.eval()`
authorSimon Sapin <simon.sapin@octobus.net>
Thu, 08 Apr 2021 14:38:27 +0200
changeset 46891 c6ceb5f27f97
parent 46890 441024b279a6
child 46892 4a6024b87dfc
rust: Remove use of `py.eval()` The previous Rust code allocated an intermediate `Vec`, converted that to a Python list, then used `eval` to run Python code that converts that list to a Python set. rust-cpython exposes Rust bindings for Python sets, let’s use that instead to construct a set directly. Differential Revision: https://phab.mercurial-scm.org/D10328
rust/hg-cpython/src/dirstate/dirstate_map.rs
--- a/rust/hg-cpython/src/dirstate/dirstate_map.rs	Thu Apr 08 21:46:54 2021 +0200
+++ b/rust/hg-cpython/src/dirstate/dirstate_map.rs	Thu Apr 08 14:38:27 2021 +0200
@@ -14,8 +14,8 @@
 
 use cpython::{
     exc, ObjectProtocol, PyBool, PyBytes, PyClone, PyDict, PyErr, PyList,
-    PyObject, PyResult, PyString, PyTuple, Python, PythonObject, ToPyObject,
-    UnsafePyLeaked,
+    PyObject, PyResult, PySet, PyString, PyTuple, Python, PythonObject,
+    ToPyObject, UnsafePyLeaked,
 };
 
 use crate::{
@@ -175,18 +175,11 @@
         let (_, other_parent) =
             inner_shared.get_non_normal_other_parent_entries();
 
-        let locals = PyDict::new(py);
-        locals.set_item(
-            py,
-            "other_parent",
-            other_parent
-                .iter()
-                .map(|v| PyBytes::new(py, v.as_bytes()))
-                .collect::<Vec<PyBytes>>()
-                .to_py_object(py),
-        )?;
-
-        py.eval("set(other_parent)", None, Some(&locals))
+        let set = PySet::empty(py)?;
+        for path in other_parent.iter() {
+            set.add(py, PyBytes::new(py, path.as_bytes()))?;
+        }
+        Ok(set.into_object())
     }
 
     def non_normal_entries(&self) -> PyResult<NonNormalEntries> {