@@ -9,7 +9,7 @@ use crate::{
9
9
impl_:: pymethods:: { self , PyMethodDef } ,
10
10
types:: { PyCapsule , PyDict , PyModule , PyString , PyTuple } ,
11
11
} ;
12
- use crate :: { Bound , IntoPy , Py , PyAny , PyResult , Python } ;
12
+ use crate :: { Bound , IntoPy , Py , PyAny , PyNativeType , PyResult , Python } ;
13
13
use std:: cell:: UnsafeCell ;
14
14
use std:: ffi:: CStr ;
15
15
@@ -34,13 +34,15 @@ impl PyCFunction {
34
34
doc : & ' static str ,
35
35
py_or_module : PyFunctionArguments < ' a > ,
36
36
) -> PyResult < & ' a Self > {
37
+ let ( py, module) = py_or_module. into_py_and_maybe_module ( ) ;
37
38
Self :: internal_new (
39
+ py,
38
40
& PyMethodDef :: cfunction_with_keywords (
39
41
name,
40
42
pymethods:: PyCFunctionWithKeywords ( fun) ,
41
43
doc,
42
44
) ,
43
- py_or_module ,
45
+ module . map ( PyNativeType :: as_borrowed ) . as_deref ( ) ,
44
46
)
45
47
. map ( Bound :: into_gil_ref)
46
48
}
@@ -53,7 +55,7 @@ impl PyCFunction {
53
55
doc : & ' static str ,
54
56
module : Option < & Bound < ' py , PyModule > > ,
55
57
) -> PyResult < Bound < ' py , Self > > {
56
- Self :: internal_new_bound (
58
+ Self :: internal_new (
57
59
py,
58
60
& PyMethodDef :: cfunction_with_keywords (
59
61
name,
@@ -78,9 +80,11 @@ impl PyCFunction {
78
80
doc : & ' static str ,
79
81
py_or_module : PyFunctionArguments < ' a > ,
80
82
) -> PyResult < & ' a Self > {
83
+ let ( py, module) = py_or_module. into_py_and_maybe_module ( ) ;
81
84
Self :: internal_new (
85
+ py,
82
86
& PyMethodDef :: noargs ( name, pymethods:: PyCFunction ( fun) , doc) ,
83
- py_or_module ,
87
+ module . map ( PyNativeType :: as_borrowed ) . as_deref ( ) ,
84
88
)
85
89
. map ( Bound :: into_gil_ref)
86
90
}
@@ -93,7 +97,7 @@ impl PyCFunction {
93
97
doc : & ' static str ,
94
98
module : Option < & Bound < ' py , PyModule > > ,
95
99
) -> PyResult < Bound < ' py , Self > > {
96
- Self :: internal_new_bound (
100
+ Self :: internal_new (
97
101
py,
98
102
& PyMethodDef :: noargs ( name, pymethods:: PyCFunction ( fun) , doc) ,
99
103
module,
@@ -180,35 +184,6 @@ impl PyCFunction {
180
184
181
185
#[ doc( hidden) ]
182
186
pub fn internal_new < ' py > (
183
- method_def : & PyMethodDef ,
184
- py_or_module : PyFunctionArguments < ' py > ,
185
- ) -> PyResult < Bound < ' py , Self > > {
186
- let ( py, module) = py_or_module. into_py_and_maybe_module ( ) ;
187
- let ( mod_ptr, module_name) : ( _ , Option < Py < PyString > > ) = if let Some ( m) = module {
188
- let mod_ptr = m. as_ptr ( ) ;
189
- ( mod_ptr, Some ( m. name ( ) ?. into_py ( py) ) )
190
- } else {
191
- ( std:: ptr:: null_mut ( ) , None )
192
- } ;
193
- let ( def, destructor) = method_def. as_method_def ( ) ?;
194
-
195
- // FIXME: stop leaking the def and destructor
196
- let def = Box :: into_raw ( Box :: new ( def) ) ;
197
- std:: mem:: forget ( destructor) ;
198
-
199
- let module_name_ptr = module_name
200
- . as_ref ( )
201
- . map_or ( std:: ptr:: null_mut ( ) , Py :: as_ptr) ;
202
-
203
- unsafe {
204
- ffi:: PyCFunction_NewEx ( def, mod_ptr, module_name_ptr)
205
- . assume_owned_or_err ( py)
206
- . downcast_into_unchecked ( )
207
- }
208
- }
209
-
210
- #[ doc( hidden) ]
211
- pub ( crate ) fn internal_new_bound < ' py > (
212
187
py : Python < ' py > ,
213
188
method_def : & PyMethodDef ,
214
189
module : Option < & Bound < ' py , PyModule > > ,
0 commit comments