Implement `_stat` module by moreal · Pull Request #5847 · RustPython/RustPython
Walkthrough
A new stat module has been added to the standard library, providing file status constants and functions for platform compatibility. The module is registered in the virtual machine. Related test overrides marked as expected failures have been removed, and "FIRMLINK" was added to the spelling whitelist.
Changes
| File(s) | Change Summary |
|---|---|
| .cspell.json | Added "FIRMLINK" to the spelling whitelist. |
| Lib/test/test_stat.py | Removed several @expectedFailure test overrides from TestFilemodeCStat; retained conditional expected failure for test_link on Windows only. |
| vm/src/stdlib/mod.rs | Registered and imported the new stat module in the standard library initialization. |
| vm/src/stdlib/stat.rs | Added new stat module with file type constants, permission bits, utility functions, and exports. |
Sequence Diagram(s)
sequenceDiagram
participant User
participant VM
participant stat_module
User->>VM: import _stat
VM->>stat_module: initialize via make_module
stat_module-->>VM: module instance with constants/functions
User->>stat_module: call filemode(mode)
stat_module-->>User: return permission string
Poem
A stately new module hops in with glee,
With file bits and flags for all to see.
Old failing tests now hop away,
While "FIRMLINK" is spelled the rabbit way.
A whisker-twitch for Unix and Win,
Let the stat adventures now begin!
🐇✨
📜 Recent review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
.cspell.json(1 hunks)Lib/test/test_stat.py(1 hunks)vm/src/stdlib/mod.rs(2 hunks)vm/src/stdlib/stat.rs(1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
- vm/src/stdlib/mod.rs
- Lib/test/test_stat.py
🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: moreal
PR: RustPython/RustPython#5847
File: vm/src/stdlib/stat.rs:547-567
Timestamp: 2025-06-27T14:47:28.770Z
Learning: In RustPython's stat module implementation, platform-specific constants like SF_SUPPORTED and SF_SYNTHETIC should be conditionally declared only for the platforms where they're available (e.g., macOS), following CPython's approach of optional declaration rather than providing fallback values for other platforms.
Learnt from: moreal
PR: RustPython/RustPython#5847
File: vm/src/stdlib/stat.rs:547-567
Timestamp: 2025-06-27T14:47:28.770Z
Learning: In RustPython's stat module implementation, platform-specific constants like SF_SUPPORTED and SF_SYNTHETIC should be conditionally declared only for the platforms where they're available (e.g., macOS), following CPython's approach of optional declaration using #ifdef checks rather than providing fallback values for other platforms.
vm/src/stdlib/stat.rs (10)
Learnt from: moreal
PR: RustPython/RustPython#5847
File: vm/src/stdlib/stat.rs:547-567
Timestamp: 2025-06-27T14:47:28.770Z
Learning: In RustPython's stat module implementation, platform-specific constants like SF_SUPPORTED and SF_SYNTHETIC should be conditionally declared only for the platforms where they're available (e.g., macOS), following CPython's approach of optional declaration rather than providing fallback values for other platforms.
Learnt from: moreal
PR: RustPython/RustPython#5847
File: vm/src/stdlib/stat.rs:547-567
Timestamp: 2025-06-27T14:47:28.770Z
Learning: In RustPython's stat module implementation, platform-specific constants like SF_SUPPORTED and SF_SYNTHETIC should be conditionally declared only for the platforms where they're available (e.g., macOS), following CPython's approach of optional declaration using #ifdef checks rather than providing fallback values for other platforms.
Learnt from: CR
PR: RustPython/RustPython#0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-06-26T12:52:11.109Z
Learning: To add a Python module to the RustPython interpreter, use 'vm.add_native_module' with the module name and module factory.
Learnt from: CR
PR: RustPython/RustPython#0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-06-26T12:52:11.109Z
Learning: When implementing a Python module in Rust for RustPython, use the #[pymodule] attribute and expose functions and classes using #[pyfunction], #[pyclass], and #[pymethod] as appropriate.
Learnt from: CR
PR: RustPython/RustPython#0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-06-26T12:52:11.109Z
Learning: Minimize modifications to CPython standard library files in the Lib/ directory; bug fixes should be made through Rust code modifications whenever possible.
Learnt from: CR
PR: RustPython/RustPython#0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-06-26T12:52:11.109Z
Learning: When comparing behavior with CPython, use the 'python' command explicitly for CPython and 'cargo run --' for RustPython to avoid confusion.
Learnt from: CR
PR: RustPython/RustPython#0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-06-26T12:52:11.109Z
Learning: To find unimplemented methods and contribution opportunities, run './whats_left.py' in the RustPython repository.
Learnt from: CR
PR: RustPython/RustPython#0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-06-26T12:52:11.109Z
Learning: Follow Rust best practices for error handling and memory management in all RustPython Rust code.
Learnt from: CR
PR: RustPython/RustPython#0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-06-26T12:52:11.109Z
Learning: When testing Python code for RustPython, always use the RustPython interpreter (via 'cargo run -- script.py') instead of the standard 'python' command.
Learnt from: CR
PR: RustPython/RustPython#0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-06-26T12:52:11.109Z
Learning: Use the macro system (such as 'pyclass', 'pymodule', 'pyfunction') when implementing Python functionality in Rust for RustPython.
🪛 Biome (1.9.4)
.cspell.json
[error] 137-137: End of file expected
Use an array for a sequence of values: [1, 2]
(parse)
⏰ Context from checks skipped due to timeout of 90000ms (10)
- GitHub Check: Check Rust code with rustfmt and clippy
- GitHub Check: Run snippets and cpython tests (ubuntu-latest)
- GitHub Check: Check the WASM package and demo
- GitHub Check: Run snippets and cpython tests (windows-latest)
- GitHub Check: Run snippets and cpython tests (macos-latest)
- GitHub Check: Run rust tests (windows-latest)
- GitHub Check: Run snippets and cpython tests on wasm-wasi
- GitHub Check: Run rust tests (ubuntu-latest)
- GitHub Check: Run rust tests (macos-latest)
- GitHub Check: Ensure compilation on various targets
🔇 Additional comments (5)
vm/src/stdlib/stat.rs (3)
1-571: Excellent implementation of the stat module following RustPython conventions.This is a comprehensive implementation of the
_statmodule that closely follows CPython's behavior with proper platform-specific handling. The code structure, use of conditional compilation, and RustPython-specific attributes (#[pymodule],#[pyfunction],#[pyattr]) are all correctly implemented.Key strengths:
- Proper platform-specific type definitions and constants
- Correct implementation of file type test functions
- Well-structured filemode string generation
- Appropriate handling of unsupported platforms (Solaris/BSD) with stubs
- Follows learned best practices for optional declaration of platform-specific constants
553-566: Platform-specific constants correctly follow CPython's optional declaration approach.The implementation correctly declares
SF_SUPPORTEDandSF_SYNTHETIConly for macOS, following CPython's approach of optional declaration rather than providing fallback values for other platforms. This aligns with the established learnings and matches CPython's#ifdefpattern.
65-76: TODO comments appropriately document missing platform support.The placeholder constants and stub functions for Solaris (
S_IFDOOR,S_IFPORT) and BSD (S_IFWHT) are appropriately handled with clear TODO comments. This follows a reasonable incremental implementation approach..cspell.json (2)
136-137: Addition of "FIRMLINK" to spell check dictionary is correct.The addition of "FIRMLINK" to the words list properly supports the new
SF_FIRMLINKconstant introduced in the stat module. The JSON syntax appears correct with no trailing comma after the last array element.
137-137: Verify JSON syntax if static analysis error persists.The static analysis tool reports a JSON parse error, but the current code appears syntactically correct with no trailing comma. This may be a false positive from the tool.
#!/bin/bash # Verify JSON syntax is valid python -m json.tool .cspell.json > /dev/null && echo "JSON is valid" || echo "JSON syntax error detected"
✨ Finishing Touches
- 📝 Generate Docstrings
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.
🪧 Tips
Chat
There are 3 ways to chat with CodeRabbit:
- Review comments: Directly reply to a review comment made by CodeRabbit. Example:
I pushed a fix in commit <commit_id>, please review it.Explain this complex logic.Open a follow-up GitHub issue for this discussion.
- Files and specific lines of code (under the "Files changed" tab): Tag
@coderabbitaiin a new review comment at the desired location with your query. Examples:@coderabbitai explain this code block.@coderabbitai modularize this function.
- PR comments: Tag
@coderabbitaiin a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:@coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.@coderabbitai read src/utils.ts and explain its main purpose.@coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.@coderabbitai help me debug CodeRabbit configuration file.
Support
Need help? Create a ticket on our support page for assistance with any issues or questions.
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.
CodeRabbit Commands (Invoked using PR comments)
@coderabbitai pauseto pause the reviews on a PR.@coderabbitai resumeto resume the paused reviews.@coderabbitai reviewto trigger an incremental review. This is useful when automatic reviews are disabled for the repository.@coderabbitai full reviewto do a full review from scratch and review all the files again.@coderabbitai summaryto regenerate the summary of the PR.@coderabbitai generate docstringsto generate docstrings for this PR.@coderabbitai generate sequence diagramto generate a sequence diagram of the changes in this PR.@coderabbitai resolveresolve all the CodeRabbit review comments.@coderabbitai configurationto show the current CodeRabbit configuration for the repository.@coderabbitai helpto get help.
Other keywords and placeholders
- Add
@coderabbitai ignoreanywhere in the PR description to prevent this PR from being reviewed. - Add
@coderabbitai summaryto generate the high-level summary at a specific location in the PR description. - Add
@coderabbitaianywhere in the PR title to generate the title automatically.
CodeRabbit Configuration File (.coderabbit.yaml)
- You can programmatically configure CodeRabbit by adding a
.coderabbit.yamlfile to the root of your repository. - Please see the configuration documentation for more information.
- If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation:
# yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json
Documentation and Community
- Visit our Documentation for detailed information on how to use CodeRabbit.
- Join our Discord Community to get help, request features, and share feedback.
- Follow us on X/Twitter for updates and announcements.