Skip to content

Commit 78dce22

Browse files
committed
compile() with PyCF_ONLY_AST is available without compiler
1 parent 2f1fb16 commit 78dce22

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

vm/src/builtins.rs

+21-11
Original file line numberDiff line numberDiff line change
@@ -124,30 +124,40 @@ struct CompileArgs {
124124
optimize: OptionalArg<PyIntRef>,
125125
}
126126

127-
#[cfg(feature = "rustpython-compiler")]
128127
fn builtin_compile(args: CompileArgs, vm: &VirtualMachine) -> PyResult {
129128
// TODO: compile::compile should probably get bytes
130129
let source = match &args.source {
131130
Either::A(string) => string.as_str(),
132131
Either::B(bytes) => str::from_utf8(bytes).unwrap(),
133132
};
134133

135-
let mode = args
136-
.mode
137-
.as_str()
138-
.parse::<compile::Mode>()
139-
.map_err(|err| vm.new_value_error(err.to_string()))?;
134+
let mode_str = args.mode.as_str();
140135

141136
let flags = args
142137
.flags
143138
.map_or(Ok(0), |v| i32::try_from_object(vm, v.into_object()))?;
144139

145140
if (flags & ast::PY_COMPILE_FLAG_AST_ONLY).is_zero() {
146-
vm.compile(&source, mode, args.filename.as_str().to_string())
147-
.map(|o| o.into_object())
148-
.map_err(|err| vm.new_syntax_error(&err))
141+
#[cfg(feature = "rustpython-compiler")]
142+
{
143+
let mode = mode_str
144+
.parse::<compile::Mode>()
145+
.map_err(|err| vm.new_value_error(err.to_string()))?;
146+
147+
vm.compile(&source, mode, args.filename.as_str().to_string())
148+
.map(|o| o.into_object())
149+
.map_err(|err| vm.new_syntax_error(&err))
150+
}
151+
#[cfg(not(feature = "rustpython-compiler"))]
152+
{
153+
Err(vm.new_value_error("PyCF_ONLY_AST flag is required without compiler support"))
154+
}
149155
} else {
150-
ast::parse(&vm, &source, mode.to_parser_mode())
156+
use rustpython_parser::parser;
157+
let mode = mode_str
158+
.parse::<parser::Mode>()
159+
.map_err(|err| vm.new_value_error(err.to_string()))?;
160+
ast::parse(&vm, &source, mode)
151161
}
152162
}
153163

@@ -757,7 +767,6 @@ pub fn make_module(vm: &VirtualMachine, module: PyObjectRef) {
757767
#[cfg(feature = "rustpython-compiler")]
758768
{
759769
extend_module!(vm, module, {
760-
"compile" => ctx.new_rustfunc(builtin_compile),
761770
"eval" => ctx.new_rustfunc(builtin_eval),
762771
"exec" => ctx.new_rustfunc(builtin_exec),
763772
});
@@ -780,6 +789,7 @@ pub fn make_module(vm: &VirtualMachine, module: PyObjectRef) {
780789
"callable" => ctx.new_rustfunc(builtin_callable),
781790
"chr" => ctx.new_rustfunc(builtin_chr),
782791
"classmethod" => ctx.classmethod_type(),
792+
"compile" => ctx.new_rustfunc(builtin_compile),
783793
"complex" => ctx.complex_type(),
784794
"delattr" => ctx.new_rustfunc(builtin_delattr),
785795
"dict" => ctx.dict_type(),

0 commit comments

Comments
 (0)