# HG changeset patch # User Yuya Nishihara # Date 1570953670 -32400 # Node ID 3fec5244aff1d831685e15cd05313ccc947f0726 # Parent 8b355cbef16d661792237ba89e7c16e9e123423c 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 -r 8b355cbef16d -r 3fec5244aff1 rust/hg-cpython/src/dirstate.rs --- a/rust/hg-cpython/src/dirstate.rs Sun Oct 13 16:58:15 2019 +0900 +++ b/rust/hg-cpython/src/dirstate.rs Sun Oct 13 17:01:10 2019 +0900 @@ -23,9 +23,10 @@ }; use libc::{c_char, c_int}; #[cfg(feature = "python27")] -use python27_sys::PyCapsule_Import; +use python27_sys as python_sys; #[cfg(feature = "python3")] -use python3_sys::PyCapsule_Import; +use python3_sys as python_sys; +use python_sys::PyCapsule_Import; use std::convert::TryFrom; use std::ffi::CStr; use std::mem::transmute; @@ -41,7 +42,7 @@ 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: @@ -76,10 +77,11 @@ // 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 {