@@ -76,8 +76,7 @@ pub fn pymodule_impl(
7676
7777 let module_def_name = module_def_ident ( fnname) ;
7878
79- quote ! {
80- #[ cfg( any( not( feature = "pep489" ) , PyPy ) ) ]
79+ let single_phase_module = quote ! {
8180 #[ no_mangle]
8281 #[ allow( non_snake_case) ]
8382 /// This autogenerated function is called by the python interpreter when importing
@@ -86,13 +85,13 @@ pub fn pymodule_impl(
8685 unsafe { #module_def_name. module_init( ) }
8786 }
8887
89- #[ cfg( any( not( feature = "pep489" ) , PyPy ) ) ]
9088 #[ doc( hidden) ]
9189 #visibility static #module_def_name: #krate:: impl_:: pymodule:: ModuleDef = unsafe {
9290 #krate:: impl_:: pymodule:: ModuleDef :: new( concat!( stringify!( #name) , "\0 " ) , #doc, #krate:: impl_:: pymodule:: ModuleInitializer ( #fnname) )
9391 } ;
92+ } ;
9493
95- # [ cfg ( all ( feature = "pep489" , not ( PyPy ) ) ) ]
94+ let multi_phase_module = quote ! {
9695 #[ no_mangle]
9796 #[ allow( non_snake_case) ]
9897 /// This autogenerated function is called by the python interpreter when importing
@@ -101,7 +100,6 @@ pub fn pymodule_impl(
101100 unsafe { #krate:: ffi:: PyModuleDef_Init ( #module_def_name. module_def( ) ) }
102101 }
103102
104- #[ cfg( all( feature = "pep489" , not( PyPy ) ) ) ]
105103 pub unsafe extern "C" fn #mod_exec_name( module: * mut #krate:: ffi:: PyObject ) -> :: std:: os:: raw:: c_int {
106104 use #krate:: conversion:: FromPyPointer ;
107105
@@ -118,7 +116,6 @@ pub fn pymodule_impl(
118116 }
119117
120118 // An array of slot definitions for multi-phase initialization, terminated by a {0, NULL} entry.
121- #[ cfg( all( feature = "pep489" , not( PyPy ) ) ) ]
122119 #[ doc( hidden) ]
123120 #visibility static #slots_name: [ #krate:: impl_:: pymodule:: ModuleDefSlot ; 2 ] = [
124121 #krate:: impl_:: pymodule:: ModuleDefSlot :: new(
@@ -131,7 +128,6 @@ pub fn pymodule_impl(
131128 ) ,
132129 ] ;
133130
134- #[ cfg( all( feature = "pep489" , not( PyPy ) ) ) ]
135131 #[ doc( hidden) ]
136132 #visibility static #module_def_name: #krate:: impl_:: pymodule:: ModuleDef = unsafe {
137133 #krate:: impl_:: pymodule:: ModuleDef :: new(
@@ -140,9 +136,22 @@ pub fn pymodule_impl(
140136 & #slots_name as * const #krate:: impl_:: pymodule:: ModuleDefSlot as * mut #krate:: ffi:: PyModuleDef_Slot ,
141137 )
142138 } ;
139+ } ;
140+
141+ if is_pypy ( ) {
142+ single_phase_module
143+ } else if cfg ! ( feature = "pep489" ) {
144+ multi_phase_module
145+ } else {
146+ single_phase_module
143147 }
144148}
145149
150+ fn is_pypy ( ) -> bool {
151+ let config = pyo3_build_config:: get ( ) ;
152+ config. implementation . is_pypy ( )
153+ }
154+
146155/// Finds and takes care of the #[pyfn(...)] in `#[pymodule]`
147156pub fn process_functions_in_module ( func : & mut syn:: ItemFn ) -> syn:: Result < ( ) > {
148157 let mut stmts: Vec < syn:: Stmt > = Vec :: new ( ) ;
0 commit comments