-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Implement winapi.GetLongPathName #6525
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
📝 WalkthroughWalkthroughAdds two _winapi module functions, Changes
Sequence Diagram(s)sequenceDiagram
participant Py as Python caller
participant VM as Rust VirtualMachine
participant Impl as get_path_name_impl
participant WinAPI as Windows API (GetShortPathNameW / GetLongPathNameW)
Py->>VM: call GetShortPathName(path) / GetLongPathName(path)
VM->>Impl: convert path -> UTF-16, invoke API (size query)
Impl->>WinAPI: call API with buffer size 0
WinAPI-->>Impl: required size (n) or 0 on error
Impl->>WinAPI: allocate buffer of size n, call API to fill buffer
WinAPI-->>Impl: filled UTF-16 data or 0 on error
Impl-->>VM: convert UTF-16 -> Wtf-8 string or map error via vm.new_last_os_error()
VM-->>Py: return PyStrRef or raise OS error
Estimated code review effort🎯 2 (Simple) | ⏱️ ~15 minutes Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: Path: .coderabbit.yml Review profile: CHILL Plan: Pro ⛔ Files ignored due to path filters (1)
📒 Files selected for processing (1)
🧰 Additional context used📓 Path-based instructions (1)**/*.rs📄 CodeRabbit inference engine (.github/copilot-instructions.md)
Files:
🧬 Code graph analysis (1)crates/vm/src/stdlib/winapi.rs (1)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (11)
🔇 Additional comments (3)
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. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (2)
crates/vm/src/stdlib/winapi.rs (2)
680-708: Consider usingWtf8Buffor proper WTF-8/surrogate handling.The function currently uses
String::from_utf16(line 707), butLCMapStringEx(line 630) usesWtf8Buf::from_wideto properly handle unpaired surrogates in WTF-8. Since the input isPyStrRefwhich supports WTF-8, the output should preserve this capability for consistency and robustness. While Windows paths with unpaired surrogates are rare, they are technically valid.Additionally, this function has nearly identical implementation to
GetLongPathNamebelow—consider extracting a generic helper to reduce duplication.🔎 Proposed refactor using Wtf8Buf pattern
+ use rustpython_common::wtf8::Wtf8Buf; + /// GetShortPathName - Return the short version of the provided path. #[pyfunction] - fn GetShortPathName(path: PyStrRef, vm: &VirtualMachine) -> PyResult<String> { + fn GetShortPathName(path: PyStrRef, vm: &VirtualMachine) -> PyResult<PyStrRef> { use windows_sys::Win32::Storage::FileSystem::GetShortPathNameW; let path_wide = path.as_wtf8().to_wide_with_nul(); // First call to get required buffer size let size = unsafe { GetShortPathNameW(path_wide.as_ptr(), null_mut(), 0) }; if size == 0 { return Err(vm.new_last_os_error()); } // Second call to get the actual short path let mut buffer: Vec<u16> = vec![0; size as usize]; let result = unsafe { GetShortPathNameW(path_wide.as_ptr(), buffer.as_mut_ptr(), buffer.len() as u32) }; if result == 0 { return Err(vm.new_last_os_error()); } // Truncate to actual length (excluding null terminator) buffer.truncate(result as usize); - String::from_utf16(&buffer).map_err(|e| vm.new_runtime_error(e.to_string())) + let result_str = Wtf8Buf::from_wide(&buffer); + Ok(vm.ctx.new_str(result_str)) }
710-737: Consider usingWtf8Buffor proper WTF-8/surrogate handling.The function currently uses
String::from_utf16(line 736), butLCMapStringEx(line 630) usesWtf8Buf::from_wideto properly handle unpaired surrogates in WTF-8. Since the input isPyStrRefwhich supports WTF-8, the output should preserve this capability for consistency and robustness. While Windows paths with unpaired surrogates are rare, they are technically valid.Additionally, this function has nearly identical implementation to
GetShortPathNameabove—consider extracting a generic helper to reduce duplication.🔎 Proposed refactor using Wtf8Buf pattern
+ use rustpython_common::wtf8::Wtf8Buf; + /// GetLongPathName - Return the long version of the provided path. #[pyfunction] - fn GetLongPathName(path: PyStrRef, vm: &VirtualMachine) -> PyResult<String> { + fn GetLongPathName(path: PyStrRef, vm: &VirtualMachine) -> PyResult<PyStrRef> { use windows_sys::Win32::Storage::FileSystem::GetLongPathNameW; let path_wide = path.as_wtf8().to_wide_with_nul(); // First call to get required buffer size let size = unsafe { GetLongPathNameW(path_wide.as_ptr(), null_mut(), 0) }; if size == 0 { return Err(vm.new_last_os_error()); } // Second call to get the actual long path let mut buffer: Vec<u16> = vec![0; size as usize]; let result = unsafe { GetLongPathNameW(path_wide.as_ptr(), buffer.as_mut_ptr(), buffer.len() as u32) }; if result == 0 { return Err(vm.new_last_os_error()); } // Truncate to actual length (excluding null terminator) buffer.truncate(result as usize); - String::from_utf16(&buffer).map_err(|e| vm.new_runtime_error(e.to_string())) + let result_str = Wtf8Buf::from_wide(&buffer); + Ok(vm.ctx.new_str(result_str)) }
📜 Review details
Configuration used: Path: .coderabbit.yml
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (1)
Lib/test/test_winapi.pyis excluded by!Lib/**
📒 Files selected for processing (1)
crates/vm/src/stdlib/winapi.rs
🧰 Additional context used
📓 Path-based instructions (1)
**/*.rs
📄 CodeRabbit inference engine (.github/copilot-instructions.md)
**/*.rs: Follow the default rustfmt code style by runningcargo fmtto 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/winapi.rs
🧬 Code graph analysis (1)
crates/vm/src/stdlib/winapi.rs (2)
crates/vm/src/stdlib/nt.rs (2)
path(1067-1067)path(1071-1071)crates/vm/src/stdlib/os.rs (1)
path(584-586)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: Run snippets and cpython tests (windows-latest)
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.