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

login
register
mail settings
Submitter Yuya Nishihara
Date Oct. 13, 2019, 9:47 a.m.
Message ID <8ba14080af7216e71166.1570960027@mimosa>
Download mbox | patch
Permalink /patch/42290/
State New
Headers show

Comments

Yuya Nishihara - Oct. 13, 2019, 9:47 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@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.

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
@@ -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> {