mirror of
https://github.com/RustPython/Parser.git
synced 2025-08-31 07:38:04 +00:00
Revert "Make CodeObject.source_path an Option<String>"
This commit is contained in:
parent
d7b4837e26
commit
a6808efab7
2 changed files with 12 additions and 19 deletions
|
@ -21,7 +21,7 @@ pub struct CodeObject {
|
||||||
pub varargs: Varargs, // *args or *
|
pub varargs: Varargs, // *args or *
|
||||||
pub kwonlyarg_names: Vec<String>,
|
pub kwonlyarg_names: Vec<String>,
|
||||||
pub varkeywords: Varargs, // **kwargs or **
|
pub varkeywords: Varargs, // **kwargs or **
|
||||||
pub source_path: Option<String>,
|
pub source_path: String,
|
||||||
pub first_line_number: usize,
|
pub first_line_number: usize,
|
||||||
pub obj_name: String, // Name of the object that created this code object
|
pub obj_name: String, // Name of the object that created this code object
|
||||||
pub is_generator: bool,
|
pub is_generator: bool,
|
||||||
|
@ -269,7 +269,7 @@ impl CodeObject {
|
||||||
varargs: Varargs,
|
varargs: Varargs,
|
||||||
kwonlyarg_names: Vec<String>,
|
kwonlyarg_names: Vec<String>,
|
||||||
varkeywords: Varargs,
|
varkeywords: Varargs,
|
||||||
source_path: Option<String>,
|
source_path: String,
|
||||||
first_line_number: usize,
|
first_line_number: usize,
|
||||||
obj_name: String,
|
obj_name: String,
|
||||||
) -> CodeObject {
|
) -> CodeObject {
|
||||||
|
|
|
@ -23,11 +23,7 @@ struct Compiler {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Compile a given sourcecode into a bytecode object.
|
/// Compile a given sourcecode into a bytecode object.
|
||||||
pub fn compile(
|
pub fn compile(source: &str, mode: &Mode, source_path: String) -> Result<CodeObject, CompileError> {
|
||||||
source: &str,
|
|
||||||
mode: &Mode,
|
|
||||||
source_path: Option<String>,
|
|
||||||
) -> Result<CodeObject, CompileError> {
|
|
||||||
match mode {
|
match mode {
|
||||||
Mode::Exec => {
|
Mode::Exec => {
|
||||||
let ast = parser::parse_program(source)?;
|
let ast = parser::parse_program(source)?;
|
||||||
|
@ -46,11 +42,11 @@ pub fn compile(
|
||||||
|
|
||||||
/// A helper function for the shared code of the different compile functions
|
/// A helper function for the shared code of the different compile functions
|
||||||
fn with_compiler(
|
fn with_compiler(
|
||||||
source_path: Option<String>,
|
source_path: String,
|
||||||
f: impl FnOnce(&mut Compiler) -> Result<(), CompileError>,
|
f: impl FnOnce(&mut Compiler) -> Result<(), CompileError>,
|
||||||
) -> Result<CodeObject, CompileError> {
|
) -> Result<CodeObject, CompileError> {
|
||||||
let mut compiler = Compiler::new();
|
let mut compiler = Compiler::new();
|
||||||
compiler.source_path = source_path;
|
compiler.source_path = Some(source_path);
|
||||||
compiler.push_new_code_object("<module>".to_string());
|
compiler.push_new_code_object("<module>".to_string());
|
||||||
f(&mut compiler)?;
|
f(&mut compiler)?;
|
||||||
let code = compiler.pop_code_object();
|
let code = compiler.pop_code_object();
|
||||||
|
@ -59,10 +55,7 @@ fn with_compiler(
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Compile a standard Python program to bytecode
|
/// Compile a standard Python program to bytecode
|
||||||
pub fn compile_program(
|
pub fn compile_program(ast: ast::Program, source_path: String) -> Result<CodeObject, CompileError> {
|
||||||
ast: ast::Program,
|
|
||||||
source_path: Option<String>,
|
|
||||||
) -> Result<CodeObject, CompileError> {
|
|
||||||
with_compiler(source_path, |compiler| {
|
with_compiler(source_path, |compiler| {
|
||||||
let symbol_table = make_symbol_table(&ast)?;
|
let symbol_table = make_symbol_table(&ast)?;
|
||||||
compiler.compile_program(&ast, symbol_table)
|
compiler.compile_program(&ast, symbol_table)
|
||||||
|
@ -72,7 +65,7 @@ pub fn compile_program(
|
||||||
/// Compile a single Python expression to bytecode
|
/// Compile a single Python expression to bytecode
|
||||||
pub fn compile_statement_eval(
|
pub fn compile_statement_eval(
|
||||||
statement: Vec<ast::LocatedStatement>,
|
statement: Vec<ast::LocatedStatement>,
|
||||||
source_path: Option<String>,
|
source_path: String,
|
||||||
) -> Result<CodeObject, CompileError> {
|
) -> Result<CodeObject, CompileError> {
|
||||||
with_compiler(source_path, |compiler| {
|
with_compiler(source_path, |compiler| {
|
||||||
let symbol_table = statements_to_symbol_table(&statement)?;
|
let symbol_table = statements_to_symbol_table(&statement)?;
|
||||||
|
@ -83,7 +76,7 @@ pub fn compile_statement_eval(
|
||||||
/// Compile a Python program to bytecode for the context of a REPL
|
/// Compile a Python program to bytecode for the context of a REPL
|
||||||
pub fn compile_program_single(
|
pub fn compile_program_single(
|
||||||
ast: ast::Program,
|
ast: ast::Program,
|
||||||
source_path: Option<String>,
|
source_path: String,
|
||||||
) -> Result<CodeObject, CompileError> {
|
) -> Result<CodeObject, CompileError> {
|
||||||
with_compiler(source_path, |compiler| {
|
with_compiler(source_path, |compiler| {
|
||||||
let symbol_table = make_symbol_table(&ast)?;
|
let symbol_table = make_symbol_table(&ast)?;
|
||||||
|
@ -126,7 +119,7 @@ impl Compiler {
|
||||||
Varargs::None,
|
Varargs::None,
|
||||||
Vec::new(),
|
Vec::new(),
|
||||||
Varargs::None,
|
Varargs::None,
|
||||||
self.source_path.clone(),
|
self.source_path.clone().unwrap(),
|
||||||
line_number,
|
line_number,
|
||||||
obj_name,
|
obj_name,
|
||||||
));
|
));
|
||||||
|
@ -603,7 +596,7 @@ impl Compiler {
|
||||||
Varargs::from(&args.vararg),
|
Varargs::from(&args.vararg),
|
||||||
args.kwonlyargs.iter().map(|a| a.arg.clone()).collect(),
|
args.kwonlyargs.iter().map(|a| a.arg.clone()).collect(),
|
||||||
Varargs::from(&args.kwarg),
|
Varargs::from(&args.kwarg),
|
||||||
self.source_path.clone(),
|
self.source_path.clone().unwrap(),
|
||||||
line_number,
|
line_number,
|
||||||
name.to_string(),
|
name.to_string(),
|
||||||
));
|
));
|
||||||
|
@ -856,7 +849,7 @@ impl Compiler {
|
||||||
Varargs::None,
|
Varargs::None,
|
||||||
vec![],
|
vec![],
|
||||||
Varargs::None,
|
Varargs::None,
|
||||||
self.source_path.clone(),
|
self.source_path.clone().unwrap(),
|
||||||
line_number,
|
line_number,
|
||||||
name.to_string(),
|
name.to_string(),
|
||||||
));
|
));
|
||||||
|
@ -1578,7 +1571,7 @@ impl Compiler {
|
||||||
Varargs::None,
|
Varargs::None,
|
||||||
vec![],
|
vec![],
|
||||||
Varargs::None,
|
Varargs::None,
|
||||||
self.source_path.clone(),
|
self.source_path.clone().unwrap(),
|
||||||
line_number,
|
line_number,
|
||||||
name.clone(),
|
name.clone(),
|
||||||
));
|
));
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue