rustpython-stdlib 0.6.0

RustPython standard libraries in Rust.
Documentation
// to allow `mod foo {}` in foo.rs; clippy thinks this is a mistake/misunderstanding of
// how `mod` works, but we want this sometimes for pymodule declarations
#![deny(clippy::disallowed_methods, clippy::disallowed_types)]
#![allow(clippy::module_inception)]

#[macro_use]
extern crate rustpython_derive;

extern crate alloc;

#[macro_use]
pub(crate) mod macros;

mod _asyncio;
mod _remote_debugging;
pub mod array;
mod binascii;
mod bisect;
mod bz2;
mod cjkcodecs;
mod cmath;
mod compression; // internal module
mod contextvars;
mod csv;
mod elementtree;

mod lzma;

mod zlib;

mod blake2;
mod hashlib;
mod md5;
mod sha1;
mod sha256;
mod sha3;
mod sha512;

mod json;

#[cfg(all(
    feature = "host_env",
    not(any(target_os = "ios", target_arch = "wasm32"))
))]
mod locale;

mod _opcode;

#[path = "_tokenize.rs"]
mod _tokenize;

mod math;

#[cfg(all(feature = "host_env", any(unix, windows)))]
mod mmap;

mod _datetime;
mod _heapq;
mod _queue;
mod _zoneinfo;
mod pickle;
mod pyexpat;
mod pystruct;
mod random;
mod statistics;
mod suggestions;

// TODO: maybe make this an extension module, if we ever get those
// mod re;
#[cfg(all(
    feature = "host_env",
    not(any(all(target_arch = "wasm32", target_os = "unknown"), target_os = "wasi"))
))]
pub mod socket;

#[cfg(any(all(target_arch = "wasm32", target_os = "unknown"), target_os = "wasi"))]
#[path = "socket_wasm.rs"]
pub mod socket;

#[cfg(all(feature = "host_env", unix, not(target_os = "redox")))]
mod syslog;

mod unicodedata;

#[cfg(feature = "host_env")]
mod faulthandler;

#[cfg(all(feature = "host_env", any(unix, target_os = "wasi")))]
mod fcntl;

#[cfg(all(feature = "host_env", not(target_arch = "wasm32")))]
mod multiprocessing;

#[cfg(all(
    feature = "host_env",
    unix,
    not(target_os = "redox"),
    not(target_os = "android")
))]
mod posixshmem;

#[cfg(all(feature = "host_env", unix))]
mod posixsubprocess;

// libc is missing constants on redox
#[cfg(all(
    feature = "sqlite",
    not(any(target_os = "android", target_arch = "wasm32"))
))]
mod _sqlite3;

#[cfg(all(feature = "host_env", windows))]
mod _testconsole;

#[cfg(all(
    feature = "host_env",
    unix,
    not(any(target_os = "android", target_os = "redox"))
))]
mod grp;

#[cfg(all(feature = "host_env", windows))]
mod overlapped;

#[cfg(all(feature = "host_env", unix, not(target_os = "redox")))]
mod resource;

#[cfg(all(feature = "host_env", target_os = "macos"))]
mod scproxy;

#[cfg(all(feature = "host_env", any(unix, windows, target_os = "wasi")))]
mod select;

#[cfg(all(
    feature = "host_env",
    not(target_arch = "wasm32"),
    feature = "ssl-openssl"
))]
mod openssl;

// Full rustls `_ssl` stays native. All wasm targets (browser unknown and
// WASI) bind the rustls-free `rustpython_host_env::ssl` surface (MemoryBIO,
// constants, OID, ALPN). WASI still compiles the rustls engine inside
// host_env. `_socket` on wasm is the rustls-free-style shim in
// `socket_wasm.rs` so `Lib/ssl.py` can import.
#[cfg(all(
    feature = "host_env",
    feature = "__ssl-rustls",
    any(not(target_arch = "wasm32"), target_os = "wasi"),
))]
pub mod ssl;

#[cfg(all(
    target_arch = "wasm32",
    feature = "ssl",
    not(all(feature = "host_env", feature = "__ssl-rustls", target_os = "wasi")),
))]
#[path = "ssl_wasm.rs"]
pub mod ssl;

#[cfg(all(feature = "ssl-openssl", feature = "__ssl-rustls", not(clippy)))]
compile_error!(r#"features "ssl-openssl" and "ssl-rustls" are mutually exclusive"#);

#[cfg(all(
    feature = "host_env",
    unix,
    not(target_os = "redox"),
    not(target_os = "ios")
))]
mod termios;

#[cfg(all(
    feature = "host_env",
    not(any(
        target_os = "android",
        target_os = "ios",
        target_os = "windows",
        target_arch = "wasm32",
        target_os = "redox",
    ))
))]
mod uuid;

use rustpython_common as common;
use rustpython_vm as vm;

use crate::vm::{Context, builtins};

/// Returns module definitions for multi-phase init modules.
/// These modules are added to sys.modules BEFORE their exec function runs,
/// allowing safe circular imports.
pub fn stdlib_module_defs(ctx: &Context) -> Vec<&'static builtins::PyModuleDef> {
    vec![
        _asyncio::module_def(ctx),
        cjkcodecs::_codecs_cn::module_def(ctx),
        cjkcodecs::_codecs_hk::module_def(ctx),
        cjkcodecs::_codecs_iso2022::module_def(ctx),
        cjkcodecs::_codecs_jp::module_def(ctx),
        cjkcodecs::_codecs_kr::module_def(ctx),
        cjkcodecs::_codecs_tw::module_def(ctx),
        cjkcodecs::multibytecodec::module_def(ctx),
        _opcode::module_def(ctx),
        _remote_debugging::module_def(ctx),
        array::module_def(ctx),
        binascii::module_def(ctx),
        bisect::module_def(ctx),
        blake2::module_def(ctx),
        bz2::module_def(ctx),
        cmath::module_def(ctx),
        contextvars::module_def(ctx),
        csv::module_def(ctx),
        elementtree::module_def(ctx),
        #[cfg(feature = "host_env")]
        faulthandler::module_def(ctx),
        #[cfg(all(feature = "host_env", any(unix, target_os = "wasi")))]
        fcntl::module_def(ctx),
        #[cfg(all(
            feature = "host_env",
            unix,
            not(any(target_os = "android", target_os = "redox"))
        ))]
        grp::module_def(ctx),
        hashlib::module_def(ctx),
        json::module_def(ctx),
        #[cfg(all(
            feature = "host_env",
            not(any(target_os = "ios", target_arch = "wasm32"))
        ))]
        locale::module_def(ctx),
        lzma::module_def(ctx),
        math::module_def(ctx),
        md5::module_def(ctx),
        #[cfg(all(feature = "host_env", any(unix, windows)))]
        mmap::module_def(ctx),
        #[cfg(all(feature = "host_env", not(target_arch = "wasm32")))]
        multiprocessing::module_def(ctx),
        #[cfg(all(
            feature = "host_env",
            not(target_arch = "wasm32"),
            feature = "ssl-openssl"
        ))]
        openssl::module_def(ctx),
        #[cfg(all(feature = "host_env", windows))]
        _testconsole::module_def(ctx),
        #[cfg(all(feature = "host_env", windows))]
        overlapped::module_def(ctx),
        #[cfg(all(feature = "host_env", unix))]
        posixsubprocess::module_def(ctx),
        #[cfg(all(
            feature = "host_env",
            unix,
            not(target_os = "redox"),
            not(target_os = "android")
        ))]
        posixshmem::module_def(ctx),
        pickle::module_def(ctx),
        pyexpat::module_def(ctx),
        pystruct::module_def(ctx),
        _datetime::module_def(ctx),
        _zoneinfo::module_def(ctx),
        _heapq::module_def(ctx),
        _queue::module_def(ctx),
        random::module_def(ctx),
        #[cfg(all(feature = "host_env", unix, not(target_os = "redox")))]
        resource::module_def(ctx),
        #[cfg(all(feature = "host_env", target_os = "macos"))]
        scproxy::module_def(ctx),
        #[cfg(all(feature = "host_env", any(unix, windows, target_os = "wasi")))]
        select::module_def(ctx),
        sha1::module_def(ctx),
        sha256::module_def(ctx),
        sha3::module_def(ctx),
        sha512::module_def(ctx),
        #[cfg(all(
            feature = "host_env",
            not(any(all(target_arch = "wasm32", target_os = "unknown"), target_os = "wasi"))
        ))]
        socket::module_def(ctx),
        #[cfg(any(all(target_arch = "wasm32", target_os = "unknown"), target_os = "wasi"))]
        socket::module_def(ctx),
        #[cfg(all(
            feature = "sqlite",
            not(any(target_os = "android", target_arch = "wasm32"))
        ))]
        _sqlite3::module_def(ctx),
        #[cfg(all(
            feature = "host_env",
            feature = "__ssl-rustls",
            any(not(target_arch = "wasm32"), target_os = "wasi"),
        ))]
        ssl::module_def(ctx),
        #[cfg(all(
            target_arch = "wasm32",
            feature = "ssl",
            not(all(feature = "host_env", feature = "__ssl-rustls", target_os = "wasi")),
        ))]
        ssl::module_def(ctx),
        statistics::module_def(ctx),
        suggestions::module_def(ctx),
        _tokenize::module_def(ctx),
        #[cfg(all(feature = "host_env", unix, not(target_os = "redox")))]
        syslog::module_def(ctx),
        #[cfg(all(
            feature = "host_env",
            unix,
            not(any(target_os = "ios", target_os = "redox"))
        ))]
        termios::module_def(ctx),
        unicodedata::module_def(ctx),
        #[cfg(all(
            feature = "host_env",
            not(any(
                target_os = "android",
                target_os = "ios",
                target_os = "windows",
                target_arch = "wasm32",
                target_os = "redox"
            ))
        ))]
        uuid::module_def(ctx),
        zlib::module_def(ctx),
    ]
}