changeset 51201:52bbb57a76ad

rust-index: results comparison helper with details This is a bit simpler to call and has the advantage of systematically log the encountered deviation. To avoid committing dead code, we apply it to the `pack_header` method, that was already returning the Rust result.
author Georges Racinet <georges.racinet@octobus.net>
date Fri, 29 Sep 2023 15:51:49 +0200
parents bc4d83047c6c
children 16d477bb0078
files rust/hg-cpython/src/revlog.rs
diffstat 1 files changed, 25 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/rust/hg-cpython/src/revlog.rs	Wed Sep 27 10:59:04 2023 +0200
+++ b/rust/hg-cpython/src/revlog.rs	Fri Sep 29 15:51:49 2023 +0200
@@ -13,7 +13,7 @@
 use cpython::{
     buffer::{Element, PyBuffer},
     exc::{IndexError, ValueError},
-    ObjectProtocol, PyBytes, PyClone, PyDict, PyErr, PyInt, PyModule,
+    ObjectProtocol, PyBool, PyBytes, PyClone, PyDict, PyErr, PyInt, PyModule,
     PyObject, PyResult, PyString, PyTuple, Python, PythonObject, ToPyObject,
 };
 use hg::{
@@ -218,10 +218,10 @@
     def pack_header(&self, *args, **kw) -> PyResult<PyObject> {
         let rindex = self.index(py).borrow();
         let packed = rindex.pack_header(args.get_item(py, 0).extract(py)?);
-        let packed = PyBytes::new(py, &packed);
+        let packed = PyBytes::new(py, &packed).into_object();
         let cpacked = self.call_cindex(py, "pack_header", args, kw)?;
-        assert!(packed.as_object().compare(py, cpacked)?.is_eq());
-        Ok(packed.into_object())
+        assert_py_eq(py, "pack_header", &packed, &cpacked)?;
+        Ok(packed)
     }
 
     /// get an index entry
@@ -630,6 +630,27 @@
     }
 }
 
+fn assert_py_eq(
+    py: Python,
+    method: &str,
+    rust: &PyObject,
+    c: &PyObject,
+) -> PyResult<()> {
+    let locals = PyDict::new(py);
+    locals.set_item(py, "rust".into_py_object(py).into_object(), rust)?;
+    locals.set_item(py, "c".into_py_object(py).into_object(), c)?;
+    let is_eq: PyBool =
+        py.eval("rust == c", None, Some(&locals))?.extract(py)?;
+    assert!(
+        is_eq.is_true(),
+        "{} results differ. Rust: {:?} C: {:?}",
+        method,
+        rust,
+        c
+    );
+    Ok(())
+}
+
 /// Create the module, with __package__ given from parent
 pub fn init_module(py: Python, package: &str) -> PyResult<PyModule> {
     let dotted_name = &format!("{}.revlog", package);