@@ -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;
@@ -65,6 +65,11 @@ mod posixshmem;
6565#[ cfg( unix) ]
6666mod posixsubprocess;
6767// libc is missing constants on redox
68+ #[ cfg( all(
69+ feature = "sqlite" ,
70+ not( any( target_os = "android" , target_arch = "wasm32" ) )
71+ ) ) ]
72+ mod _sqlite3;
6873#[ cfg( all( unix, not( any( target_os = "android" , target_os = "redox" ) ) ) ) ]
6974mod grp;
7075#[ cfg( windows) ]
@@ -75,11 +80,6 @@ mod resource;
7580mod scproxy;
7681#[ cfg( any( unix, windows, target_os = "wasi" ) ) ]
7782mod select;
78- #[ cfg( all(
79- feature = "sqlite" ,
80- not( any( target_os = "android" , target_arch = "wasm32" ) )
81- ) ) ]
82- mod sqlite;
8383
8484#[ cfg( all( not( target_arch = "wasm32" ) , feature = "ssl-openssl" ) ) ]
8585mod openssl;
@@ -105,131 +105,87 @@ 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- } } ;
129- }
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- }
113+ pub fn stdlib_module_defs ( ctx : & Context ) -> Vec < & ' static builtins:: PyModuleDef > {
114+ vec ! [
115+ _opcode:: module_def( ctx) ,
116+ array:: module_def( ctx) ,
117+ binascii:: module_def( ctx) ,
118+ bisect:: module_def( ctx) ,
119+ blake2:: module_def( ctx) ,
120+ bz2:: module_def( ctx) ,
121+ cmath:: module_def( ctx) ,
122+ contextvars:: module_def( ctx) ,
123+ csv:: module_def( ctx) ,
124+ faulthandler:: module_def( ctx) ,
160125 #[ 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- }
126+ fcntl:: module_def( ctx) ,
127+ gc:: module_def( ctx) ,
128+ #[ cfg( all( unix, not( any( target_os = "android" , target_os = "redox" ) ) ) ) ]
129+ grp:: module_def( ctx) ,
130+ hashlib:: module_def( ctx) ,
131+ json:: module_def( ctx) ,
132+ #[ cfg( not( any( target_os = "ios" , target_arch = "wasm32" ) ) ) ]
133+ locale:: module_def( ctx) ,
173134 #[ 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- }
135+ lzma:: module_def( ctx) ,
136+ math:: module_def( ctx) ,
137+ md5:: module_def( ctx) ,
138+ #[ cfg( any( unix, windows) ) ]
139+ mmap:: module_def( ctx) ,
140+ #[ cfg( not( target_arch = "wasm32" ) ) ]
141+ multiprocessing:: module_def( ctx) ,
185142 #[ cfg( all( not( target_arch = "wasm32" ) , feature = "ssl-openssl" ) ) ]
186- {
187- "_ssl" => openssl:: module_def,
188- }
143+ openssl:: module_def( ctx) ,
189144 #[ cfg( windows) ]
190- {
191- "_overlapped" => overlapped:: module_def,
192- }
145+ overlapped:: module_def( ctx) ,
193146 #[ cfg( unix) ]
194- {
195- "_posixsubprocess" => posixsubprocess:: module_def,
196- }
147+ posixsubprocess:: module_def( ctx) ,
197148 #[ 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- }
149+ posixshmem:: module_def( ctx) ,
150+ pyexpat:: module_def( ctx) ,
151+ pystruct:: module_def( ctx) ,
152+ random:: module_def( ctx) ,
205153 #[ 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- }
154+ resource:: module_def( ctx) ,
218155 #[ 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- }
156+ scproxy:: module_def( ctx) ,
157+ #[ cfg( any( unix, windows, target_os = "wasi" ) ) ]
158+ select:: module_def( ctx) ,
159+ sha1:: module_def( ctx) ,
160+ sha256:: module_def( ctx) ,
161+ sha3:: module_def( ctx) ,
162+ sha512:: module_def( ctx) ,
163+ #[ cfg( not( target_arch = "wasm32" ) ) ]
164+ socket:: module_def( ctx) ,
165+ #[ cfg( all(
166+ feature = "sqlite" ,
167+ not( any( target_os = "android" , target_arch = "wasm32" ) )
168+ ) ) ]
169+ _sqlite3:: module_def( ctx) ,
170+ #[ cfg( all( not( target_arch = "wasm32" ) , feature = "ssl-rustls" ) ) ]
171+ ssl:: module_def( ctx) ,
172+ statistics:: module_def( ctx) ,
173+ suggestions:: module_def( ctx) ,
174+ #[ cfg( all( unix, not( target_os = "redox" ) ) ) ]
175+ syslog:: module_def( ctx) ,
176+ #[ cfg( all( unix, not( any( target_os = "ios" , target_os = "redox" ) ) ) ) ]
177+ termios:: module_def( ctx) ,
230178 #[ cfg( feature = "tkinter" ) ]
231- {
232- "_tkinter" => tkinter:: module_def,
233- }
234- }
179+ tkinter:: module_def( ctx) ,
180+ unicodedata:: module_def( ctx) ,
181+ #[ cfg( not( any(
182+ target_os = "android" ,
183+ target_os = "ios" ,
184+ target_os = "windows" ,
185+ target_arch = "wasm32" ,
186+ target_os = "redox"
187+ ) ) ) ]
188+ uuid:: module_def( ctx) ,
189+ zlib:: module_def( ctx) ,
190+ ]
235191}
0 commit comments