@@ -38,10 +38,10 @@ mod json;
3838#[ cfg( not( any( target_os = "ios" , target_arch = "wasm32" ) ) ) ]
3939mod locale;
4040
41+ mod _opcode;
4142mod math;
4243#[ cfg( any( unix, windows) ) ]
4344mod mmap;
44- mod opcode;
4545mod pyexpat;
4646mod pystruct;
4747mod random;
@@ -105,131 +105,109 @@ mod tkinter;
105105use rustpython_common as common;
106106use rustpython_vm as vm;
107107
108- use crate :: vm:: { builtins, stdlib:: StdlibDefFunc } ;
109- use alloc:: borrow:: Cow ;
108+ use crate :: vm:: { Context , builtins} ;
110109
111110/// Returns module definitions for multi-phase init modules.
112111/// These modules are added to sys.modules BEFORE their exec function runs,
113112/// allowing safe circular imports.
114- pub fn get_module_defs ( ) -> impl Iterator < Item = ( Cow < ' static , str > , StdlibDefFunc ) > {
115- macro_rules! modules {
116- {
117- $(
118- #[ cfg( $cfg: meta) ]
119- { $( $key: expr => $val: expr) ,* $( , ) ? }
120- ) *
121- } => { {
122- [
123- $(
124- $( #[ cfg( $cfg) ] ( Cow :: <' static , str >:: from( $key) , $val as StdlibDefFunc ) , ) *
125- ) *
126- ]
127- . into_iter( )
128- } } ;
113+ pub fn stdlib_module_defs ( ctx : & Context ) -> Vec < & ' static builtins:: PyModuleDef > {
114+ let mut defs = vec ! [
115+ array:: module_def( ctx) ,
116+ binascii:: module_def( ctx) ,
117+ bisect:: module_def( ctx) ,
118+ blake2:: module_def( ctx) ,
119+ bz2:: module_def( ctx) ,
120+ cmath:: module_def( ctx) ,
121+ contextvars:: module_def( ctx) ,
122+ csv:: module_def( ctx) ,
123+ faulthandler:: module_def( ctx) ,
124+ gc:: module_def( ctx) ,
125+ hashlib:: module_def( ctx) ,
126+ json:: module_def( ctx) ,
127+ math:: module_def( ctx) ,
128+ md5:: module_def( ctx) ,
129+ _opcode:: module_def( ctx) ,
130+ random:: module_def( ctx) ,
131+ sha1:: module_def( ctx) ,
132+ sha256:: module_def( ctx) ,
133+ sha3:: module_def( ctx) ,
134+ sha512:: module_def( ctx) ,
135+ statistics:: module_def( ctx) ,
136+ pystruct:: module_def( ctx) ,
137+ suggestions:: module_def( ctx) ,
138+ zlib:: module_def( ctx) ,
139+ pyexpat:: module_def( ctx) ,
140+ unicodedata:: module_def( ctx) ,
141+ ] ;
142+
143+ #[ cfg( any( unix, target_os = "wasi" ) ) ]
144+ defs. push ( fcntl:: module_def ( ctx) ) ;
145+
146+ #[ cfg( any( unix, windows, target_os = "wasi" ) ) ]
147+ defs. push ( select:: module_def ( ctx) ) ;
148+
149+ #[ cfg( not( target_arch = "wasm32" ) ) ]
150+ {
151+ defs. push ( multiprocessing:: module_def ( ctx) ) ;
152+ defs. push ( socket:: module_def ( ctx) ) ;
129153 }
130- modules ! {
131- #[ cfg( all( ) ) ]
132- {
133- "array" => array:: module_def,
134- "binascii" => binascii:: module_def,
135- "_bisect" => bisect:: module_def,
136- "_blake2" => blake2:: module_def,
137- "_bz2" => bz2:: module_def,
138- "cmath" => cmath:: module_def,
139- "_contextvars" => contextvars:: module_def,
140- "_csv" => csv:: module_def,
141- "faulthandler" => faulthandler:: module_def,
142- "gc" => gc:: module_def,
143- "_hashlib" => hashlib:: module_def,
144- "_json" => json:: module_def,
145- "math" => math:: module_def,
146- "_md5" => md5:: module_def,
147- "_opcode" => opcode:: module_def,
148- "_random" => random:: module_def,
149- "_sha1" => sha1:: module_def,
150- "_sha256" => sha256:: module_def,
151- "_sha3" => sha3:: module_def,
152- "_sha512" => sha512:: module_def,
153- "_statistics" => statistics:: module_def,
154- "_struct" => pystruct:: module_def,
155- "_suggestions" => suggestions:: module_def,
156- "zlib" => zlib:: module_def,
157- "pyexpat" => pyexpat:: module_def,
158- "unicodedata" => unicodedata:: module_def,
159- }
160- #[ cfg( any( unix, target_os = "wasi" ) ) ]
161- {
162- "fcntl" => fcntl:: module_def,
163- }
164- #[ cfg( any( unix, windows, target_os = "wasi" ) ) ]
165- {
166- "select" => select:: module_def,
167- }
168- #[ cfg( not( target_arch = "wasm32" ) ) ]
169- {
170- "_multiprocessing" => multiprocessing:: module_def,
171- "_socket" => socket:: module_def,
172- }
173- #[ cfg( not( any( target_os = "android" , target_arch = "wasm32" ) ) ) ]
174- {
175- "_lzma" => lzma:: module_def,
176- }
177- #[ cfg( all( feature = "sqlite" , not( any( target_os = "android" , target_arch = "wasm32" ) ) ) ) ]
178- {
179- "_sqlite3" => sqlite:: module_def,
180- }
181- #[ cfg( all( not( target_arch = "wasm32" ) , feature = "ssl-rustls" ) ) ]
182- {
183- "_ssl" => ssl:: module_def,
184- }
185- #[ cfg( all( not( target_arch = "wasm32" ) , feature = "ssl-openssl" ) ) ]
186- {
187- "_ssl" => openssl:: module_def,
188- }
189- #[ cfg( windows) ]
190- {
191- "_overlapped" => overlapped:: module_def,
192- }
193- #[ cfg( unix) ]
194- {
195- "_posixsubprocess" => posixsubprocess:: module_def,
196- }
197- #[ cfg( all( unix, not( target_os = "redox" ) , not( target_os = "android" ) ) ) ]
198- {
199- "_posixshmem" => posixshmem:: module_def,
200- }
201- #[ cfg( any( unix, windows) ) ]
202- {
203- "mmap" => mmap:: module_def,
204- }
205- #[ cfg( all( unix, not( target_os = "redox" ) ) ) ]
206- {
207- "syslog" => syslog:: module_def,
208- "resource" => resource:: module_def,
209- }
210- #[ cfg( all( unix, not( any( target_os = "ios" , target_os = "redox" ) ) ) ) ]
211- {
212- "termios" => termios:: module_def,
213- }
214- #[ cfg( all( unix, not( any( target_os = "android" , target_os = "redox" ) ) ) ) ]
215- {
216- "grp" => grp:: module_def,
217- }
218- #[ cfg( target_os = "macos" ) ]
219- {
220- "_scproxy" => scproxy:: module_def,
221- }
222- #[ cfg( not( any( target_os = "android" , target_os = "ios" , target_os = "windows" , target_arch = "wasm32" , target_os = "redox" ) ) ) ]
223- {
224- "_uuid" => uuid:: module_def,
225- }
226- #[ cfg( not( any( target_os = "ios" , target_arch = "wasm32" ) ) ) ]
227- {
228- "_locale" => locale:: module_def,
229- }
230- #[ cfg( feature = "tkinter" ) ]
231- {
232- "_tkinter" => tkinter:: module_def,
233- }
154+
155+ #[ cfg( not( any( target_os = "android" , target_arch = "wasm32" ) ) ) ]
156+ defs. push ( lzma:: module_def ( ctx) ) ;
157+
158+ #[ cfg( all(
159+ feature = "sqlite" ,
160+ not( any( target_os = "android" , target_arch = "wasm32" ) )
161+ ) ) ]
162+ defs. push ( sqlite:: module_def ( ctx) ) ;
163+
164+ #[ cfg( all( not( target_arch = "wasm32" ) , feature = "ssl-rustls" ) ) ]
165+ defs. push ( ssl:: module_def ( ctx) ) ;
166+
167+ #[ cfg( all( not( target_arch = "wasm32" ) , feature = "ssl-openssl" ) ) ]
168+ defs. push ( openssl:: module_def ( ctx) ) ;
169+
170+ #[ cfg( windows) ]
171+ defs. push ( overlapped:: module_def ( ctx) ) ;
172+
173+ #[ cfg( unix) ]
174+ defs. push ( posixsubprocess:: module_def ( ctx) ) ;
175+
176+ #[ cfg( all( unix, not( target_os = "redox" ) , not( target_os = "android" ) ) ) ]
177+ defs. push ( posixshmem:: module_def ( ctx) ) ;
178+
179+ #[ cfg( any( unix, windows) ) ]
180+ defs. push ( mmap:: module_def ( ctx) ) ;
181+
182+ #[ cfg( all( unix, not( target_os = "redox" ) ) ) ]
183+ {
184+ defs. push ( syslog:: module_def ( ctx) ) ;
185+ defs. push ( resource:: module_def ( ctx) ) ;
234186 }
187+
188+ #[ cfg( all( unix, not( any( target_os = "ios" , target_os = "redox" ) ) ) ) ]
189+ defs. push ( termios:: module_def ( ctx) ) ;
190+
191+ #[ cfg( all( unix, not( any( target_os = "android" , target_os = "redox" ) ) ) ) ]
192+ defs. push ( grp:: module_def ( ctx) ) ;
193+
194+ #[ cfg( target_os = "macos" ) ]
195+ defs. push ( scproxy:: module_def ( ctx) ) ;
196+
197+ #[ cfg( not( any(
198+ target_os = "android" ,
199+ target_os = "ios" ,
200+ target_os = "windows" ,
201+ target_arch = "wasm32" ,
202+ target_os = "redox"
203+ ) ) ) ]
204+ defs. push ( uuid:: module_def ( ctx) ) ;
205+
206+ #[ cfg( not( any( target_os = "ios" , target_arch = "wasm32" ) ) ) ]
207+ defs. push ( locale:: module_def ( ctx) ) ;
208+
209+ #[ cfg( feature = "tkinter" ) ]
210+ defs. push ( tkinter:: module_def ( ctx) ) ;
211+
212+ defs
235213}
0 commit comments