1+ use crate :: pyframe:: PyFrameObject ;
12use crate :: pystate:: with_vm;
23use crate :: unicodeobject:: decode_fsdefault_and_size;
34use core:: ffi:: { CStr , c_char, c_int} ;
45use core:: ptr:: NonNull ;
56use rustpython_vm:: builtins:: { PyCode , PyDict } ;
67use rustpython_vm:: function:: ArgMapping ;
78use rustpython_vm:: scope:: Scope ;
8- use rustpython_vm:: version;
99use rustpython_vm:: { AsObject , PyObject , TryFromObject } ;
10+ use rustpython_vm:: { PyObjectRef , version} ;
1011
1112#[ unsafe( no_mangle) ]
1213pub unsafe extern "C" fn Py_CompileString (
@@ -42,6 +43,16 @@ pub unsafe extern "C" fn PyEval_EvalCode(
4243 } )
4344}
4445
46+ #[ unsafe( no_mangle) ]
47+ pub unsafe extern "C" fn PyEval_EvalFrame ( f : * mut PyFrameObject ) -> * mut PyObject {
48+ unsafe { PyEval_EvalFrameEx ( f, 0 ) }
49+ }
50+
51+ #[ unsafe( no_mangle) ]
52+ pub unsafe extern "C" fn PyEval_EvalFrameEx ( f : * mut PyFrameObject , _exc : c_int ) -> * mut PyObject {
53+ with_vm ( |vm| vm. run_frame ( unsafe { & * f } . to_owned ( ) ) )
54+ }
55+
4556#[ unsafe( no_mangle) ]
4657pub extern "C" fn PyEval_GetBuiltins ( ) -> * mut PyObject {
4758 with_vm ( |vm| {
@@ -52,6 +63,82 @@ pub extern "C" fn PyEval_GetBuiltins() -> *mut PyObject {
5263 } )
5364}
5465
66+ #[ unsafe( no_mangle) ]
67+ pub extern "C" fn PyEval_GetFrame ( ) -> * mut PyFrameObject {
68+ with_vm ( |vm| -> * mut PyObject {
69+ vm. current_frame ( )
70+ . map ( |frame| frame. as_object ( ) . as_raw ( ) . cast_mut ( ) )
71+ . unwrap_or_default ( )
72+ } )
73+ . cast ( )
74+ }
75+
76+ #[ unsafe( no_mangle) ]
77+ pub extern "C" fn PyEval_GetFrameBuiltins ( ) -> * mut PyObject {
78+ with_vm ( |vm| {
79+ vm. current_frame ( ) . map_or_else (
80+ || vm. builtins . as_object ( ) . to_owned ( ) ,
81+ |frame| frame. builtins . as_object ( ) . to_owned ( ) ,
82+ )
83+ } )
84+ }
85+
86+ #[ unsafe( no_mangle) ]
87+ pub extern "C" fn PyEval_GetFrameGlobals ( ) -> * mut PyObject {
88+ with_vm ( |vm| {
89+ vm. current_frame ( )
90+ . map ( |frame| frame. globals . as_object ( ) . to_owned ( ) . into_raw ( ) . as_ptr ( ) )
91+ . unwrap_or_default ( )
92+ } )
93+ }
94+
95+ #[ unsafe( no_mangle) ]
96+ pub extern "C" fn PyEval_GetFrameLocals ( ) -> * mut PyObject {
97+ with_vm ( |vm| {
98+ let Some ( frame) = vm. current_frame ( ) else {
99+ return Ok ( core:: ptr:: null_mut ( ) ) ;
100+ } ;
101+ let locals: PyObjectRef = frame. locals ( vm) ?. into ( ) ;
102+ Ok ( locals. into_raw ( ) . as_ptr ( ) )
103+ } )
104+ }
105+
106+ #[ unsafe( no_mangle) ]
107+ pub extern "C" fn PyEval_GetGlobals ( ) -> * mut PyObject {
108+ with_vm ( |vm| {
109+ vm. current_frame ( )
110+ . map ( |frame| frame. globals . as_object ( ) . as_raw ( ) )
111+ . unwrap_or_default ( )
112+ } )
113+ }
114+
115+ #[ unsafe( no_mangle) ]
116+ pub extern "C" fn PyEval_GetLocals ( ) -> * mut PyObject {
117+ with_vm ( |vm| {
118+ let Some ( frame) = vm. current_frame ( ) else {
119+ return Ok ( core:: ptr:: null_mut ( ) ) ;
120+ } ;
121+ let _ = frame. locals ( vm) ?;
122+ Ok ( frame. locals . as_object ( vm) . as_raw ( ) . cast_mut ( ) )
123+ } )
124+ }
125+
126+ #[ unsafe( no_mangle) ]
127+ pub unsafe extern "C" fn PyEval_GetFuncDesc ( func : * mut PyObject ) -> * const c_char {
128+ with_vm ( |vm| {
129+ let func = unsafe { & * func } ;
130+ let cls = func. class ( ) ;
131+ if cls. is ( vm. ctx . types . bound_method_type )
132+ || cls. is ( vm. ctx . types . function_type )
133+ || cls. is ( vm. ctx . types . builtin_function_or_method_type )
134+ {
135+ c"()"
136+ } else {
137+ c" object"
138+ }
139+ } )
140+ }
141+
55142#[ cfg( test) ]
56143mod tests {
57144 use pyo3:: exceptions:: PyException ;
0 commit comments