@@ -11,6 +11,8 @@ use num_bigint::Sign;
11
11
use num_traits:: { Signed , ToPrimitive , Zero } ;
12
12
#[ cfg( feature = "rustpython-compiler" ) ]
13
13
use rustpython_compiler:: compile;
14
+ #[ cfg( feature = "rustpython-parser" ) ]
15
+ use rustpython_parser:: parser;
14
16
15
17
use crate :: exceptions:: PyBaseExceptionRef ;
16
18
use crate :: function:: { single_or_tuple_any, Args , KwArgs , OptionalArg , PyFuncArgs } ;
@@ -32,6 +34,7 @@ use crate::pyobject::{
32
34
} ;
33
35
use crate :: readline:: { Readline , ReadlineResult } ;
34
36
use crate :: scope:: Scope ;
37
+ #[ cfg( feature = "rustpython-parser" ) ]
35
38
use crate :: stdlib:: ast;
36
39
use crate :: vm:: VirtualMachine ;
37
40
@@ -125,6 +128,7 @@ struct CompileArgs {
125
128
optimize : OptionalArg < PyIntRef > ,
126
129
}
127
130
131
+ #[ cfg( feature = "rustpython-compiler" ) ]
128
132
fn builtin_compile ( args : CompileArgs , vm : & VirtualMachine ) -> PyResult {
129
133
// TODO: compile::compile should probably get bytes
130
134
let source = match & args. source {
@@ -138,27 +142,28 @@ fn builtin_compile(args: CompileArgs, vm: &VirtualMachine) -> PyResult {
138
142
. flags
139
143
. map_or ( Ok ( 0 ) , |v| i32:: try_from_object ( vm, v. into_object ( ) ) ) ?;
140
144
141
- if ( flags & ast :: PY_COMPILE_FLAG_AST_ONLY ) . is_zero ( ) {
142
- # [ cfg ( feature = "rustpython-compiler" ) ]
143
- {
145
+ # [ cfg ( feature = "rustpython-parser" ) ]
146
+ {
147
+ if ( flags & ast :: PY_COMPILE_FLAG_AST_ONLY ) . is_zero ( ) {
144
148
let mode = mode_str
145
149
. parse :: < compile:: Mode > ( )
146
150
. map_err ( |err| vm. new_value_error ( err. to_string ( ) ) ) ?;
147
151
148
152
vm. compile ( & source, mode, args. filename . as_str ( ) . to_owned ( ) )
149
153
. map ( |o| o. into_object ( ) )
150
154
. map_err ( |err| vm. new_syntax_error ( & err) )
155
+ } else {
156
+ let mode = mode_str
157
+ . parse :: < parser:: Mode > ( )
158
+ . map_err ( |err| vm. new_value_error ( err. to_string ( ) ) ) ?;
159
+ ast:: parse ( & vm, & source, mode)
151
160
}
152
- #[ cfg( not( feature = "rustpython-compiler" ) ) ]
153
- {
154
- Err ( vm. new_value_error ( "PyCF_ONLY_AST flag is required without compiler support" ) )
155
- }
156
- } else {
157
- use rustpython_parser:: parser;
158
- let mode = mode_str
159
- . parse :: < parser:: Mode > ( )
160
- . map_err ( |err| vm. new_value_error ( err. to_string ( ) ) ) ?;
161
- ast:: parse ( & vm, & source, mode)
161
+ }
162
+ #[ cfg( not( feature = "rustpython-parser" ) ) ]
163
+ {
164
+ Err ( vm. new_value_error (
165
+ "PyCF_ONLY_AST flag is not supported without parser support" . to_string ( ) ,
166
+ ) )
162
167
}
163
168
}
164
169
@@ -217,6 +222,7 @@ fn builtin_exec(
217
222
run_code ( vm, source, scope, compile:: Mode :: Exec )
218
223
}
219
224
225
+ #[ cfg( feature = "rustpython-compiler" ) ]
220
226
fn run_code (
221
227
vm : & VirtualMachine ,
222
228
source : Either < PyStringRef , PyCodeRef > ,
@@ -237,6 +243,7 @@ fn run_code(
237
243
vm. run_code_obj ( code_obj, scope)
238
244
}
239
245
246
+ #[ cfg( feature = "rustpython-compiler" ) ]
240
247
fn make_scope ( vm : & VirtualMachine , scope : ScopeArgs ) -> PyResult < Scope > {
241
248
let globals = scope. globals ;
242
249
let current_scope = vm. current_scope ( ) ;
@@ -776,6 +783,7 @@ pub fn make_module(vm: &VirtualMachine, module: PyObjectRef) {
776
783
extend_module ! ( vm, module, {
777
784
"eval" => ctx. new_function( builtin_eval) ,
778
785
"exec" => ctx. new_function( builtin_exec) ,
786
+ "compile" => ctx. new_function( builtin_compile) ,
779
787
} ) ;
780
788
}
781
789
@@ -796,7 +804,6 @@ pub fn make_module(vm: &VirtualMachine, module: PyObjectRef) {
796
804
"callable" => ctx. new_function( builtin_callable) ,
797
805
"chr" => ctx. new_function( builtin_chr) ,
798
806
"classmethod" => ctx. classmethod_type( ) ,
799
- "compile" => ctx. new_function( builtin_compile) ,
800
807
"complex" => ctx. complex_type( ) ,
801
808
"delattr" => ctx. new_function( builtin_delattr) ,
802
809
"dict" => ctx. dict_type( ) ,
0 commit comments