module_def

This commit is contained in:
Jeong YunWon committed 2026-01-21 22:39:40 +09:00
1 parent fee1a4b097
commit 32d2406fa8
42 files changed
+155 -137

No files matched your search

+2 -2
View File
@@ -123,7 +123,7 @@ pub fn impl_pymodule(attr: PunctuatedNestedMeta, module_item: Item) -> Result<To
pub(crate) const DOC: Option<&'static str> = #doc;
},
parse_quote! {
pub(crate) fn __module_def(
pub(crate) fn module_def(
ctx: &::rustpython_vm::Context,
) -> &'static ::rustpython_vm::builtins::PyModuleDef {
DEF.get_or_init(|| {
@@ -144,7 +144,7 @@ pub fn impl_pymodule(attr: PunctuatedNestedMeta, module_item: Item) -> Result<To
vm: &::rustpython_vm::VirtualMachine
) -> ::rustpython_vm::PyRef<::rustpython_vm::builtins::PyModule> {
use ::rustpython_vm::PyPayload;
let module = ::rustpython_vm::builtins::PyModule::from_def(__module_def(&vm.ctx)).into_ref(&vm.ctx);
let module = ::rustpython_vm::builtins::PyModule::from_def(module_def(&vm.ctx)).into_ref(&vm.ctx);
__init_dict(vm, &module);
extend_module(vm, &module).unwrap();
module
+1 -1
View File
@@ -1,7 +1,7 @@
// spell-checker:ignore hexlify unhexlify uuencodes CRCTAB rlecode rledecode
pub(super) use decl::crc32;
pub(crate) use decl::make_module;
pub(crate) use decl::module_def;
use rustpython_vm::{VirtualMachine, builtins::PyBaseExceptionRef, convert::ToPyException};
const PAD: u8 = 61u8;
+1 -1
View File
@@ -1,4 +1,4 @@
pub(crate) use _bisect::make_module;
pub(crate) use _bisect::module_def;
#[pymodule]
mod _bisect {
+1 -1
View File
@@ -1,6 +1,6 @@
// spell-checker:ignore usedforsecurity HASHXOF
pub(crate) use _blake2::make_module;
pub(crate) use _blake2::module_def;
#[pymodule]
mod _blake2 {
+1 -1
View File
@@ -1,6 +1,6 @@
// spell-checker:ignore compresslevel
pub(crate) use _bz2::make_module;
pub(crate) use _bz2::module_def;
#[pymodule]
mod _bz2 {
+1 -1
View File
@@ -1,4 +1,4 @@
pub(crate) use cmath::make_module;
pub(crate) use cmath::module_def;
#[pymodule]
mod cmath {
+1 -1
View File
@@ -1,4 +1,4 @@
pub(crate) use _csv::make_module;
pub(crate) use _csv::module_def;
#[pymodule]
mod _csv {
+1 -1
View File
@@ -1,4 +1,4 @@
pub(crate) use decl::make_module;
pub(crate) use decl::module_def;
#[allow(static_mut_refs)] // TODO: group code only with static mut refs
#[pymodule(name = "faulthandler")]
+1 -1
View File
@@ -1,6 +1,6 @@
// spell-checker:disable
pub(crate) use fcntl::make_module;
pub(crate) use fcntl::module_def;
#[pymodule]
mod fcntl {
+8 -6
View File
@@ -1,8 +1,8 @@
pub(crate) use gc::make_module;
pub(crate) use gc::module_def;
#[pymodule]
mod gc {
use crate::vm::{PyResult, VirtualMachine, function::FuncArgs};
use crate::vm::{PyObjectRef, PyResult, VirtualMachine, function::FuncArgs};
#[pyfunction]
fn collect(_args: FuncArgs, _vm: &VirtualMachine) -> i32 {
@@ -40,13 +40,15 @@ mod gc {
}
#[pyfunction]
fn get_referents(_args: FuncArgs, vm: &VirtualMachine) -> PyResult {
Err(vm.new_not_implemented_error(""))
fn get_referents(_args: FuncArgs, vm: &VirtualMachine) -> PyObjectRef {
// RustPython does not track object references.
vm.ctx.new_tuple(vec![]).into()
}
#[pyfunction]
fn get_referrers(_args: FuncArgs, vm: &VirtualMachine) -> PyResult {
Err(vm.new_not_implemented_error(""))
fn get_referrers(_args: FuncArgs, vm: &VirtualMachine) -> PyObjectRef {
// RustPython does not track object references.
vm.ctx.new_list(vec![]).into()
}
#[pyfunction]
+1 -1
View File
@@ -1,5 +1,5 @@
// spell-checker:disable
pub(crate) use grp::make_module;
pub(crate) use grp::module_def;
#[pymodule]
mod grp {
+1 -1
View File
@@ -1,6 +1,6 @@
// spell-checker:ignore usedforsecurity HASHXOF
pub(crate) use _hashlib::make_module;
pub(crate) use _hashlib::module_def;
#[pymodule]
pub mod _hashlib {
+1 -1
View File
@@ -1,4 +1,4 @@
pub(crate) use _json::make_module;
pub(crate) use _json::module_def;
mod machinery;
#[pymodule]
+105 -87
View File
@@ -115,13 +115,112 @@ use alloc::borrow::Cow;
/// These modules are added to sys.modules BEFORE their exec function runs,
/// allowing safe circular imports.
pub fn get_module_defs() -> impl Iterator<Item = (Cow<'static, str>, StdlibDefFunc)> {
[(
Cow::from("_asyncio"),
_asyncio::__module_def as StdlibDefFunc,
)]
.into_iter()
macro_rules! modules {
{
$(
#[cfg($cfg:meta)]
{ $( $key:expr => $val:expr),* $(,)? }
)*
} => {{
[
$(
$(#[cfg($cfg)] (Cow::<'static, str>::from($key), $val as StdlibDefFunc),)*
)*
]
.into_iter()
}};
}
modules! {
#[cfg(all())]
{
"_asyncio" => _asyncio::module_def,
"binascii" => binascii::module_def,
"_bisect" => bisect::module_def,
"_blake2" => blake2::module_def,
"_bz2" => bz2::module_def,
"cmath" => cmath::module_def,
"_csv" => csv::module_def,
"faulthandler" => faulthandler::module_def,
"gc" => gc::module_def,
"_hashlib" => hashlib::module_def,
"_json" => json::module_def,
"math" => math::module_def,
"_md5" => md5::module_def,
"_opcode" => opcode::module_def,
"_random" => random::module_def,
"_sha1" => sha1::module_def,
"_sha3" => sha3::module_def,
"_statistics" => statistics::module_def,
"_struct" => pystruct::module_def,
"_suggestions" => suggestions::module_def,
"zlib" => zlib::module_def,
}
#[cfg(any(unix, target_os = "wasi"))]
{
"fcntl" => fcntl::module_def,
}
#[cfg(not(target_arch = "wasm32"))]
{
"_multiprocessing" => multiprocessing::module_def,
}
#[cfg(not(any(target_os = "android", target_arch = "wasm32")))]
{
"_lzma" => lzma::module_def,
}
#[cfg(all(not(target_arch = "wasm32"), feature = "ssl-rustls"))]
{
"_ssl" => ssl::module_def,
}
#[cfg(windows)]
{
"_overlapped" => overlapped::module_def,
}
#[cfg(unix)]
{
"_posixsubprocess" => posixsubprocess::module_def,
}
#[cfg(all(unix, not(target_os = "redox"), not(target_os = "android")))]
{
"_posixshmem" => posixshmem::module_def,
}
#[cfg(any(unix, windows))]
{
"mmap" => mmap::module_def,
}
#[cfg(all(unix, not(target_os = "redox")))]
{
"syslog" => syslog::module_def,
"resource" => resource::module_def,
}
#[cfg(all(unix, not(any(target_os = "ios", target_os = "redox"))))]
{
"termios" => termios::module_def,
}
#[cfg(all(unix, not(any(target_os = "android", target_os = "redox"))))]
{
"grp" => grp::module_def,
}
#[cfg(target_os = "macos")]
{
"_scproxy" => scproxy::module_def,
}
#[cfg(not(any(target_os = "android", target_os = "ios", target_os = "windows", target_arch = "wasm32", target_os = "redox")))]
{
"_uuid" => uuid::module_def,
}
#[cfg(not(any(target_os = "ios", target_arch = "wasm32")))]
{
"_locale" => locale::module_def,
}
#[cfg(feature = "tkinter")]
{
"_tkinter" => tkinter::module_def,
}
}
}
/// Returns module initializers for single-phase init modules.
/// These are modules that don't use #[pymodule] macro or need custom initialization.
pub fn get_module_inits() -> impl Iterator<Item = (Cow<'static, str>, StdlibInitFunc)> {
macro_rules! modules {
{
@@ -142,37 +241,11 @@ pub fn get_module_inits() -> impl Iterator<Item = (Cow<'static, str>, StdlibInit
#[cfg(all())]
{
"array" => array::make_module,
"binascii" => binascii::make_module,
"_bisect" => bisect::make_module,
"_bz2" => bz2::make_module,
"cmath" => cmath::make_module,
"_contextvars" => contextvars::make_module,
"_csv" => csv::make_module,
"faulthandler" => faulthandler::make_module,
"gc" => gc::make_module,
"_hashlib" => hashlib::make_module,
"_sha1" => sha1::make_module,
"_sha3" => sha3::make_module,
"pyexpat" => pyexpat::make_module,
"_sha256" => sha256::make_module,
"_sha512" => sha512::make_module,
"_md5" => md5::make_module,
"_blake2" => blake2::make_module,
"_json" => json::make_module,
"math" => math::make_module,
"pyexpat" => pyexpat::make_module,
"_opcode" => opcode::make_module,
"_random" => random::make_module,
"_statistics" => statistics::make_module,
"_struct" => pystruct::make_module,
"unicodedata" => unicodedata::make_module,
"zlib" => zlib::make_module,
"_statistics" => statistics::make_module,
"_suggestions" => suggestions::make_module,
// crate::vm::sysmodule::sysconfigdata_name() => sysconfigdata::make_module,
}
#[cfg(any(unix, target_os = "wasi"))]
{
"fcntl" => fcntl::make_module,
}
#[cfg(any(unix, windows, target_os = "wasi"))]
{
@@ -180,70 +253,15 @@ pub fn get_module_inits() -> impl Iterator<Item = (Cow<'static, str>, StdlibInit
}
#[cfg(not(target_arch = "wasm32"))]
{
"_multiprocessing" => multiprocessing::make_module,
"_socket" => socket::make_module,
}
#[cfg(not(any(target_os = "android", target_arch = "wasm32")))]
{
"_lzma" => lzma::make_module,
}
#[cfg(all(feature = "sqlite", not(any(target_os = "android", target_arch = "wasm32"))))]
{
"_sqlite3" => sqlite::make_module,
}
#[cfg(all(not(target_arch = "wasm32"), feature = "ssl-rustls"))]
{
"_ssl" => ssl::make_module,
}
#[cfg(all(not(target_arch = "wasm32"), feature = "ssl-openssl"))]
{
"_ssl" => openssl::make_module,
}
#[cfg(windows)]
{
"_overlapped" => overlapped::make_module,
}
// Unix-only
#[cfg(unix)]
{
"_posixsubprocess" => posixsubprocess::make_module,
}
#[cfg(all(unix, not(target_os = "redox"), not(target_os = "android")))]
{
"_posixshmem" => posixshmem::make_module,
}
#[cfg(any(unix, windows))]
{
"mmap" => mmap::make_module,
}
#[cfg(all(unix, not(target_os = "redox")))]
{
"syslog" => syslog::make_module,
"resource" => resource::make_module,
}
#[cfg(all(unix, not(any(target_os = "ios", target_os = "redox"))))]
{
"termios" => termios::make_module,
}
#[cfg(all(unix, not(any(target_os = "android", target_os = "redox"))))]
{
"grp" => grp::make_module,
}
#[cfg(target_os = "macos")]
{
"_scproxy" => scproxy::make_module,
}
#[cfg(not(any(target_os = "android", target_os = "ios", target_os = "windows", target_arch = "wasm32", target_os = "redox")))]
{
"_uuid" => uuid::make_module,
}
#[cfg(not(any(target_os = "ios", target_arch = "wasm32")))]
{
"_locale" => locale::make_module,
}
#[cfg(feature = "tkinter")]
{
"_tkinter" => tkinter::make_module,
}
}
}
+1 -1
View File
@@ -1,6 +1,6 @@
// spell-checker:ignore abday abmon yesexpr noexpr CRNCYSTR RADIXCHAR AMPM THOUSEP
pub(crate) use _locale::make_module;
pub(crate) use _locale::module_def;
#[cfg(windows)]
#[repr(C)]
+1 -1
View File
@@ -1,6 +1,6 @@
// spell-checker:ignore ARMTHUMB
pub(crate) use _lzma::make_module;
pub(crate) use _lzma::module_def;
#[pymodule]
mod _lzma {
+1 -1
View File
@@ -1,4 +1,4 @@
pub(crate) use math::make_module;
pub(crate) use math::__module_def;
use crate::vm::{VirtualMachine, builtins::PyBaseExceptionRef};
+1 -1
View File
@@ -1,4 +1,4 @@
pub(crate) use _md5::make_module;
pub(crate) use _md5::module_def;
#[pymodule]
mod _md5 {
+1 -1
View File
@@ -1,6 +1,6 @@
// spell-checker:disable
//! mmap module
pub(crate) use mmap::make_module;
pub(crate) use mmap::module_def;
#[pymodule]
mod mmap {
+1 -1
View File
@@ -1,4 +1,4 @@
pub(crate) use _multiprocessing::make_module;
pub(crate) use _multiprocessing::module_def;
#[cfg(windows)]
#[pymodule]
+1 -1
View File
@@ -1,4 +1,4 @@
pub(crate) use opcode::make_module;
pub(crate) use opcode::__module_def;
#[pymodule]
mod opcode {
+1 -1
View File
@@ -1,6 +1,6 @@
// spell-checker:disable
pub(crate) use _overlapped::make_module;
pub(crate) use _overlapped::module_def;
#[allow(non_snake_case)]
#[pymodule]
+1 -1
View File
@@ -1,5 +1,5 @@
#[cfg(all(unix, not(target_os = "redox"), not(target_os = "android")))]
pub(crate) use _posixshmem::make_module;
pub(crate) use _posixshmem::module_def;
#[cfg(all(unix, not(target_os = "redox"), not(target_os = "android")))]
#[pymodule]
+1 -1
View File
@@ -22,7 +22,7 @@ use alloc::ffi::CString;
use core::{convert::Infallible as Never, ffi::CStr, marker::PhantomData, ops::Deref};
pub(crate) use _posixsubprocess::make_module;
pub(crate) use _posixsubprocess::module_def;
#[pymodule]
mod _posixsubprocess {
+1 -1
View File
@@ -5,7 +5,7 @@
//! Use this rust module to do byte packing:
//! <https://docs.rs/byteorder/1.2.6/byteorder/>
pub(crate) use _struct::make_module;
pub(crate) use _struct::module_def;
#[pymodule]
pub(crate) mod _struct {
+1 -1
View File
@@ -1,6 +1,6 @@
//! Random module.
pub(crate) use _random::make_module;
pub(crate) use _random::module_def;
#[pymodule]
mod _random {
+1 -1
View File
@@ -1,4 +1,4 @@
pub(crate) use re::make_module;
pub(crate) use re::module_def;
#[pymodule]
mod re {
+1 -1
View File
@@ -1,6 +1,6 @@
// spell-checker:disable
pub(crate) use resource::make_module;
pub(crate) use resource::module_def;
#[pymodule]
mod resource {
+1 -1
View File
@@ -1,4 +1,4 @@
pub(crate) use _scproxy::make_module;
pub(crate) use _scproxy::module_def;
#[pymodule]
mod _scproxy {
+1 -1
View File
@@ -1,4 +1,4 @@
pub(crate) use _sha1::make_module;
pub(crate) use _sha1::module_def;
#[pymodule]
mod _sha1 {
+1 -1
View File
@@ -1,4 +1,4 @@
pub(crate) use _sha3::make_module;
pub(crate) use _sha3::module_def;
#[pymodule]
mod _sha3 {
+1 -1
View File
@@ -25,7 +25,7 @@ mod compat;
// SSL exception types (shared with openssl backend)
mod error;
pub(crate) use _ssl::make_module;
pub(crate) use _ssl::module_def;
#[allow(non_snake_case)]
#[allow(non_upper_case_globals)]
+1 -1
View File
@@ -1,4 +1,4 @@
pub(crate) use _statistics::make_module;
pub(crate) use _statistics::module_def;
#[pymodule]
mod _statistics {
+1 -1
View File
@@ -1,4 +1,4 @@
pub(crate) use _suggestions::make_module;
pub(crate) use _suggestions::module_def;
#[pymodule]
mod _suggestions {
+1 -1
View File
@@ -1,6 +1,6 @@
// spell-checker:ignore logoption openlog setlogmask upto NDELAY ODELAY
pub(crate) use syslog::make_module;
pub(crate) use syslog::module_def;
#[pymodule(name = "syslog")]
mod syslog {
+1 -1
View File
@@ -1,6 +1,6 @@
// spell-checker:disable
pub(crate) use self::termios::make_module;
pub(crate) use self::termios::module_def;
#[pymodule]
mod termios {
+1 -1
View File
@@ -1,6 +1,6 @@
// spell-checker:ignore createcommand
pub(crate) use self::_tkinter::make_module;
pub(crate) use self::_tkinter::module_def;
#[pymodule]
mod _tkinter {
+1 -1
View File
@@ -1,4 +1,4 @@
pub(crate) use _uuid::make_module;
pub(crate) use _uuid::module_def;
#[pymodule]
mod _uuid {
+1 -1
View File
@@ -1,6 +1,6 @@
// spell-checker:ignore compressobj decompressobj zdict chunksize zlibmodule miniz chunker
pub(crate) use zlib::make_module;
pub(crate) use zlib::module_def;
#[pymodule]
mod zlib {
+1 -1
View File
@@ -2,7 +2,7 @@
//!
//! Implements the list of [builtin Python functions](https://docs.python.org/3/library/builtins.html).
use crate::{Py, VirtualMachine, builtins::PyModule, class::PyClassImpl};
pub(crate) use builtins::{__module_def, DOC};
pub(crate) use builtins::{DOC, module_def};
pub use builtins::{ascii, print, reversed};
#[pymodule]
+1 -3
View File
@@ -1,8 +1,6 @@
use crate::{Py, PyResult, VirtualMachine, builtins::PyModule, convert::ToPyObject};
pub(crate) use sys::{
__module_def, DOC, MAXSIZE, RUST_MULTIARCH, UnraisableHookArgsData, multiarch,
};
pub(crate) use sys::{DOC, MAXSIZE, RUST_MULTIARCH, UnraisableHookArgsData, module_def, multiarch};
#[pymodule(name = "_jit")]
mod sys_jit {
+2 -2
View File
@@ -159,8 +159,8 @@ impl VirtualMachine {
};
// Hard-core modules:
let builtins = new_module(stdlib::builtins::__module_def(&ctx));
let sys_module = new_module(stdlib::sys::__module_def(&ctx));
let builtins = new_module(stdlib::builtins::module_def(&ctx));
let sys_module = new_module(stdlib::sys::module_def(&ctx));
let import_func = ctx.none();
let profile_func = RefCell::new(ctx.none());