Skip to main content

rustpython_stdlib/
lib.rs

1// to allow `mod foo {}` in foo.rs; clippy thinks this is a mistake/misunderstanding of
2// how `mod` works, but we want this sometimes for pymodule declarations
3#![deny(clippy::disallowed_methods, clippy::disallowed_types)]
4#![allow(clippy::module_inception)]
5
6#[macro_use]
7extern crate rustpython_derive;
8
9extern crate alloc;
10
11#[macro_use]
12pub(crate) mod macros;
13
14mod _asyncio;
15mod _remote_debugging;
16pub mod array;
17mod binascii;
18mod bisect;
19mod bz2;
20mod cjkcodecs;
21mod cmath;
22mod compression; // internal module
23mod contextvars;
24mod csv;
25mod elementtree;
26
27mod lzma;
28
29mod zlib;
30
31mod blake2;
32mod hashlib;
33mod md5;
34mod sha1;
35mod sha256;
36mod sha3;
37mod sha512;
38
39mod json;
40
41#[cfg(all(
42    feature = "host_env",
43    not(any(target_os = "ios", target_arch = "wasm32"))
44))]
45mod locale;
46
47mod _opcode;
48
49#[path = "_tokenize.rs"]
50mod _tokenize;
51
52mod math;
53
54#[cfg(all(feature = "host_env", any(unix, windows)))]
55mod mmap;
56
57mod _datetime;
58mod _heapq;
59mod _queue;
60mod _zoneinfo;
61mod pickle;
62mod pyexpat;
63mod pystruct;
64mod random;
65mod statistics;
66mod suggestions;
67
68// TODO: maybe make this an extension module, if we ever get those
69// mod re;
70#[cfg(all(
71    feature = "host_env",
72    not(any(all(target_arch = "wasm32", target_os = "unknown"), target_os = "wasi"))
73))]
74pub mod socket;
75
76#[cfg(any(all(target_arch = "wasm32", target_os = "unknown"), target_os = "wasi"))]
77#[path = "socket_wasm.rs"]
78pub mod socket;
79
80#[cfg(all(feature = "host_env", unix, not(target_os = "redox")))]
81mod syslog;
82
83mod unicodedata;
84
85#[cfg(feature = "host_env")]
86mod faulthandler;
87
88#[cfg(all(feature = "host_env", any(unix, target_os = "wasi")))]
89mod fcntl;
90
91#[cfg(all(feature = "host_env", not(target_arch = "wasm32")))]
92mod multiprocessing;
93
94#[cfg(all(
95    feature = "host_env",
96    unix,
97    not(target_os = "redox"),
98    not(target_os = "android")
99))]
100mod posixshmem;
101
102#[cfg(all(feature = "host_env", unix))]
103mod posixsubprocess;
104
105// libc is missing constants on redox
106#[cfg(all(
107    feature = "sqlite",
108    not(any(target_os = "android", target_arch = "wasm32"))
109))]
110mod _sqlite3;
111
112#[cfg(all(feature = "host_env", windows))]
113mod _testconsole;
114
115#[cfg(all(
116    feature = "host_env",
117    unix,
118    not(any(target_os = "android", target_os = "redox"))
119))]
120mod grp;
121
122#[cfg(all(feature = "host_env", windows))]
123mod overlapped;
124
125#[cfg(all(feature = "host_env", unix, not(target_os = "redox")))]
126mod resource;
127
128#[cfg(all(feature = "host_env", target_os = "macos"))]
129mod scproxy;
130
131#[cfg(all(feature = "host_env", any(unix, windows, target_os = "wasi")))]
132mod select;
133
134#[cfg(all(
135    feature = "host_env",
136    not(target_arch = "wasm32"),
137    feature = "ssl-openssl"
138))]
139mod openssl;
140
141// Full rustls `_ssl` stays native. All wasm targets (browser unknown and
142// WASI) bind the rustls-free `rustpython_host_env::ssl` surface (MemoryBIO,
143// constants, OID, ALPN). WASI still compiles the rustls engine inside
144// host_env. `_socket` on wasm is the rustls-free-style shim in
145// `socket_wasm.rs` so `Lib/ssl.py` can import.
146#[cfg(all(
147    feature = "host_env",
148    feature = "__ssl-rustls",
149    any(not(target_arch = "wasm32"), target_os = "wasi"),
150))]
151pub mod ssl;
152
153#[cfg(all(
154    target_arch = "wasm32",
155    feature = "ssl",
156    not(all(feature = "host_env", feature = "__ssl-rustls", target_os = "wasi")),
157))]
158#[path = "ssl_wasm.rs"]
159pub mod ssl;
160
161#[cfg(all(feature = "ssl-openssl", feature = "__ssl-rustls", not(clippy)))]
162compile_error!(r#"features "ssl-openssl" and "ssl-rustls" are mutually exclusive"#);
163
164#[cfg(all(
165    feature = "host_env",
166    unix,
167    not(target_os = "redox"),
168    not(target_os = "ios")
169))]
170mod termios;
171
172#[cfg(all(
173    feature = "host_env",
174    not(any(
175        target_os = "android",
176        target_os = "ios",
177        target_os = "windows",
178        target_arch = "wasm32",
179        target_os = "redox",
180    ))
181))]
182mod uuid;
183
184use rustpython_common as common;
185use rustpython_vm as vm;
186
187use crate::vm::{Context, builtins};
188
189/// Returns module definitions for multi-phase init modules.
190/// These modules are added to sys.modules BEFORE their exec function runs,
191/// allowing safe circular imports.
192pub fn stdlib_module_defs(ctx: &Context) -> Vec<&'static builtins::PyModuleDef> {
193    vec![
194        _asyncio::module_def(ctx),
195        cjkcodecs::_codecs_cn::module_def(ctx),
196        cjkcodecs::_codecs_hk::module_def(ctx),
197        cjkcodecs::_codecs_iso2022::module_def(ctx),
198        cjkcodecs::_codecs_jp::module_def(ctx),
199        cjkcodecs::_codecs_kr::module_def(ctx),
200        cjkcodecs::_codecs_tw::module_def(ctx),
201        cjkcodecs::multibytecodec::module_def(ctx),
202        _opcode::module_def(ctx),
203        _remote_debugging::module_def(ctx),
204        array::module_def(ctx),
205        binascii::module_def(ctx),
206        bisect::module_def(ctx),
207        blake2::module_def(ctx),
208        bz2::module_def(ctx),
209        cmath::module_def(ctx),
210        contextvars::module_def(ctx),
211        csv::module_def(ctx),
212        elementtree::module_def(ctx),
213        #[cfg(feature = "host_env")]
214        faulthandler::module_def(ctx),
215        #[cfg(all(feature = "host_env", any(unix, target_os = "wasi")))]
216        fcntl::module_def(ctx),
217        #[cfg(all(
218            feature = "host_env",
219            unix,
220            not(any(target_os = "android", target_os = "redox"))
221        ))]
222        grp::module_def(ctx),
223        hashlib::module_def(ctx),
224        json::module_def(ctx),
225        #[cfg(all(
226            feature = "host_env",
227            not(any(target_os = "ios", target_arch = "wasm32"))
228        ))]
229        locale::module_def(ctx),
230        lzma::module_def(ctx),
231        math::module_def(ctx),
232        md5::module_def(ctx),
233        #[cfg(all(feature = "host_env", any(unix, windows)))]
234        mmap::module_def(ctx),
235        #[cfg(all(feature = "host_env", not(target_arch = "wasm32")))]
236        multiprocessing::module_def(ctx),
237        #[cfg(all(
238            feature = "host_env",
239            not(target_arch = "wasm32"),
240            feature = "ssl-openssl"
241        ))]
242        openssl::module_def(ctx),
243        #[cfg(all(feature = "host_env", windows))]
244        _testconsole::module_def(ctx),
245        #[cfg(all(feature = "host_env", windows))]
246        overlapped::module_def(ctx),
247        #[cfg(all(feature = "host_env", unix))]
248        posixsubprocess::module_def(ctx),
249        #[cfg(all(
250            feature = "host_env",
251            unix,
252            not(target_os = "redox"),
253            not(target_os = "android")
254        ))]
255        posixshmem::module_def(ctx),
256        pickle::module_def(ctx),
257        pyexpat::module_def(ctx),
258        pystruct::module_def(ctx),
259        _datetime::module_def(ctx),
260        _zoneinfo::module_def(ctx),
261        _heapq::module_def(ctx),
262        _queue::module_def(ctx),
263        random::module_def(ctx),
264        #[cfg(all(feature = "host_env", unix, not(target_os = "redox")))]
265        resource::module_def(ctx),
266        #[cfg(all(feature = "host_env", target_os = "macos"))]
267        scproxy::module_def(ctx),
268        #[cfg(all(feature = "host_env", any(unix, windows, target_os = "wasi")))]
269        select::module_def(ctx),
270        sha1::module_def(ctx),
271        sha256::module_def(ctx),
272        sha3::module_def(ctx),
273        sha512::module_def(ctx),
274        #[cfg(all(
275            feature = "host_env",
276            not(any(all(target_arch = "wasm32", target_os = "unknown"), target_os = "wasi"))
277        ))]
278        socket::module_def(ctx),
279        #[cfg(any(all(target_arch = "wasm32", target_os = "unknown"), target_os = "wasi"))]
280        socket::module_def(ctx),
281        #[cfg(all(
282            feature = "sqlite",
283            not(any(target_os = "android", target_arch = "wasm32"))
284        ))]
285        _sqlite3::module_def(ctx),
286        #[cfg(all(
287            feature = "host_env",
288            feature = "__ssl-rustls",
289            any(not(target_arch = "wasm32"), target_os = "wasi"),
290        ))]
291        ssl::module_def(ctx),
292        #[cfg(all(
293            target_arch = "wasm32",
294            feature = "ssl",
295            not(all(feature = "host_env", feature = "__ssl-rustls", target_os = "wasi")),
296        ))]
297        ssl::module_def(ctx),
298        statistics::module_def(ctx),
299        suggestions::module_def(ctx),
300        _tokenize::module_def(ctx),
301        #[cfg(all(feature = "host_env", unix, not(target_os = "redox")))]
302        syslog::module_def(ctx),
303        #[cfg(all(
304            feature = "host_env",
305            unix,
306            not(any(target_os = "ios", target_os = "redox"))
307        ))]
308        termios::module_def(ctx),
309        unicodedata::module_def(ctx),
310        #[cfg(all(
311            feature = "host_env",
312            not(any(
313                target_os = "android",
314                target_os = "ios",
315                target_os = "windows",
316                target_arch = "wasm32",
317                target_os = "redox"
318            ))
319        ))]
320        uuid::module_def(ctx),
321        zlib::module_def(ctx),
322    ]
323}