[PATCH 4 of 7] rust-cpython: fix signature of make_dirstate_tuple()

Yuya Nishihara yuya at tcha.org
Sun Oct 13 05:47:07 EDT 2019


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1570953670 -32400
#      Sun Oct 13 17:01:10 2019 +0900
# Node ID 8ba14080af7216e711666457c2c068661ae3a97e
# Parent  63a1df3aac72e67667eb7210f364f4b2d892b06f
rust-cpython: fix signature of make_dirstate_tuple()

Fortunately, the layout of PyObject {} is compatible with a C pointer, but
we shouldn't rely on that. This also fixes the handling of NULL pointer.

diff --git a/rust/hg-cpython/src/dirstate.rs b/rust/hg-cpython/src/dirstate.rs
--- a/rust/hg-cpython/src/dirstate.rs
+++ b/rust/hg-cpython/src/dirstate.rs
@@ -15,7 +15,7 @@ mod dirs_multiset;
 mod dirstate_map;
 
 use crate::dirstate::{dirs_multiset::Dirs, dirstate_map::DirstateMap};
-use crate::python_sys::PyCapsule_Import;
+use crate::python_sys::{self, PyCapsule_Import};
 use cpython::{
     exc, PyBytes, PyDict, PyErr, PyModule, PyObject, PyResult, PySequence,
     Python,
@@ -40,7 +40,7 @@ type MakeDirstateTupleFn = unsafe extern
     mode: c_int,
     size: c_int,
     mtime: c_int,
-) -> PyObject;
+) -> *mut python_sys::PyObject;
 
 /// This is largely a copy/paste from cindex.rs, pending the merge of a
 /// `py_capsule_fn!` macro in the rust-cpython project:
@@ -75,10 +75,11 @@ pub fn make_dirstate_tuple(
     // just do a naive enum cast.
     let state_code: u8 = state.into();
 
-    unsafe {
+    let maybe_obj = unsafe {
         let ptr = make(state_code as c_char, mode, size, mtime);
-        Ok(ptr)
-    }
+        PyObject::from_owned_ptr_opt(py, ptr)
+    };
+    maybe_obj.ok_or_else(|| PyErr::fetch(py))
 }
 
 pub fn extract_dirstate(py: Python, dmap: &PyDict) -> Result<StateMap, PyErr> {


More information about the Mercurial-devel mailing list