@@ -13,16 +13,37 @@ use libffi::middle::{Arg, Cif, CodePtr, Type};
1313use libloading:: Symbol ;
1414use num_traits:: ToPrimitive ;
1515use rustpython_common:: lock:: PyRwLock ;
16+ use std:: ffi;
1617use std:: fmt:: Debug ;
1718
1819// https://github.com/python/cpython/blob/4f8bb3947cfbc20f970ff9d9531e1132a9e95396/Modules/_ctypes/callproc.c#L15
1920
21+ /// Returns none if the `_type_` attribute cannot be found/converted to a string
22+ fn get_type_char ( ty : PyTypeRef ) -> Option < String > {
23+ // PyTypeRef could be c_int, c_float, c_double, etc., we need to get the _type_ attribute
24+ // and convert it to a Type
25+ let type_str = ty. get_attr ( "_type_" ) ?;
26+ let type_str = type_str. downcast_ref :: < PyStr > ( ) ?. to_string ( ) ;
27+ Some ( type_str)
28+ }
29+
30+ fn type_ref_to_type ( ty : PyTypeRef ) -> Type {
31+ // PyTypeRef could be c_int, c_float, c_double, etc., we need to get the _type_ attribute
32+ // and convert it to a Type
33+ unimplemented ! ( )
34+ }
35+
36+ fn obj_from_type_ref_and_data ( data : ffi:: c_void , ty : PyTypeRef ) -> PyObjectRef {
37+ unimplemented ! ( )
38+ }
39+
2040#[ derive( Debug ) ]
2141pub struct Function {
2242 args : Vec < Type > ,
2343 // TODO: no protection from use-after-free
2444 pointer : CodePtr ,
2545 cif : Cif ,
46+ return_type : Option < PyTypeRef > ,
2647}
2748
2849unsafe impl Send for Function { }
@@ -85,7 +106,7 @@ impl Function {
85106 . map_err ( |err| vm. new_attribute_error ( err) ) ?
86107 } ;
87108 let code_ptr = CodePtr ( * pointer as * mut _ ) ;
88- let return_type = match ret_type {
109+ let return_type = match ret_type. clone ( ) {
89110 // TODO: Fix this
90111 Some ( _t) => {
91112 return Err ( vm. new_not_implemented_error ( "Return type not implemented" . to_string ( ) ) ) ;
@@ -97,6 +118,7 @@ impl Function {
97118 args,
98119 cif,
99120 pointer : code_ptr,
121+ return_type : ret_type,
100122 } )
101123 }
102124
@@ -119,8 +141,7 @@ impl Function {
119141 Err ( vm. new_type_error ( "Expected a ctypes simple type" . to_string ( ) ) )
120142 } )
121143 . collect :: < PyResult < Vec < Arg > > > ( ) ?;
122- // TODO: FIX return
123- let result: i32 = unsafe { self . cif . call ( self . pointer , & args) } ;
144+ let result: ffi:: c_void = unsafe { self . cif . call ( self . pointer , & args) } ;
124145 Ok ( vm. ctx . new_int ( result) . into ( ) )
125146 }
126147}
0 commit comments