Skip to content

Commit 3664693

Browse files
committed
capi: add PyFrame_GetLocals, GetGlobals, GetBuiltins
Wire the C-API getters to frame.f_locals, f_globals, and f_builtins, and cover write-through vs snapshot locals in a capi test. Assisted-by: Grok:4.6
1 parent 54420c4 commit 3664693

2 files changed

Lines changed: 136 additions & 3 deletions

File tree

‎crates/capi/src/pyframe.rs‎

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::PyObject;
12
use crate::pystate::with_vm;
23
use core::ffi::c_int;
34
use rustpython_vm::Py;
@@ -16,3 +17,135 @@ pub unsafe extern "C" fn PyFrame_GetCode(frame: *mut PyFrameObject) -> *mut PyCo
1617
pub unsafe extern "C" fn PyFrame_GetLineNumber(frame: *mut PyFrameObject) -> c_int {
1718
with_vm(|_vm| Ok(unsafe { &*frame }.lineno()))
1819
}
20+
21+
#[unsafe(no_mangle)]
22+
pub unsafe extern "C" fn PyFrame_GetLocals(frame: *mut PyFrameObject) -> *mut PyObject {
23+
with_vm(|vm| unsafe { &*frame }.f_locals(vm))
24+
}
25+
26+
#[unsafe(no_mangle)]
27+
pub unsafe extern "C" fn PyFrame_GetGlobals(frame: *mut PyFrameObject) -> *mut PyObject {
28+
with_vm(|_vm| Ok(unsafe { &*frame }.f_globals()))
29+
}
30+
31+
#[unsafe(no_mangle)]
32+
pub unsafe extern "C" fn PyFrame_GetBuiltins(frame: *mut PyFrameObject) -> *mut PyObject {
33+
with_vm(|_vm| Ok(unsafe { &*frame }.f_builtins()))
34+
}
35+
36+
#[cfg(test)]
37+
mod tests {
38+
use alloc::ffi::CString;
39+
use pyo3::prelude::*;
40+
use rustpython_vm::AsObject;
41+
42+
fn current_frame_ptr() -> *mut super::PyFrameObject {
43+
rustpython_vm::vm::thread::with_current_vm(|vm| {
44+
vm.current_frame()
45+
.unwrap()
46+
.as_object()
47+
.as_raw()
48+
.cast_mut()
49+
.cast()
50+
})
51+
}
52+
53+
#[pyfunction]
54+
fn write_local_via_frame_get_locals(name: &str, value: i64) -> bool {
55+
let locals = unsafe { super::PyFrame_GetLocals(current_frame_ptr()) };
56+
assert!(!locals.is_null());
57+
let key = CString::new(name).unwrap();
58+
let py_value = crate::longobject::PyLong_FromLongLong(value);
59+
let rc =
60+
unsafe { crate::abstract_::PyMapping_SetItemString(locals, key.as_ptr(), py_value) };
61+
unsafe {
62+
crate::refcount::_Py_DecRef(py_value);
63+
crate::refcount::_Py_DecRef(locals);
64+
}
65+
rc == 0
66+
}
67+
68+
#[pyfunction]
69+
fn snapshot_set_does_not_write(name: &str, value: i64) -> bool {
70+
let locals = crate::ceval::PyEval_GetFrameLocals();
71+
assert!(!locals.is_null());
72+
let key = CString::new(name).unwrap();
73+
let py_value = crate::longobject::PyLong_FromLongLong(value);
74+
let rc =
75+
unsafe { crate::abstract_::PyMapping_SetItemString(locals, key.as_ptr(), py_value) };
76+
unsafe {
77+
crate::refcount::_Py_DecRef(py_value);
78+
crate::refcount::_Py_DecRef(locals);
79+
}
80+
rc == 0
81+
}
82+
83+
#[pyfunction]
84+
fn frame_globals_is_globals() -> bool {
85+
rustpython_vm::vm::thread::with_current_vm(|vm| {
86+
let frame = vm.current_frame().unwrap();
87+
let got = unsafe { super::PyFrame_GetGlobals(current_frame_ptr()) };
88+
let expected = frame.iframe().globals().as_object().as_raw();
89+
let same = core::ptr::eq(got.cast_const(), expected);
90+
unsafe { crate::refcount::_Py_DecRef(got) };
91+
same
92+
})
93+
}
94+
95+
#[pyfunction]
96+
fn frame_builtins_is_builtins() -> bool {
97+
rustpython_vm::vm::thread::with_current_vm(|vm| {
98+
let frame = vm.current_frame().unwrap();
99+
let got = unsafe { super::PyFrame_GetBuiltins(current_frame_ptr()) };
100+
let expected = frame.iframe().builtins().as_object().as_raw();
101+
let same = core::ptr::eq(got.cast_const(), expected);
102+
unsafe { crate::refcount::_Py_DecRef(got) };
103+
same
104+
})
105+
}
106+
107+
#[test]
108+
fn get_locals_globals_builtins() {
109+
Python::attach(|py| {
110+
let globals = pyo3::types::PyDict::new(py);
111+
globals
112+
.set_item(
113+
"write_local_via_frame_get_locals",
114+
wrap_pyfunction!(write_local_via_frame_get_locals, py).unwrap(),
115+
)
116+
.unwrap();
117+
globals
118+
.set_item(
119+
"snapshot_set_does_not_write",
120+
wrap_pyfunction!(snapshot_set_does_not_write, py).unwrap(),
121+
)
122+
.unwrap();
123+
globals
124+
.set_item(
125+
"frame_globals_is_globals",
126+
wrap_pyfunction!(frame_globals_is_globals, py).unwrap(),
127+
)
128+
.unwrap();
129+
globals
130+
.set_item(
131+
"frame_builtins_is_builtins",
132+
wrap_pyfunction!(frame_builtins_is_builtins, py).unwrap(),
133+
)
134+
.unwrap();
135+
py.run(
136+
c"\
137+
def optimized():
138+
x = 1
139+
ok = write_local_via_frame_get_locals('x', 2)
140+
snapshot_ok = snapshot_set_does_not_write('x', 9)
141+
return x, ok, snapshot_ok, frame_globals_is_globals(), frame_builtins_is_builtins()
142+
143+
assert optimized() == (2, True, True, True, True)
144+
",
145+
Some(&globals),
146+
None,
147+
)
148+
.unwrap();
149+
})
150+
}
151+
}

‎crates/vm/src/builtins/frame.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -468,12 +468,12 @@ impl FrameObject {
468468
#[pyclass(flags(DISALLOW_INSTANTIATION), with(Py, Representable))]
469469
impl FrameObject {
470470
#[pygetset]
471-
fn f_globals(&self) -> PyDictRef {
471+
pub fn f_globals(&self) -> PyDictRef {
472472
self.iframe().globals().to_owned()
473473
}
474474

475475
#[pygetset]
476-
fn f_builtins(&self) -> PyObjectRef {
476+
pub fn f_builtins(&self) -> PyObjectRef {
477477
self.iframe().builtins().to_owned()
478478
}
479479

@@ -892,7 +892,7 @@ impl Py<FrameObject> {
892892
}
893893

894894
#[pygetset]
895-
fn f_locals(&self, vm: &VirtualMachine) -> PyResult {
895+
pub fn f_locals(&self, vm: &VirtualMachine) -> PyResult {
896896
if self.uses_locals_proxy(vm)? {
897897
let proxy = crate::builtins::FrameLocalsProxy::new(self.to_owned());
898898
Ok(proxy.into_ref(&vm.ctx).into())

0 commit comments

Comments
 (0)