Skip to content

Conversation

@youknowone
Copy link
Member

@youknowone youknowone commented Dec 8, 2025

Summary by CodeRabbit

  • New Features

    • Added Windows process spawning and execution functions with environment variable support
    • Enhanced directory creation with improved security descriptor handling on Windows
    • New process mode and I/O flag constants
  • Bug Fixes

    • Fixed exit code handling to properly support larger return values

✏️ Tip: You can customize this high-level summary in your review settings.

@youknowone youknowone marked this pull request as ready for review December 8, 2025 14:57
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 8, 2025

Walkthrough

Added Windows-specific feature flag to Cargo.toml for Windows-sys dependency. Extended nt.rs module with new process spawn/exec functions (spawnv, spawnve, execv, execve), constants for spawn modes and I/O flags, and a restructured mkdir API using MkdirArgs struct. Changed waitpid/wait return types to accommodate larger exit codes.

Changes

Cohort / File(s) Summary
Windows-sys feature flag
crates/vm/Cargo.toml
Added Win32_Security_Authorization feature to Windows-sys dependency under Windows target.
Process spawn/exec functions
crates/vm/src/stdlib/nt.rs
Added MSVC extern C bindings for wide-character process functions (_wexecv, _wexecve, _wspawnv, _wspawnve). Introduced public wrappers spawnv, spawnve, execv, execve with argument validation and environment string assembly.
Process mode and I/O constants
crates/vm/src/stdlib/nt.rs
Added constants: P_WAIT, P_NOWAIT, P_OVERLAY, P_NOWAITO, P_DETACH for spawn modes; O_SHORT_LIVED for I/O flags; EX_OK and TMP_MAX for compatibility.
waitpid/wait return type changes
crates/vm/src/stdlib/nt.rs
Changed waitpid and wait return types from (intptr_t, i32) to (intptr_t, u64) to accommodate larger exit codes.
mkdir API restructuring
crates/vm/src/stdlib/nt.rs
Introduced MkdirArgs struct with configurable mode (default 0o777) and directory ACL handling. Refactored mkdir function to accept MkdirArgs instead of individual parameters. Added SDDL-based security descriptor construction for mode 0o700.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~30 minutes

  • Argument and environment validation logic: Verify that non-empty path elements, non-empty first argv elements, and embedded null/equals-sign checks in environment keys are correctly enforced across all new spawn/exec functions.
  • Wide-character conversion: Confirm proper UTF-8 to UTF-16 conversions for paths and arguments, and null-termination handling for MSVC interop.
  • mkdir ACL handling: Review SDDL-based security descriptor construction for mode 0o700 and verify correct application to created directories.
  • Return type changes: Ensure u64 return type for exit codes does not cause issues with existing callers and correctly maps MSVC exit codes.

Possibly related PRs

Poem

🐰 Windows bindings now take flight,
With spawn and exec shining bright,
Wide chars dancing, paths aligned,
mkdir's ACLs by design—
Process launching, done just right! 🪟✨

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main additions: Windows support for execv, spawnv functions and wait return type changes.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (3)
crates/vm/src/stdlib/nt.rs (3)

435-441: Inconsistent error message format.

The error messages here use a different format ("execve: argv...") compared to execv and spawnv which use "execv() arg 2...". Consider aligning for consistency.

         let first = argv
             .first()
-            .ok_or_else(|| vm.new_value_error("execve: argv must not be empty"))?;
+            .ok_or_else(|| vm.new_value_error("execve() arg 2 must not be empty"))?;
 
         if first.is_empty() {
-            return Err(vm.new_value_error("execve: argv first element cannot be empty"));
+            return Err(vm.new_value_error("execve() arg 2 first element cannot be empty"));
         }

266-267: Consider extracting duplicated helper.

The make_widestring closure is duplicated across spawnv, spawnve, execv, and execve. Consider extracting it to a shared helper function to reduce duplication.

fn make_widestring(s: &str, vm: &VirtualMachine) -> PyResult<widestring::WideCString> {
    widestring::WideCString::from_os_str(s).map_err(|err| err.to_pyexception(vm))
}

Also applies to: 309-310, 384-385, 425-426


333-358: Consider extracting environment building logic.

The environment dictionary to wide-string conversion (including validation for null characters and = in keys) is duplicated between spawnve and execve. A shared helper would reduce duplication.

Also applies to: 449-474

📜 Review details

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2a42535 and 25d77a1.

⛔ Files ignored due to path filters (1)
  • Lib/test/test_os.py is excluded by !Lib/**
📒 Files selected for processing (2)
  • crates/vm/Cargo.toml (1 hunks)
  • crates/vm/src/stdlib/nt.rs (6 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.rs

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

**/*.rs: Follow the default rustfmt code style by running cargo fmt to format Rust code
Always run clippy to lint Rust code (cargo clippy) before completing tasks and fix any warnings or lints introduced by changes
Follow Rust best practices for error handling and memory management
Use the macro system (pyclass, pymodule, pyfunction, etc.) when implementing Python functionality in Rust

