Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
e9a70c7
Update dependencies in Cargo.lock and add Cargo.lock to .gitignore
shawhanken Dec 18, 2025
68da611
Merge branch 'main' of https://github.com/yusufyian/RustPython
shawhanken Dec 18, 2025
1bf2bdf
Update dependencies in Cargo.lock and add Cargo.lock to .gitignore
shawhanken Dec 19, 2025
1726b36
Add additional reference files to .gitignore
shawhanken Dec 19, 2025
fd57302
Update .gitignore to exclude additional files and improve dependency …
shawhanken Dec 19, 2025
ecfabb8
Merge branch 'RustPython:main' into main
yusufyian Dec 19, 2025
bc9b80a
Merge branch 'main' of https://github.com/yusufyian/RustPython
shawhanken Dec 19, 2025
1a1c97a
Add checkpoint functionality to VirtualMachine
shawhanken Dec 19, 2025
3997507
Add checkpoint request handling and update checkpoint functionality
shawhanken Dec 19, 2025
551d025
Enhance demo script with additional print statements for debugging
shawhanken Dec 19, 2025
665790f
Update demo user and enhance README with testing instructions
shawhanken Dec 24, 2025
c2edc6b
Update .gitignore to include demo files
shawhanken Dec 24, 2025
4dc6120
Update .gitignore and demo script for type checking
shawhanken Dec 25, 2025
9ac5e54
Rename RustPython binary to "pvm" in Cargo.toml and update demo.py fo…
yusufyian Dec 26, 2025
8ea2822
Rename RustPython binary to 'pvm' in Cargo.toml and update demo.py fo…
yusufyian Dec 26, 2025
66dc584
Update README and test script to reflect binary name change from 'rus…
yusufyian Dec 29, 2025
6c26391
Refactor demo.py for improved clarity and structure in checkpointing …
yusufyian Dec 29, 2025
a1c1891
Update demo.py and README for financial trading scenario simulation
yusufyian Dec 29, 2025
f41b080
Enhance demo.py with additional trading scenario features and update …
yusufyian Dec 29, 2025
5f547a5
Implement PVM host and runtime modules with initial configurations
yusufyian Dec 29, 2025
3ed0799
Update various files for improved functionality and clarity
yusufyian Dec 29, 2025
9704677
Enhance VM functionality and code clarity
yusufyian Dec 29, 2025
965b201
Enhance demo script and update .gitignore
yusufyian Dec 30, 2025
9f354c2
Update .gitignore to include all reference files and remove specific …
yusufyian Dec 30, 2025
b50f1f3
Remove obsolete reference files for PVM integration and continuation …
yusufyian Dec 30, 2025
f8fc889
Remove obsolete demo files for checkpoint/resume functionality
yusufyian Dec 30, 2025
26ac488
Remove obsolete binary snapshot file for demo functionality
yusufyian Dec 30, 2025
b2ba6ea
Remove obsolete test script for checkpoint/resume functionality
yusufyian Dec 30, 2025
2dfed2d
Refactor comments in state_store.py for clarity and consistency
yusufyian Dec 30, 2025
d8aabdd
Enhance checkpoint functionality and update .gitignore
yusufyian Dec 30, 2025
6d54427
Update source location handling in compiler-source
yusufyian Dec 30, 2025
a1a7ec6
Refactor checkpoint and snapshot handling for improved functionality
yusufyian Dec 30, 2025
3620c13
Update version formatting in version.rs to reflect PVM 0.0.2 integration
yusufyian Dec 31, 2025
fe31a3d
Add PVM versioning support and update build script
yusufyian Dec 31, 2025
8eaf89f
Implement multi-frame checkpoint support and enhance stack management
yusufyian Dec 31, 2025
5544761
Enhance checkpoint functionality and update demo scripts
yusufyian Dec 31, 2025
13117d6
Add support for enumerate, zip, map, and filter in snapshot handling
yusufyian Dec 31, 2025
63c44b5
Enhance checkpoint and block stack management in VM
yusufyian Dec 31, 2025
c6806af
Add support for ListIterator and RangeIterator in snapshot handling
yusufyian Jan 3, 2026
9844d0d
Add comprehensive demo snapshot to .gitignore
yusufyian Jan 3, 2026
a07fed6
Remove Cargo.lock file to prevent unnecessary tracking of dependencies
yusufyian Jan 5, 2026
06e9e6a
Add error handling and execution options in PVM runtime
yusufyian Jan 5, 2026
072c0cc
Add determinism support and enhance error handling in PVM runtime
yusufyian Jan 8, 2026
5e01466
Enhance PVM runtime with import tracing and version updates
yusufyian Jan 8, 2026
b9dc39d
Add new business scenario demos to README
yusufyian Jan 8, 2026
d5afdde
Fix formatting issue in escrow_marketplace_demo.py by adding a newlin…
yusufyian Jan 12, 2026
9f3c462
Enhance PVM runtime with continuation support and new features
yusufyian Jan 18, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Enhance checkpoint functionality and update .gitignore
- Added a new entry to `.gitignore` for the `demo.rpsnap` file to improve source management.
- Refactored `checkpoint.rs` to streamline checkpoint saving and loading processes, including the introduction of `save_checkpoint_bytes_from_exec` for better data handling.
- Updated `save_checkpoint_from_exec` to accept a `code` parameter, enhancing flexibility in checkpoint management.
- Marked several functions as `#[allow(dead_code)]` to facilitate future development without immediate usage requirements.
  • Loading branch information
yusufyian committed Dec 30, 2025
commit d8aabddd5d9f095d709e9d0dca12cd997ae2d007
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ Lib/test/data/*
Cargo.lock
refs/*
.gitignore
Comment on lines +25 to +35
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Duplicate entry and self-ignore issue.

  1. Cargo.lock appears twice (lines 25 and 33) - remove one to avoid redundancy.
  2. .gitignore ignoring itself (line 35) is almost certainly unintended. This would cause the .gitignore file to be tracked initially but ignored for future modifications, leading to confusion when changes to ignore rules aren't picked up.
🔧 Suggested fix
 Cargo.lock

-
 Lib/site-packages/*
 !Lib/site-packages/README.txt
 Lib/test/data/*
 !Lib/test/data/README

-Cargo.lock
 refs/*
-.gitignore
 examples/breakpoint_resume_demo/demo.rpsnap
📝 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
Cargo.lock
Lib/site-packages/*
!Lib/site-packages/README.txt
Lib/test/data/*
!Lib/test/data/README
Cargo.lock
refs/*
.gitignore
Cargo.lock
Lib/site-packages/*
!Lib/site-packages/README.txt
Lib/test/data/*
!Lib/test/data/README
refs/*
🤖 Prompt for AI Agents
In @.gitignore around lines 25 - 35, Remove the duplicate "Cargo.lock" entry and
delete the self-ignore ".gitignore" entry from the .gitignore file: remove the
redundant "Cargo.lock" line and the line that lists ".gitignore" so the
gitignore file is not ignored itself and the lockfile is only listed once.

examples/breakpoint_resume_demo/demo.rpsnap
3 changes: 2 additions & 1 deletion crates/vm/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ impl Frame {
Ok(state.stack.iter().cloned().collect())
}

#[allow(dead_code)]
pub(crate) fn restore_stack(
&self,
stack: Vec<PyObjectRef>,
Expand Down Expand Up @@ -458,7 +459,7 @@ impl ExecutingFrame<'_> {
if let Some(path) = maybe_checkpoint_request(vm, op, idx as u32) {
let source_path = self.code.source_path.as_str();
let lasti = self.lasti();
checkpoint::save_checkpoint_from_exec(vm, source_path, lasti, self.globals, &path)?;
checkpoint::save_checkpoint_from_exec(vm, source_path, lasti, &self.code, self.globals, &path)?;
std::process::exit(0);
}
}
Expand Down
243 changes: 71 additions & 172 deletions crates/vm/src/vm/checkpoint.rs
Original file line number Diff line number Diff line change
@@ -1,82 +1,44 @@
use crate::{
PyObjectRef, PyPayload, PyResult, VirtualMachine,
builtins::{PyBytesRef, PyDictRef},
compiler,
PyPayload, PyResult, VirtualMachine,
builtins::{PyDictRef, code::PyCode},
convert::TryFromObject,
frame::FrameRef,
scope::Scope,
vm::snapshot,
};
use crate::AsObject;
use crate::bytecode;
use crate::builtins::function::PyFunction;
use std::fs;

const CHECKPOINT_VERSION: u32 = 1;

struct CheckpointSnapshot {
source_path: String,
lasti: u32,
stack: Vec<PyObjectRef>,
globals: Vec<(String, PyObjectRef)>,
}

impl CheckpointSnapshot {
fn to_pydict(&self, vm: &VirtualMachine) -> PyResult<PyDictRef> {
let payload = vm.ctx.new_dict();
payload.set_item(
"version",
vm.ctx.new_int(CHECKPOINT_VERSION).into(),
vm,
)?;
payload.set_item(
"source_path",
vm.ctx.new_str(self.source_path.clone()).into(),
vm,
)?;
payload.set_item("lasti", vm.ctx.new_int(self.lasti).into(), vm)?;
payload.set_item("stack", vm.ctx.new_list(self.stack.clone()).into(), vm)?;

let globals = vm.ctx.new_dict();
for (key, value) in &self.globals {
globals.set_item(key.as_str(), value.clone(), vm)?;
}
payload.set_item("globals", globals.into(), vm)?;
Ok(payload)
}

fn from_pydict(vm: &VirtualMachine, dict: PyDictRef) -> PyResult<Self> {
let version: u32 = dict.get_item("version", vm)?.try_into_value(vm)?;
if version != CHECKPOINT_VERSION {
return Err(vm.new_value_error(format!(
"unsupported checkpoint version: {version}"
)));
}

let source_path: String = dict.get_item("source_path", vm)?.try_into_value(vm)?;
let lasti: u32 = dict.get_item("lasti", vm)?.try_into_value(vm)?;
let stack: Vec<PyObjectRef> = dict.get_item("stack", vm)?.try_into_value(vm)?;
#[allow(dead_code)]
pub(crate) fn save_checkpoint(vm: &VirtualMachine, path: &str) -> PyResult<()> {
let frame = vm
.current_frame()
.ok_or_else(|| vm.new_runtime_error("checkpoint requires an active frame".to_owned()))?;
let frame = frame.to_owned();

let globals_obj = dict.get_item("globals", vm)?;
let globals_dict = PyDictRef::try_from_object(vm, globals_obj)?;
let mut globals = Vec::new();
for (key, value) in &globals_dict {
let key = key
.downcast_ref::<crate::builtins::PyStr>()
.ok_or_else(|| vm.new_type_error("checkpoint globals key must be str".to_owned()))?;
globals.push((key.as_str().to_owned(), value));
}
ensure_supported_frame(vm, &frame)?;
let resume_lasti = compute_resume_lasti(vm, &frame)?;

Ok(Self {
source_path,
lasti,
stack,
globals,
})
let stack = frame.checkpoint_stack(vm)?;
if !stack.is_empty() {
return Err(vm.new_value_error(
"checkpoint requires an empty value stack".to_owned(),
));
}
let data = save_checkpoint_bytes_from_exec(
vm,
frame.code.source_path.as_str(),
resume_lasti,
&frame.code,
&frame.globals,
)?;
fs::write(path, data).map_err(|err| vm.new_os_error(format!("checkpoint write failed: {err}")))?;
Ok(())
}

#[allow(dead_code)]
pub(crate) fn save_checkpoint(vm: &VirtualMachine, path: &str) -> PyResult<()> {
pub(crate) fn save_checkpoint_bytes(vm: &VirtualMachine) -> PyResult<Vec<u8>> {
let frame = vm
.current_frame()
.ok_or_else(|| vm.new_runtime_error("checkpoint requires an active frame".to_owned()))?;
Expand All @@ -91,98 +53,92 @@ pub(crate) fn save_checkpoint(vm: &VirtualMachine, path: &str) -> PyResult<()> {
"checkpoint requires an empty value stack".to_owned(),
));
}
let globals = extract_globals_from_dict(vm, &frame.globals)?;

let snapshot = CheckpointSnapshot {
source_path: frame.code.source_path.as_str().to_owned(),
lasti: resume_lasti,
stack: Vec::new(),
globals,
};

write_snapshot(vm, path, snapshot)?;
Ok(())
save_checkpoint_bytes_from_exec(
vm,
frame.code.source_path.as_str(),
resume_lasti,
&frame.code,
&frame.globals,
)
}

pub(crate) fn save_checkpoint_from_exec(
vm: &VirtualMachine,
source_path: &str,
lasti: u32,
code: &PyCode,
globals: &PyDictRef,
path: &str,
) -> PyResult<()> {
let globals = extract_globals_from_dict(vm, globals)?;
let snapshot = CheckpointSnapshot {
source_path: source_path.to_owned(),
lasti,
stack: Vec::new(),
globals,
};
write_snapshot(vm, path, snapshot)
let data = save_checkpoint_bytes_from_exec(vm, source_path, lasti, code, globals)?;
fs::write(path, data).map_err(|err| vm.new_os_error(format!("checkpoint write failed: {err}")))?;
Ok(())
}

pub(crate) fn save_checkpoint_bytes_from_exec(
vm: &VirtualMachine,
source_path: &str,
lasti: u32,
code: &PyCode,
globals: &PyDictRef,
) -> PyResult<Vec<u8>> {
snapshot::dump_checkpoint_state(vm, source_path, lasti, code, globals)
}

pub(crate) fn resume_script_from_checkpoint(
vm: &VirtualMachine,
scope: Scope,
_scope: Scope,
script_path: &str,
checkpoint_path: &str,
) -> PyResult<()> {
let snapshot = load_checkpoint(vm, checkpoint_path)?;
if snapshot.source_path != script_path {
let data = fs::read(checkpoint_path)
.map_err(|err| vm.new_os_error(format!("checkpoint read failed: {err}")))?;
resume_script_from_bytes(vm, script_path, &data)
}

pub(crate) fn resume_script_from_bytes(
vm: &VirtualMachine,
script_path: &str,
data: &[u8],
) -> PyResult<()> {
let (state, objects) = snapshot::load_checkpoint_state(vm, data)?;
if state.source_path != script_path {
return Err(vm.new_value_error(format!(
"checkpoint source_path '{}' does not match script '{}'",
snapshot.source_path, script_path
state.source_path, script_path
)));
}

let source = fs::read_to_string(script_path)
.map_err(|err| vm.new_os_error(format!("failed reading script '{script_path}': {err}")))?;
let code = snapshot::decode_code_object(vm, &state.code)
.map_err(|err| vm.new_value_error(format!("checkpoint code invalid: {err:?}")))?;
let code_obj: crate::PyRef<PyCode> = vm.ctx.new_pyref(PyCode::new(code));

let code_obj = vm
.compile(&source, compiler::Mode::Exec, script_path.to_owned())
.map_err(|err| vm.new_syntax_error(&err, Some(&source)))?;
let globals_obj = objects
.get(state.root as usize)
.cloned()
.ok_or_else(|| vm.new_runtime_error("checkpoint globals missing".to_owned()))?;
let module_dict = PyDictRef::try_from_object(vm, globals_obj)?;

let module_dict = scope.globals.clone();
if !module_dict.contains_key("__file__", vm) {
module_dict.set_item("__file__", vm.ctx.new_str(script_path).into(), vm)?;
module_dict.set_item("__cached__", vm.ctx.none(), vm)?;
}

for (key, value) in snapshot.globals {
module_dict.set_item(key.as_str(), value, vm)?;
}

let scope = Scope::with_builtins(None, module_dict.clone(), vm);
let func = PyFunction::new(code_obj.clone(), module_dict, vm)?;
let func_obj = func.into_ref(&vm.ctx).into();
let frame = crate::frame::Frame::new(code_obj, scope, vm.builtins.dict(), &[], Some(func_obj), vm)
.into_ref(&vm.ctx);

if snapshot.lasti as usize >= frame.code.instructions.len() {
if state.lasti as usize >= frame.code.instructions.len() {
return Err(vm.new_value_error(
"checkpoint lasti is out of range for current bytecode".to_owned(),
));
}
frame.set_lasti(snapshot.lasti);
frame.restore_stack(snapshot.stack, vm)?;
frame.set_lasti(state.lasti);
vm.run_frame(frame).map(drop)
}

fn load_checkpoint(vm: &VirtualMachine, path: &str) -> PyResult<CheckpointSnapshot> {
let data = fs::read(path)
.map_err(|err| vm.new_os_error(format!("checkpoint read failed: {err}")))?;
let payload = marshal_loads(vm, data)?;
let dict = PyDictRef::try_from_object(vm, payload)?;
CheckpointSnapshot::from_pydict(vm, dict)
}

fn write_snapshot(vm: &VirtualMachine, path: &str, snapshot: CheckpointSnapshot) -> PyResult<()> {
let payload = snapshot.to_pydict(vm)?;
let data = marshal_dumps(vm, payload.into())?;
fs::write(path, data).map_err(|err| vm.new_os_error(format!("checkpoint write failed: {err}")))?;
Ok(())
}

#[allow(dead_code)]
fn compute_resume_lasti(vm: &VirtualMachine, frame: &FrameRef) -> PyResult<u32> {
let lasti = frame.lasti();
Expand Down Expand Up @@ -220,60 +176,3 @@ fn ensure_supported_frame(vm: &VirtualMachine, frame: &FrameRef) -> PyResult<()>
}
Ok(())
}
Comment on lines +468 to +477
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

🏁 Script executed:

rg -n "ensure_supported_frame" --type rust

Repository: RustPython/RustPython

Length of output: 182


🏁 Script executed:

# Get more context about the checkpoint function and its multi-frame support
cd crates/vm/src/vm && wc -l checkpoint.rs && head -50 checkpoint.rs

Repository: RustPython/RustPython

Length of output: 1951


🏁 Script executed:

# Search for checkpoint function and multi-frame related code
rg -n "multi.*frame|multiple.*frame" crates/vm/src/vm/checkpoint.rs -i

Repository: RustPython/RustPython

Length of output: 155


🏁 Script executed:

# Check if ensure_supported_frame is imported or used anywhere else in the codebase
rg "ensure_supported_frame" --type rust -B 2 -A 2

Repository: RustPython/RustPython

Length of output: 403


🏁 Script executed:

# Read the full context around line 273 and the checkpoint functions
sed -n '250,300p' crates/vm/src/vm/checkpoint.rs

Repository: RustPython/RustPython

Length of output: 2019


🏁 Script executed:

# Read the save_checkpoint_bytes_from_frames function to understand multi-frame handling
sed -n '100,200p' crates/vm/src/vm/checkpoint.rs

Repository: RustPython/RustPython

Length of output: 3312


Remove unused ensure_supported_frame function that contradicts multi-frame checkpoint support.

This function is marked #[allow(dead_code)] and is never called. More importantly, its single-frame check (vm.frames.borrow().len() != 1) contradicts the actual checkpoint implementation which explicitly handles multiple frames (see lines 267-291 where it branches on frame_refs.len() == 1 and includes dedicated logic for multi-frame execution). Delete this unused function.

🤖 Prompt for AI Agents
In `@crates/vm/src/vm/checkpoint.rs` around lines 468 - 477, Delete the unused
helper ensure_supported_frame which conflicts with multi-frame checkpoint logic:
remove the entire function definition (including the #[allow(dead_code)]
attribute) and any dead imports only used by it; keep
validate_frame_for_checkpoint, VirtualMachine, and FrameRef untouched so
existing checkpoint logic that handles both single- and multi-frame cases (the
frame_refs.len() branching) remains correct.


fn extract_globals_from_dict(
vm: &VirtualMachine,
dict: &PyDictRef,
) -> PyResult<Vec<(String, PyObjectRef)>> {
let mut globals: Vec<(String, PyObjectRef)> = Vec::new();
for (key, value) in dict {
let key = key
.downcast_ref::<crate::builtins::PyStr>()
.ok_or_else(|| vm.new_type_error("checkpoint globals key must be str".to_owned()))?;
let key_str = key.as_str();
if key_str.starts_with("__") {
continue;
}
if !is_marshaled_value(&value, vm) {
continue;
}
globals.push((key_str.to_owned(), value));
}
Ok(globals)
}

fn is_marshaled_value(obj: &PyObjectRef, vm: &VirtualMachine) -> bool {
if vm.is_none(obj) {
return true;
}
obj.fast_isinstance(vm.ctx.types.int_type)
|| obj.fast_isinstance(vm.ctx.types.bool_type)
|| obj.fast_isinstance(vm.ctx.types.float_type)
|| obj.fast_isinstance(vm.ctx.types.complex_type)
|| obj.fast_isinstance(vm.ctx.types.str_type)
|| obj.fast_isinstance(vm.ctx.types.bytes_type)
|| obj.fast_isinstance(vm.ctx.types.bytearray_type)
|| obj.fast_isinstance(vm.ctx.types.list_type)
|| obj.fast_isinstance(vm.ctx.types.tuple_type)
|| obj.fast_isinstance(vm.ctx.types.dict_type)
|| obj.fast_isinstance(vm.ctx.types.set_type)
|| obj.fast_isinstance(vm.ctx.types.frozenset_type)
|| obj.fast_isinstance(vm.ctx.types.ellipsis_type)
}

fn marshal_dumps(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Vec<u8>> {
let marshal = vm.import("marshal", 0)?;
let dumps = marshal.get_attr("dumps", vm)?;
let data = dumps.call((obj,), vm)?;
let data: PyBytesRef = data.downcast().map_err(|_| {
vm.new_type_error("marshal.dumps did not return bytes".to_owned())
})?;
Ok(data.as_bytes().to_vec())
}

fn marshal_loads(vm: &VirtualMachine, data: Vec<u8>) -> PyResult<PyObjectRef> {
let marshal = vm.import("marshal", 0)?;
let loads = marshal.get_attr("loads", vm)?;
let data: PyObjectRef = vm.ctx.new_bytes(data).into();
loads.call((data,), vm)
}
1 change: 1 addition & 0 deletions crates/vm/src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
mod compile;
mod context;
pub(crate) mod checkpoint;
pub(crate) mod snapshot;
mod interpreter;
mod method;
mod setting;
Expand Down
Loading