rython_stdpython
The default Python runtime library for the Rython compiler ecosystem. This crate provides a comprehensive, Python-compatible standard library implementation in Rust that serves as the runtime foundation for Python code compiled to Rust via the Rython toolchain.
Overview
rython_stdpython
is a complete Python runtime environment written in Rust that enables compiled Python code to access all Python built-ins, types, and standard operations without requiring any imports. It provides both std
and no_std
variants, making it suitable for everything from desktop applications to embedded systems.
Key Features
- Complete Python Built-ins: 40+ built-in functions implemented (
print
,len
,range
,enumerate
,zip
,min
,max
,sum
,all
,any
, etc.) - Full Type System: Python-compatible implementations of
str
,list
,dict
,tuple
,set
,int
,float
,bool
with all their methods - Generic Trait System: Flexible, zero-cost abstractions that work with any type implementing the appropriate trait
- Exception Handling: Complete Python exception hierarchy (
ValueError
,TypeError
,IndexError
, etc.) - Both std and no_std: Supports standard library environments and embedded/no_std targets
- Memory Safe: All operations maintain Rust's memory safety guarantees
- Performance Optimized: Native Rust implementations provide superior performance to interpreted Python
Architecture
This library uses a generic trait-based design that mirrors Python's built-in behavior:
Conversion Traits
PyAbs
: Generic absolute value (abs(-5i64)
,abs(-3.14f64)
)PyBool
: Generic boolean conversion (bool(42)
,bool("")
)PyInt
: Generic integer conversion (int("123")
,int(3.14)
)PyFloat
: Generic float conversion (float("3.14")
,float(42)
)PyToString
: Generic string conversion (str(123)
,str(true)
)PySum
: Generic summation (sum(&[1,2,3])
,sum(&pylist)
)
Runtime Traits
Len
: Universal length calculationTruthy
: Python-style truthiness evaluation
What's Implemented
Python Built-in Functions (40+)
✅ Math/Logic: abs()
, min()
, max()
, sum()
, all()
, any()
✅ Iteration: enumerate()
, zip()
, range()
, len()
✅ Type Conversion: bool()
, int()
, float()
, str()
, list()
, dict()
, tuple()
, set()
✅ Object Introspection: type()
, isinstance()
, hasattr()
, getattr()
, setattr()
, delattr()
, id()
, hash()
✅ Character/Unicode: ord()
, chr()
✅ I/O: print()
with full parameter support (std mode only)
Python Built-in Types with Complete Method Sets
PyStr (String Type)
✅ Core Methods: split()
, join()
, strip()
, lower()
, upper()
, replace()
✅ Search Methods: find()
, count()
, startswith()
, endswith()
✅ Formatting: format()
(basic implementation)
PyList (List Type)
✅ Modification: append()
, extend()
, insert()
, remove()
, pop()
, clear()
✅ Search/Sort: index()
, count()
, sort()
, reverse()
✅ Utilities: copy()
, indexing with get()
/set()
PyDictionary (Dictionary Type)
✅ Access: get()
, get_or_default()
, contains_key()
✅ Modification: set()
, pop()
, clear()
, update()
✅ Iteration: keys()
, values()
, items()
PyTuple (Tuple Type)
✅ Immutable sequence: Index access, slicing support
PySet (Set Type)
✅ Modification: add()
, remove()
, discard()
, clear()
✅ Set Operations: union()
, intersection()
, difference()
✅ Membership: contains()
Exception System
✅ Complete Exception Hierarchy: PyException
, ValueError
, TypeError
, IndexError
, KeyError
, AttributeError
, NameError
, ZeroDivisionError
, OverflowError
, RuntimeError
What's Not Implemented
❌ Advanced Python Features: Decorators, metaclasses, generators, async/await
❌ Complex Built-ins: exec()
, eval()
, compile()
, globals()
, locals()
❌ File I/O (no_std mode): File operations, directory handling
❌ Networking: Socket operations, HTTP clients
❌ Threading: Thread management, locks, synchronization
❌ Regular Expressions: re
module functionality
❌ Date/Time: datetime
, time
module functionality
❌ OS Interface: os
, sys
module functionality
❌ Import System: Dynamic module loading
Usage
Standard Library Mode (default)
[]
= "1.0"
No-Std Mode (embedded systems)
[]
= { = "1.0", = false, = ["nostd"] }
Example Usage
use *;
Integration with Rython
This crate serves as the runtime foundation for the entire Rython ecosystem:
- rythonc: The Python-to-Rust compiler generates code that calls these runtime functions
- python-ast-rs: AST code generation targets these built-in implementations
- python-mod-rs: Embedded Python modules depend on these built-ins
When Python code is compiled to Rust, it naturally maps to function calls in this library:
# Python code
=
=
// Generated Rust code
let mut my_list = from_vec;
let total = sum; // Uses PySum trait
print; // Uses PyToString trait
Building and Testing
# Standard library version
# No-std version
# Run specific test modules
License
This project is part of the Rython compiler ecosystem.