Refine documentation in interpreter.rs by 77axel · Pull Request #6566 · RustPython/RustPython
🏁 Script executed:
# Search for all _stdlib_dir references rg -n "_stdlib_dir" --type=rust -C3
Repository: RustPython/RustPython
Length of output: 897
🏁 Script executed:
# Look for where setup_frozen_stdlib is called to understand initialization order rg -n "setup_frozen_stdlib" --type=rust -B2 -A2
Repository: RustPython/RustPython
Length of output: 624
🏁 Script executed:
# Check the VirtualMachine initialization and sys_module setup rg -n "sys_module" src/interpreter.rs -B1 -A1 | head -50
Repository: RustPython/RustPython
Length of output: 259
🏁 Script executed:
# Look at the full context around line 197 to see initialization order sed -n '180,210p' src/interpreter.rs
Repository: RustPython/RustPython
Length of output: 1046
🏁 Script executed:
# Check if sys_module is created before or after setup_frozen_stdlib rg -n "sys_module.*=" src/interpreter.rs | head -20
Repository: RustPython/RustPython
Length of output: 47
🏁 Script executed:
# Look for any module initialization that might touch _stdlib_dir rg -n "def.*_stdlib_dir|_stdlib_dir.*=" --type=python
Repository: RustPython/RustPython
Length of output: 95
🏁 Script executed:
# Search for where sys module is initially created/initialized rg -n "sys_module" src/interpreter.rs | head -30
Repository: RustPython/RustPython
Length of output: 155
🏁 Script executed:
# Look for VirtualMachine::new to understand initialization order ast-grep --pattern 'impl VirtualMachine { $$$ pub fn new($_) { $$$ } $$$ }'
Repository: RustPython/RustPython
Length of output: 47
🏁 Script executed:
# Search for any code path that could set _stdlib_dir before setup_frozen_stdlib rg -n "set_attr.*_stdlib_dir|_stdlib_dir" src/
Repository: RustPython/RustPython
Length of output: 401
🏁 Script executed:
# Look for VirtualMachine creation and sys_module initialization rg -n "sys_module\s*=" src/interpreter.rs -B2 -A2
Repository: RustPython/RustPython
Length of output: 47
🏁 Script executed:
# Find where init_stdlib is called to understand the full initialization sequence rg -n "init_stdlib" --type=rust -B3 -A3
Repository: RustPython/RustPython
Length of output: 5257
🏁 Script executed:
# Check if there's any Python-level code or module that might set _stdlib_dir fd --type f -e py | xargs rg "_stdlib_dir" 2>/dev/null || echo "No Python files found with _stdlib_dir"
Repository: RustPython/RustPython
Length of output: 103
🏁 Script executed:
# Look for VirtualMachine::new and sys module initialization rg -n "fn new\(" src/interpreter.rs crates/vm/src/vm/mod.rs -A 50 | grep -A 50 "impl.*VirtualMachine" | head -100
Repository: RustPython/RustPython
Length of output: 47
🏁 Script executed:
# Search in vm crate for sys_module creation rg -n "sys_module\s*:" crates/vm/src/ -B2 -A2
Repository: RustPython/RustPython
Length of output: 720
🏁 Script executed:
# Look at the actual flow: when is sys module initialized relative to init_stdlib grep -n "pub fn interpreter\|init_stdlib\|init_hook" src/interpreter.rs | head -20
Repository: RustPython/RustPython
Length of output: 628
🏁 Script executed:
# Look at the Interpreter creation flow to see when sys_module is created rg -n "pub fn interpreter" src/interpreter.rs -A 20
Repository: RustPython/RustPython
Length of output: 814
🏁 Script executed:
# Find where VirtualMachine is constructed rg -n "VirtualMachine\s*{" crates/vm/src/vm/mod.rs -B5 -A20 | head -80
Repository: RustPython/RustPython
Length of output: 164
🏁 Script executed:
# Look for sys module initialization in new() rg -n "sys_module.*PyModule\|fn new\(" crates/vm/src/vm/mod.rs -A 30 | head -100
Repository: RustPython/RustPython
Length of output: 47
🏁 Script executed:
# Find Interpreter::with_init to see when sys_module is created rg -n "with_init\|pub fn with_init" crates/vm/src/ --type=rust -A 30 | head -100
Repository: RustPython/RustPython
Length of output: 47
🏁 Script executed:
# Look for sys module initialization/creation rg -n "PyModule::new\(.*sys\|sys.*PyModule" crates/vm/src/ --type=rust -B2 -A2
Repository: RustPython/RustPython
Length of output: 47
🏁 Script executed:
# Check if sys module is set up before or after hooks are called rg -n "pub fn with_init" crates/vm/src/ -A 50
Repository: RustPython/RustPython
Length of output: 3824
🏁 Script executed:
# Look at VirtualMachine::new to see when sys_module is created rg -n "impl VirtualMachine" crates/vm/src/vm/mod.rs -A 100 | grep -A 50 "pub fn new"
Repository: RustPython/RustPython
Length of output: 47
🏁 Script executed:
# Find the VirtualMachine::new implementation sed -n '500,700p' crates/vm/src/vm/mod.rs | head -150
Repository: RustPython/RustPython
Length of output: 5630
🏁 Script executed:
# Look for the initialize method to see what it does rg -n "pub fn initialize\(" crates/vm/src/vm/mod.rs -A 30
Repository: RustPython/RustPython
Length of output: 47
🏁 Script executed:
# Find VirtualMachine::new rg -n "fn new\(" crates/vm/src/vm/mod.rs -A 40
Repository: RustPython/RustPython
Length of output: 1734
🏁 Script executed:
# Find the initialize method rg -n "fn initialize\(" crates/vm/src/vm/mod.rs -A 50
Repository: RustPython/RustPython
Length of output: 2578
🏁 Script executed:
# Look at stdlib::sys::init_module to see if it sets _stdlib_dir rg -n "pub fn init_module\|_stdlib_dir" crates/stdlib/src/sys.rs -B2 -A2
Repository: RustPython/RustPython
Length of output: 129
🏁 Script executed:
# Check what attributes stdlib::sys::init_module actually sets rg -n "def __module_def\|init_module" crates/stdlib/src/sys.rs -A 80 | head -150
Repository: RustPython/RustPython
Length of output: 129
🏁 Script executed:
# Find where sys module is defined fd "sys.rs" --type f
Repository: RustPython/RustPython
Length of output: 92
🏁 Script executed:
# Search for sys module definition rg -n "stdlib.*sys\|pub fn init_module.*sys" --type=rust | head -20
Repository: RustPython/RustPython
Length of output: 47
🏁 Script executed:
# Look at initialization order more carefully sed -n '280,320p' crates/vm/src/vm/mod.rs
Repository: RustPython/RustPython
Length of output: 1877
🏁 Script executed:
# Check stdlib::sys::init_module to see what it does rg -n "pub fn init_module" crates/vm/src/stdlib/sys.rs -A 100
Repository: RustPython/RustPython
Length of output: 47
🏁 Script executed:
# Search for _stdlib_dir in sys.rs rg -n "_stdlib_dir" crates/vm/src/stdlib/sys.rs
Repository: RustPython/RustPython
Length of output: 47
🏁 Script executed:
# Look at the full sys.rs file to understand initialization
head -100 crates/vm/src/stdlib/sys.rsRepository: RustPython/RustPython
Length of output: 3318
🏁 Script executed:
# Check if init_module sets _stdlib_dir or any similar attribute cat crates/vm/src/stdlib/sys.rs | head -200
Repository: RustPython/RustPython
Length of output: 6292
🏁 Script executed:
# Look at the full sys module to see if _stdlib_dir is anywhere
wc -l crates/vm/src/stdlib/sys.rsRepository: RustPython/RustPython
Length of output: 97
🏁 Script executed:
# Search the entire file for _stdlib_dir grep -n "_stdlib_dir" crates/vm/src/stdlib/sys.rs
Repository: RustPython/RustPython
Length of output: 47
🏁 Script executed:
# Get a sense of the file structure
tail -100 crates/vm/src/stdlib/sys.rsRepository: RustPython/RustPython
Length of output: 2953
🏁 Script executed:
# Check if any other code sets _stdlib_dir before setup_frozen_stdlib rg -n "set_attr.*_stdlib_dir|_stdlib_dir.*set" --type=rust
Repository: RustPython/RustPython
Length of output: 164
🏁 Script executed:
# Check the initialization sequence in detail - get the full setup_frozen_stdlib and init_stdlib flow sed -n '190,225p' src/interpreter.rs
Repository: RustPython/RustPython
Length of output: 1265
🏁 Script executed:
# Verify the timing: check if any init_hook before setup_frozen_stdlib could set _stdlib_dir rg -n "init_hook\|setup_frozen_stdlib\|init_stdlib" src/interpreter.rs | head -30
Repository: RustPython/RustPython
Length of output: 47
The assert! on _stdlib_dir is safe and protected by initialization order.
The sys module is created as an empty struct in VirtualMachine::new(), and setup_frozen_stdlib() runs as an init hook before stdlib::sys::init_module() is called in vm.initialize(). Since no code path sets _stdlib_dir before this function, the attribute will not exist at the time of the assertion.
However, the current assert! panics without context. Consider replacing it with a more informative check:
This provides better debugging information if the initialization invariant is ever violated.