@@ -124,30 +124,40 @@ struct CompileArgs {
124
124
optimize : OptionalArg < PyIntRef > ,
125
125
}
126
126
127
- #[ cfg( feature = "rustpython-compiler" ) ]
128
127
fn builtin_compile ( args : CompileArgs , vm : & VirtualMachine ) -> PyResult {
129
128
// TODO: compile::compile should probably get bytes
130
129
let source = match & args. source {
131
130
Either :: A ( string) => string. as_str ( ) ,
132
131
Either :: B ( bytes) => str:: from_utf8 ( bytes) . unwrap ( ) ,
133
132
} ;
134
133
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 ( ) ;
140
135
141
136
let flags = args
142
137
. flags
143
138
. map_or ( Ok ( 0 ) , |v| i32:: try_from_object ( vm, v. into_object ( ) ) ) ?;
144
139
145
140
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
+ }
149
155
} 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)
151
161
}
152
162
}
153
163
@@ -757,7 +767,6 @@ pub fn make_module(vm: &VirtualMachine, module: PyObjectRef) {
757
767
#[ cfg( feature = "rustpython-compiler" ) ]
758
768
{
759
769
extend_module ! ( vm, module, {
760
- "compile" => ctx. new_rustfunc( builtin_compile) ,
761
770
"eval" => ctx. new_rustfunc( builtin_eval) ,
762
771
"exec" => ctx. new_rustfunc( builtin_exec) ,
763
772
} ) ;
@@ -780,6 +789,7 @@ pub fn make_module(vm: &VirtualMachine, module: PyObjectRef) {
780
789
"callable" => ctx. new_rustfunc( builtin_callable) ,
781
790
"chr" => ctx. new_rustfunc( builtin_chr) ,
782
791
"classmethod" => ctx. classmethod_type( ) ,
792
+ "compile" => ctx. new_rustfunc( builtin_compile) ,
783
793
"complex" => ctx. complex_type( ) ,
784
794
"delattr" => ctx. new_rustfunc( builtin_delattr) ,
785
795
"dict" => ctx. dict_type( ) ,
0 commit comments