Patchwork [3,of,7] rust-cpython: fix signature of make_dirstate_tuple()

login
register
mail settings
Submitter Yuya Nishihara
Date Oct. 13, 2019, 1:41 p.m.
Message ID <1c18f9652cda28e7d8d0.1570974099@mimosa>
Download mbox | patch
Permalink /patch/42297/
State New
Headers show

Comments

Yuya Nishihara - Oct. 13, 2019, 1:41 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1570953670 -32400
#      Sun Oct 13 17:01:10 2019 +0900
# Node ID 1c18f9652cda28e7d8d0ffc2e7a01d16622bc98f
# Parent  46a5d47e23a43c12115de6707fe727e52a8a5f74
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.

Patch

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
@@ -23,9 +23,10 @@  use hg::{
 };
 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 @@  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:
@@ -76,10 +77,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> {