Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
module_def
  • Loading branch information
youknowone committed Jan 21, 2026
commit 32d2406fa8bd891cd88aa511eb183d5b3d17d1c5
4 changes: 2 additions & 2 deletions crates/derive-impl/src/pymodule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(|| {
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion crates/stdlib/src/binascii.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion crates/stdlib/src/bisect.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub(crate) use _bisect::make_module;
pub(crate) use _bisect::module_def;

#[pymodule]
mod _bisect {
Expand Down
2 changes: 1 addition & 1 deletion crates/stdlib/src/blake2.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// spell-checker:ignore usedforsecurity HASHXOF

pub(crate) use _blake2::make_module;
pub(crate) use _blake2::module_def;

#[pymodule]
mod _blake2 {
Expand Down
2 changes: 1 addition & 1 deletion crates/stdlib/src/bz2.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// spell-checker:ignore compresslevel

pub(crate) use _bz2::make_module;
pub(crate) use _bz2::module_def;

#[pymodule]
mod _bz2 {
Expand Down
2 changes: 1 addition & 1 deletion crates/stdlib/src/cmath.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub(crate) use cmath::make_module;
pub(crate) use cmath::module_def;

#[pymodule]
mod cmath {
Expand Down
2 changes: 1 addition & 1 deletion crates/stdlib/src/csv.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub(crate) use _csv::make_module;
pub(crate) use _csv::module_def;

#[pymodule]
mod _csv {
Expand Down
2 changes: 1 addition & 1 deletion crates/stdlib/src/faulthandler.rs
Original file line number Diff line number Diff line change
@@ -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")]
Expand Down
2 changes: 1 addition & 1 deletion crates/stdlib/src/fcntl.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// spell-checker:disable

pub(crate) use fcntl::make_module;
pub(crate) use fcntl::module_def;

#[pymodule]
mod fcntl {
Expand Down
14 changes: 8 additions & 6 deletions crates/stdlib/src/gc.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion crates/stdlib/src/grp.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// spell-checker:disable
pub(crate) use grp::make_module;
pub(crate) use grp::module_def;

#[pymodule]
mod grp {
Expand Down
2 changes: 1 addition & 1 deletion crates/stdlib/src/hashlib.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion crates/stdlib/src/json.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub(crate) use _json::make_module;
pub(crate) use _json::module_def;
mod machinery;

#[pymodule]
Expand Down
152 changes: 85 additions & 67 deletions crates/stdlib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,6 @@ 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()
}

pub fn get_module_inits() -> impl Iterator<Item = (Cow<'static, str>, StdlibInitFunc)> {
macro_rules! modules {
{
$(
Expand All @@ -132,7 +124,7 @@ pub fn get_module_inits() -> impl Iterator<Item = (Cow<'static, str>, StdlibInit
} => {{
[
$(
$(#[cfg($cfg)] (Cow::<'static, str>::from($key), Box::new($val) as StdlibInitFunc),)*
$(#[cfg($cfg)] (Cow::<'static, str>::from($key), $val as StdlibDefFunc),)*
)*
]
.into_iter()
Expand All @@ -141,109 +133,135 @@ pub fn get_module_inits() -> impl Iterator<Item = (Cow<'static, str>, StdlibInit
modules! {
#[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,
"_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,
"_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::make_module,
}
#[cfg(any(unix, windows, target_os = "wasi"))]
{
"select" => select::make_module,
"fcntl" => fcntl::module_def,
}
#[cfg(not(target_arch = "wasm32"))]
{
"_multiprocessing" => multiprocessing::make_module,
"_socket" => socket::make_module,
"_multiprocessing" => multiprocessing::module_def,
}
#[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,
"_lzma" => lzma::module_def,
}
#[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,
"_ssl" => ssl::module_def,
}
#[cfg(windows)]
{
"_overlapped" => overlapped::make_module,
"_overlapped" => overlapped::module_def,
}
// Unix-only
#[cfg(unix)]
{
"_posixsubprocess" => posixsubprocess::make_module,
"_posixsubprocess" => posixsubprocess::module_def,
}
#[cfg(all(unix, not(target_os = "redox"), not(target_os = "android")))]
{
"_posixshmem" => posixshmem::make_module,
"_posixshmem" => posixshmem::module_def,
}
#[cfg(any(unix, windows))]
{
"mmap" => mmap::make_module,
"mmap" => mmap::module_def,
}
#[cfg(all(unix, not(target_os = "redox")))]
{
"syslog" => syslog::make_module,
"resource" => resource::make_module,
"syslog" => syslog::module_def,
"resource" => resource::module_def,
}
#[cfg(all(unix, not(any(target_os = "ios", target_os = "redox"))))]
{
"termios" => termios::make_module,
"termios" => termios::module_def,
}
#[cfg(all(unix, not(any(target_os = "android", target_os = "redox"))))]
{
"grp" => grp::make_module,
"grp" => grp::module_def,
}
#[cfg(target_os = "macos")]
{
"_scproxy" => scproxy::make_module,
"_scproxy" => scproxy::module_def,
}
#[cfg(not(any(target_os = "android", target_os = "ios", target_os = "windows", target_arch = "wasm32", target_os = "redox")))]
{
"_uuid" => uuid::make_module,
"_uuid" => uuid::module_def,
}
#[cfg(not(any(target_os = "ios", target_arch = "wasm32")))]
{
"_locale" => locale::make_module,
"_locale" => locale::module_def,
}
#[cfg(feature = "tkinter")]
{
"_tkinter" => tkinter::make_module,
"_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 {
{
$(
#[cfg($cfg:meta)]
{ $( $key:expr => $val:expr),* $(,)? }
)*
} => {{
[
$(
$(#[cfg($cfg)] (Cow::<'static, str>::from($key), Box::new($val) as StdlibInitFunc),)*
)*
]
.into_iter()
}};
}
modules! {
#[cfg(all())]
{
"array" => array::make_module,
"_contextvars" => contextvars::make_module,
"pyexpat" => pyexpat::make_module,
"_sha256" => sha256::make_module,
"_sha512" => sha512::make_module,
"unicodedata" => unicodedata::make_module,
}
#[cfg(any(unix, windows, target_os = "wasi"))]
{
"select" => select::make_module,
}
#[cfg(not(target_arch = "wasm32"))]
{
"_socket" => socket::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-openssl"))]
{
"_ssl" => openssl::make_module,
}
}
}
2 changes: 1 addition & 1 deletion crates/stdlib/src/locale.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion crates/stdlib/src/lzma.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// spell-checker:ignore ARMTHUMB

pub(crate) use _lzma::make_module;
pub(crate) use _lzma::module_def;

#[pymodule]
mod _lzma {
Expand Down
2 changes: 1 addition & 1 deletion crates/stdlib/src/math.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub(crate) use math::make_module;
pub(crate) use math::__module_def;

use crate::vm::{VirtualMachine, builtins::PyBaseExceptionRef};

Expand Down
2 changes: 1 addition & 1 deletion crates/stdlib/src/md5.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub(crate) use _md5::make_module;
pub(crate) use _md5::module_def;

#[pymodule]
mod _md5 {
Expand Down
2 changes: 1 addition & 1 deletion crates/stdlib/src/mmap.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// spell-checker:disable
//! mmap module
pub(crate) use mmap::make_module;
pub(crate) use mmap::module_def;

#[pymodule]
mod mmap {
Expand Down
2 changes: 1 addition & 1 deletion crates/stdlib/src/multiprocessing.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub(crate) use _multiprocessing::make_module;
pub(crate) use _multiprocessing::module_def;

#[cfg(windows)]
#[pymodule]
Expand Down
2 changes: 1 addition & 1 deletion crates/stdlib/src/opcode.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub(crate) use opcode::make_module;
pub(crate) use opcode::__module_def;

#[pymodule]
mod opcode {
Expand Down
Loading