|
| 1 | +# GitHub Copilot Instructions for RustPython |
| 2 | + |
| 3 | +This document provides guidelines for working with GitHub Copilot when contributing to the RustPython project. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +RustPython is a Python 3 interpreter written in Rust, implementing Python 3.13.0+ compatibility. The project aims to provide: |
| 8 | + |
| 9 | +- A complete Python-3 environment entirely in Rust (not CPython bindings) |
| 10 | +- A clean implementation without compatibility hacks |
| 11 | +- Cross-platform support, including WebAssembly compilation |
| 12 | +- The ability to embed Python scripting in Rust applications |
| 13 | + |
| 14 | +## Repository Structure |
| 15 | + |
| 16 | +- `src/` - Top-level code for the RustPython binary |
| 17 | +- `vm/` - The Python virtual machine implementation |
| 18 | + - `builtins/` - Python built-in types and functions |
| 19 | + - `stdlib/` - Essential standard library modules implemented in Rust, required to run the Python core |
| 20 | +- `compiler/` - Python compiler components |
| 21 | + - `parser/` - Parser for converting Python source to AST |
| 22 | + - `core/` - Bytecode representation in Rust structures |
| 23 | + - `codegen/` - AST to bytecode compiler |
| 24 | +- `Lib/` - CPython's standard library in Python (copied from CPython) |
| 25 | +- `derive/` - Rust macros for RustPython |
| 26 | +- `common/` - Common utilities |
| 27 | +- `extra_tests/` - Integration tests and snippets |
| 28 | +- `stdlib/` - Non-essential Python standard library modules implemented in Rust (useful but not required for core functionality) |
| 29 | +- `wasm/` - WebAssembly support |
| 30 | +- `jit/` - Experimental JIT compiler implementation |
| 31 | +- `pylib/` - Python standard library packaging (do not modify this directory directly - its contents are generated automatically) |
| 32 | + |
| 33 | +## Important Development Notes |
| 34 | + |
| 35 | +### Running Python Code |
| 36 | + |
| 37 | +When testing Python code, always use RustPython instead of the standard `python` command: |
| 38 | + |
| 39 | +```bash |
| 40 | +# Use this instead of python script.py |
| 41 | +cargo run -- script.py |
| 42 | + |
| 43 | +# For interactive REPL |
| 44 | +cargo run |
| 45 | + |
| 46 | +# With specific features |
| 47 | +cargo run --features ssl |
| 48 | + |
| 49 | +# Release mode (recommended for better performance) |
| 50 | +cargo run --release -- script.py |
| 51 | +``` |
| 52 | + |
| 53 | +### Comparing with CPython |
| 54 | + |
| 55 | +When you need to compare behavior with CPython or run test suites: |
| 56 | + |
| 57 | +```bash |
| 58 | +# Use python command to explicitly run CPython |
| 59 | +python my_test_script.py |
| 60 | + |
| 61 | +# Run RustPython |
| 62 | +cargo run -- my_test_script.py |
| 63 | +``` |
| 64 | + |
| 65 | +### Working with the Lib Directory |
| 66 | + |
| 67 | +The `Lib/` directory contains Python standard library files copied from the CPython repository. Important notes: |
| 68 | + |
| 69 | +- These files should be edited very conservatively |
| 70 | +- Modifications should be minimal and only to work around RustPython limitations |
| 71 | +- Tests in `Lib/test` often use one of the following markers: |
| 72 | + - Add a `# TODO: RUSTPYTHON` comment when modifications are made |
| 73 | + - `unittest.skip("TODO: RustPython <reason>")` |
| 74 | + - `unittest.expectedFailure` with `# TODO: RUSTPYTHON <reason>` comment |
| 75 | + |
| 76 | +### Testing |
| 77 | + |
| 78 | +```bash |
| 79 | +# Run Rust unit tests |
| 80 | +cargo test --workspace --exclude rustpython_wasm |
| 81 | + |
| 82 | +# Run Python snippets tests |
| 83 | +cd extra_tests |
| 84 | +pytest -v |
| 85 | + |
| 86 | +# Run the Python test module |
| 87 | +cargo run --release -- -m test |
| 88 | +``` |
| 89 | + |
| 90 | +### Determining What to Implement |
| 91 | + |
| 92 | +Run `./whats_left.py` to get a list of unimplemented methods, which is helpful when looking for contribution opportunities. |
| 93 | + |
| 94 | +## Coding Guidelines |
| 95 | + |
| 96 | +### Rust Code |
| 97 | + |
| 98 | +- Follow the default rustfmt code style (`cargo fmt` to format) |
| 99 | +- Use clippy to lint code (`cargo clippy`) |
| 100 | +- Follow Rust best practices for error handling and memory management |
| 101 | +- Use the macro system (`pyclass`, `pymodule`, `pyfunction`, etc.) when implementing Python functionality in Rust |
| 102 | + |
| 103 | +### Python Code |
| 104 | + |
| 105 | +- Follow PEP 8 style for custom Python code |
| 106 | +- Use ruff for linting Python code |
| 107 | +- Minimize modifications to CPython standard library files |
| 108 | + |
| 109 | +## Integration Between Rust and Python |
| 110 | + |
| 111 | +The project provides several mechanisms for integration: |
| 112 | + |
| 113 | +- `pymodule` macro for creating Python modules in Rust |
| 114 | +- `pyclass` macro for implementing Python classes in Rust |
| 115 | +- `pyfunction` macro for exposing Rust functions to Python |
| 116 | +- `PyObjectRef` and other types for working with Python objects in Rust |
| 117 | + |
| 118 | +## Common Patterns |
| 119 | + |
| 120 | +### Implementing a Python Module in Rust |
| 121 | + |
| 122 | +```rust |
| 123 | +#[pymodule] |
| 124 | +mod mymodule { |
| 125 | + use rustpython_vm::prelude::*; |
| 126 | + |
| 127 | + #[pyfunction] |
| 128 | + fn my_function(value: i32) -> i32 { |
| 129 | + value * 2 |
| 130 | + } |
| 131 | + |
| 132 | + #[pyattr] |
| 133 | + #[pyclass(name = "MyClass")] |
| 134 | + #[derive(Debug, PyPayload)] |
| 135 | + struct MyClass { |
| 136 | + value: usize, |
| 137 | + } |
| 138 | + |
| 139 | + #[pyclass] |
| 140 | + impl MyClass { |
| 141 | + #[pymethod] |
| 142 | + fn get_value(&self) -> usize { |
| 143 | + self.value |
| 144 | + } |
| 145 | + } |
| 146 | +} |
| 147 | +``` |
| 148 | + |
| 149 | +### Adding a Python Module to the Interpreter |
| 150 | + |
| 151 | +```rust |
| 152 | +vm.add_native_module( |
| 153 | + "my_module_name".to_owned(), |
| 154 | + Box::new(my_module::make_module), |
| 155 | +); |
| 156 | +``` |
| 157 | + |
| 158 | +## Building for Different Targets |
| 159 | + |
| 160 | +### WebAssembly |
| 161 | + |
| 162 | +```bash |
| 163 | +# Build for WASM |
| 164 | +cargo build --target wasm32-wasip1 --no-default-features --features freeze-stdlib,stdlib --release |
| 165 | +``` |
| 166 | + |
| 167 | +### JIT Support |
| 168 | + |
| 169 | +```bash |
| 170 | +# Enable JIT support |
| 171 | +cargo run --features jit |
| 172 | +``` |
| 173 | + |
| 174 | +### SSL Support |
| 175 | + |
| 176 | +```bash |
| 177 | +# Enable SSL support |
| 178 | +cargo run --features ssl |
| 179 | +``` |
| 180 | + |
| 181 | +## Documentation |
| 182 | + |
| 183 | +- Check the [architecture document](architecture/architecture.md) for a high-level overview |
| 184 | +- Read the [development guide](DEVELOPMENT.md) for detailed setup instructions |
| 185 | +- Generate documentation with `cargo doc --no-deps --all` |
| 186 | +- Online documentation is available at [docs.rs/rustpython](https://docs.rs/rustpython/) |
0 commit comments