rust/hg-cpython/src/revlog.rs
changeset 51198 52bbb57a76ad
parent 51197 bc4d83047c6c
child 51199 16d477bb0078
equal deleted inserted replaced
51197:bc4d83047c6c 51198:52bbb57a76ad
    11     PyRevision,
    11     PyRevision,
    12 };
    12 };
    13 use cpython::{
    13 use cpython::{
    14     buffer::{Element, PyBuffer},
    14     buffer::{Element, PyBuffer},
    15     exc::{IndexError, ValueError},
    15     exc::{IndexError, ValueError},
    16     ObjectProtocol, PyBytes, PyClone, PyDict, PyErr, PyInt, PyModule,
    16     ObjectProtocol, PyBool, PyBytes, PyClone, PyDict, PyErr, PyInt, PyModule,
    17     PyObject, PyResult, PyString, PyTuple, Python, PythonObject, ToPyObject,
    17     PyObject, PyResult, PyString, PyTuple, Python, PythonObject, ToPyObject,
    18 };
    18 };
    19 use hg::{
    19 use hg::{
    20     index::IndexHeader,
    20     index::IndexHeader,
    21     index::{RevisionDataParams, COMPRESSION_MODE_INLINE},
    21     index::{RevisionDataParams, COMPRESSION_MODE_INLINE},
   216 
   216 
   217     /// return a binary packed version of the header
   217     /// return a binary packed version of the header
   218     def pack_header(&self, *args, **kw) -> PyResult<PyObject> {
   218     def pack_header(&self, *args, **kw) -> PyResult<PyObject> {
   219         let rindex = self.index(py).borrow();
   219         let rindex = self.index(py).borrow();
   220         let packed = rindex.pack_header(args.get_item(py, 0).extract(py)?);
   220         let packed = rindex.pack_header(args.get_item(py, 0).extract(py)?);
   221         let packed = PyBytes::new(py, &packed);
   221         let packed = PyBytes::new(py, &packed).into_object();
   222         let cpacked = self.call_cindex(py, "pack_header", args, kw)?;
   222         let cpacked = self.call_cindex(py, "pack_header", args, kw)?;
   223         assert!(packed.as_object().compare(py, cpacked)?.is_eq());
   223         assert_py_eq(py, "pack_header", &packed, &cpacked)?;
   224         Ok(packed.into_object())
   224         Ok(packed)
   225     }
   225     }
   226 
   226 
   227     /// get an index entry
   227     /// get an index entry
   228     def get(&self, *args, **kw) -> PyResult<PyObject> {
   228     def get(&self, *args, **kw) -> PyResult<PyObject> {
   229         self.call_cindex(py, "get", args, kw)
   229         self.call_cindex(py, "get", args, kw)
   628         NodeMapError::MultipleResults => revlog_error(py),
   628         NodeMapError::MultipleResults => revlog_error(py),
   629         NodeMapError::RevisionNotInIndex(r) => nodemap_rev_not_in_index(py, r),
   629         NodeMapError::RevisionNotInIndex(r) => nodemap_rev_not_in_index(py, r),
   630     }
   630     }
   631 }
   631 }
   632 
   632 
       
   633 fn assert_py_eq(
       
   634     py: Python,
       
   635     method: &str,
       
   636     rust: &PyObject,
       
   637     c: &PyObject,
       
   638 ) -> PyResult<()> {
       
   639     let locals = PyDict::new(py);
       
   640     locals.set_item(py, "rust".into_py_object(py).into_object(), rust)?;
       
   641     locals.set_item(py, "c".into_py_object(py).into_object(), c)?;
       
   642     let is_eq: PyBool =
       
   643         py.eval("rust == c", None, Some(&locals))?.extract(py)?;
       
   644     assert!(
       
   645         is_eq.is_true(),
       
   646         "{} results differ. Rust: {:?} C: {:?}",
       
   647         method,
       
   648         rust,
       
   649         c
       
   650     );
       
   651     Ok(())
       
   652 }
       
   653 
   633 /// Create the module, with __package__ given from parent
   654 /// Create the module, with __package__ given from parent
   634 pub fn init_module(py: Python, package: &str) -> PyResult<PyModule> {
   655 pub fn init_module(py: Python, package: &str) -> PyResult<PyModule> {
   635     let dotted_name = &format!("{}.revlog", package);
   656     let dotted_name = &format!("{}.revlog", package);
   636     let m = PyModule::new(py, dotted_name)?;
   657     let m = PyModule::new(py, dotted_name)?;
   637     m.add(py, "__package__", package)?;
   658     m.add(py, "__package__", package)?;