Skip to content

Commit 37efb05

Browse files
Add eval function to c-api
1 parent 26f5bbf commit 37efb05

3 files changed

Lines changed: 86 additions & 1 deletion

File tree

‎crates/capi/Cargo.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ license.workspace = true
1212
crate-type = ["cdylib", "rlib"]
1313

1414
[dependencies]
15-
rustpython-vm = { workspace = true, features = ["threading"] }
15+
rustpython-vm = { workspace = true, features = ["threading", "compiler"] }
1616
rustpython-stdlib = {workspace = true, features = ["threading"] }
1717

1818
[dev-dependencies]

‎crates/capi/src/ceval.rs‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
use crate::pystate::with_vm;
2+
use core::ffi::{CStr, c_char, c_int};
3+
use core::ptr::NonNull;
4+
use rustpython_vm::builtins::{PyCode, PyDict};
5+
use rustpython_vm::compiler::Mode;
6+
use rustpython_vm::function::ArgMapping;
7+
use rustpython_vm::scope::Scope;
8+
use rustpython_vm::{AsObject, PyObject, TryFromObject};
9+
10+
#[unsafe(no_mangle)]
11+
pub unsafe extern "C" fn Py_CompileString(
12+
code: *const c_char,
13+
filename: *const c_char,
14+
start: c_int,
15+
) -> *mut PyObject {
16+
with_vm(|vm| {
17+
let code = unsafe { CStr::from_ptr(code) }.to_str().map_err(|_| {
18+
vm.new_system_error("Py_CompileString called with non UTF-8 code string")
19+
})?;
20+
let filename = unsafe { CStr::from_ptr(filename) }
21+
.to_str()
22+
.map_err(|_| vm.new_system_error("Py_CompileString called with non UTF-8 filename"))?;
23+
24+
let mode = match start {
25+
256 => Mode::Single,
26+
257 => Mode::Exec,
27+
258 => Mode::Eval,
28+
_ => {
29+
return Err(
30+
vm.new_system_error("Invalid start argument passed to Py_CompileString")
31+
);
32+
}
33+
};
34+
35+
vm.compile(code, mode, filename.to_owned())
36+
.map_err(|err| vm.new_syntax_error(&err, Some(code)))
37+
})
38+
}
39+
40+
#[unsafe(no_mangle)]
41+
pub unsafe extern "C" fn PyEval_EvalCode(
42+
co: *mut PyObject,
43+
globals: *mut PyObject,
44+
locals: *mut PyObject,
45+
) -> *mut PyObject {
46+
with_vm(|vm| {
47+
let code = unsafe { &*co }.try_downcast_ref::<PyCode>(vm)?;
48+
let globals = unsafe { &*globals }.try_downcast_ref::<PyDict>(vm)?;
49+
let locals = NonNull::new(locals)
50+
.map(|ptr| ArgMapping::try_from_object(vm, unsafe { ptr.as_ref() }.to_owned()))
51+
.transpose()?;
52+
53+
let scope = Scope::with_builtins(locals, globals.to_owned(), vm);
54+
55+
vm.run_code_obj(code.to_owned(), scope)
56+
})
57+
}
58+
59+
#[unsafe(no_mangle)]
60+
pub extern "C" fn PyEval_GetBuiltins() -> *mut PyObject {
61+
with_vm(|vm| vm.builtins.as_object().as_raw())
62+
}
63+
64+
#[cfg(false)]
65+
mod tests {
66+
use pyo3::exceptions::PyException;
67+
use pyo3::prelude::*;
68+
69+
#[test]
70+
fn test_code_eval() {
71+
Python::attach(|py| {
72+
let result = py.eval(c"1 + 1", None, None).unwrap();
73+
assert_eq!(result.extract::<u32>().unwrap(), 2);
74+
})
75+
}
76+
77+
#[test]
78+
fn test_code_run_exception() {
79+
Python::attach(|py| {
80+
let err = py.run(c"raise Exception()", None, None).unwrap_err();
81+
assert!(err.is_instance_of::<PyException>(py));
82+
})
83+
}
84+
}

‎crates/capi/src/lib.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ extern crate alloc;
1010

1111
pub mod abstract_;
1212
pub mod bytesobject;
13+
pub mod ceval;
1314
pub mod import;
1415
pub mod longobject;
1516
pub mod object;

0 commit comments

Comments
 (0)