@@ -23,7 +23,11 @@ struct Compiler {
2323}
2424
2525/// Compile a given sourcecode into a bytecode object.
26- pub fn compile ( source : & str , mode : & Mode , source_path : String ) -> Result < CodeObject , CompileError > {
26+ pub fn compile (
27+ source : & str ,
28+ mode : & Mode ,
29+ source_path : Option < String > ,
30+ ) -> Result < CodeObject , CompileError > {
2731 match mode {
2832 Mode :: Exec => {
2933 let ast = parser:: parse_program ( source) ?;
@@ -42,11 +46,11 @@ pub fn compile(source: &str, mode: &Mode, source_path: String) -> Result<CodeObj
4246
4347/// A helper function for the shared code of the different compile functions
4448fn with_compiler (
45- source_path : String ,
49+ source_path : Option < String > ,
4650 f : impl FnOnce ( & mut Compiler ) -> Result < ( ) , CompileError > ,
4751) -> Result < CodeObject , CompileError > {
4852 let mut compiler = Compiler :: new ( ) ;
49- compiler. source_path = Some ( source_path) ;
53+ compiler. source_path = source_path;
5054 compiler. push_new_code_object ( "<module>" . to_string ( ) ) ;
5155 f ( & mut compiler) ?;
5256 let code = compiler. pop_code_object ( ) ;
@@ -55,7 +59,10 @@ fn with_compiler(
5559}
5660
5761/// Compile a standard Python program to bytecode
58- pub fn compile_program ( ast : ast:: Program , source_path : String ) -> Result < CodeObject , CompileError > {
62+ pub fn compile_program (
63+ ast : ast:: Program ,
64+ source_path : Option < String > ,
65+ ) -> Result < CodeObject , CompileError > {
5966 with_compiler ( source_path, |compiler| {
6067 let symbol_table = make_symbol_table ( & ast) ?;
6168 compiler. compile_program ( & ast, symbol_table)
@@ -65,7 +72,7 @@ pub fn compile_program(ast: ast::Program, source_path: String) -> Result<CodeObj
6572/// Compile a single Python expression to bytecode
6673pub fn compile_statement_eval (
6774 statement : Vec < ast:: LocatedStatement > ,
68- source_path : String ,
75+ source_path : Option < String > ,
6976) -> Result < CodeObject , CompileError > {
7077 with_compiler ( source_path, |compiler| {
7178 let symbol_table = statements_to_symbol_table ( & statement) ?;
@@ -76,7 +83,7 @@ pub fn compile_statement_eval(
7683/// Compile a Python program to bytecode for the context of a REPL
7784pub fn compile_program_single (
7885 ast : ast:: Program ,
79- source_path : String ,
86+ source_path : Option < String > ,
8087) -> Result < CodeObject , CompileError > {
8188 with_compiler ( source_path, |compiler| {
8289 let symbol_table = make_symbol_table ( & ast) ?;
@@ -119,7 +126,7 @@ impl Compiler {
119126 Varargs :: None ,
120127 Vec :: new ( ) ,
121128 Varargs :: None ,
122- self . source_path . clone ( ) . unwrap ( ) ,
129+ self . source_path . clone ( ) ,
123130 line_number,
124131 obj_name,
125132 ) ) ;
@@ -596,7 +603,7 @@ impl Compiler {
596603 Varargs :: from ( & args. vararg ) ,
597604 args. kwonlyargs . iter ( ) . map ( |a| a. arg . clone ( ) ) . collect ( ) ,
598605 Varargs :: from ( & args. kwarg ) ,
599- self . source_path . clone ( ) . unwrap ( ) ,
606+ self . source_path . clone ( ) ,
600607 line_number,
601608 name. to_string ( ) ,
602609 ) ) ;
@@ -849,7 +856,7 @@ impl Compiler {
849856 Varargs :: None ,
850857 vec ! [ ] ,
851858 Varargs :: None ,
852- self . source_path . clone ( ) . unwrap ( ) ,
859+ self . source_path . clone ( ) ,
853860 line_number,
854861 name. to_string ( ) ,
855862 ) ) ;
@@ -1571,7 +1578,7 @@ impl Compiler {
15711578 Varargs :: None ,
15721579 vec ! [ ] ,
15731580 Varargs :: None ,
1574- self . source_path . clone ( ) . unwrap ( ) ,
1581+ self . source_path . clone ( ) ,
15751582 line_number,
15761583 name. clone ( ) ,
15771584 ) ) ;
0 commit comments