Files:

  • crates/vm/src/stdlib/nt.rs
🧬 Code graph analysis (1)
crates/vm/src/stdlib/nt.rs (3)
crates/vm/src/stdlib/posix.rs (8)
  • waitpid (1705-1710)
  • wait (1713-1715)
  • s (1917-1918)
  • s (2311-2311)
  • v (2410-2410)
  • v (2413-2413)
  • v (2419-2419)
  • v (2422-2422)
crates/vm/src/stdlib/os.rs (3)
  • errno_err (39-41)
  • path (485-487)
  • std (1060-1060)
crates/vm/src/windows.rs (3)
  • std (407-407)
  • std (460-460)
  • std (471-471)
🔇 Additional comments (8)
crates/vm/Cargo.toml (1)

132-132: LGTM!

The Win32_Security_Authorization feature addition is correctly placed and necessary for the new SDDL-based security descriptor functionality in mkdir.

crates/vm/src/stdlib/nt.rs (7)

165-176: LGTM!

The cast chain (status as u32) as u64 correctly preserves the full bit pattern for large Windows exit codes (e.g., 0xC000013A), and the left-shift by 8 properly aligns with POSIX waitpid format.


178-182: LGTM!

Consistent return type with the updated waitpid.


239-254: LGTM!

The extern declarations match the MSVC CRT signatures for wide-string spawn/exec functions.


256-296: LGTM!

The implementation correctly validates arguments, builds the null-terminated wide string array, and ensures the argv vector stays alive during the unsafe call. The error messages use correct 1-indexed argument numbers matching CPython convention.


375-413: LGTM!

The execv implementation properly validates arguments and uses OsPath for path handling. Error messages correctly reference "arg 2" for argv.


60-62: TMP_MAX should match MSVC's standard value or document the intentional divergence.

MSVC's <stdio.h> defines TMP_MAX as 32767, not i32::MAX (2147483647). If the intent is to match CPython's Windows behavior, add a comment explaining this choice; otherwise, consider aligning with the standard MSVC value to avoid unexpected behavioral differences for code relying on this constant.


695-752: Implementation correctly mirrors CPython's Windows mkdir(mode=0o700) behavior.

The SDDL string is accurate: it creates a protected discretionary ACL (D:P) granting Full Access (FA) with inheritance (OICI) to SYSTEM (SY), Administrators (BA), and the directory owner (OW)—matching the security intent of 0o700 mode on Unix systems. Memory management is sound: the security descriptor is properly freed via LocalFree in all code paths.

Comment on lines +319 to +325
let first = argv
.first()
.ok_or_else(|| vm.new_value_error("spawnve() arg 2 cannot be empty"))?;

if first.is_empty() {
return Err(vm.new_value_error("spawnve() arg 2 first element cannot be empty"));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Incorrect argument index in error messages.

For spawnve(mode, path, argv, env), the argv parameter is argument 3, not argument 2. This is inconsistent with spawnv which correctly uses "arg 3".

         let first = argv
             .first()
-            .ok_or_else(|| vm.new_value_error("spawnve() arg 2 cannot be empty"))?;
+            .ok_or_else(|| vm.new_value_error("spawnve() arg 3 cannot be empty"))?;
 
         if first.is_empty() {
-            return Err(vm.new_value_error("spawnve() arg 2 first element cannot be empty"));
+            return Err(vm.new_value_error("spawnve() arg 3 first element cannot be empty"));
         }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
let first = argv
.first()
.ok_or_else(|| vm.new_value_error("spawnve() arg 2 cannot be empty"))?;
if first.is_empty() {
return Err(vm.new_value_error("spawnve() arg 2 first element cannot be empty"));
}
let first = argv
.first()
.ok_or_else(|| vm.new_value_error("spawnve() arg 3 cannot be empty"))?;
if first.is_empty() {
return Err(vm.new_value_error("spawnve() arg 3 first element cannot be empty"));
}
🤖 Prompt for AI Agents
In crates/vm/src/stdlib/nt.rs around lines 319-325, the two error messages
mistakenly refer to `argv` as "arg 2" for spawnve(); update both messages to
reference "arg 3" instead so they match the spawnv wording and correctly reflect
the parameter order: change "spawnve() arg 2 cannot be empty" to "spawnve() arg
3 cannot be empty" and "spawnve() arg 2 first element cannot be empty" to
"spawnve() arg 3 first element cannot be empty".

@youknowone youknowone merged commit bf565e9 into RustPython:main Dec 8, 2025
13 checks passed
@youknowone youknowone deleted the windows-os branch December 8, 2025 15:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant