Skip to content

Commit aefbae4

Browse files
authored
Revert "Make CodeObject.source_path an Option<String>"
1 parent 86b8c07 commit aefbae4

File tree

13 files changed

+51
-59
lines changed

13 files changed

+51
-59
lines changed

compiler/src/bytecode.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub struct CodeObject {
2121
pub varargs: Varargs, // *args or *
2222
pub kwonlyarg_names: Vec<String>,
2323
pub varkeywords: Varargs, // **kwargs or **
24-
pub source_path: Option<String>,
24+
pub source_path: String,
2525
pub first_line_number: usize,
2626
pub obj_name: String, // Name of the object that created this code object
2727
pub is_generator: bool,
@@ -269,7 +269,7 @@ impl CodeObject {
269269
varargs: Varargs,
270270
kwonlyarg_names: Vec<String>,
271271
varkeywords: Varargs,
272-
source_path: Option<String>,
272+
source_path: String,
273273
first_line_number: usize,
274274
obj_name: String,
275275
) -> CodeObject {

compiler/src/compile.rs

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,7 @@ struct Compiler {
2323
}
2424

2525
/// Compile a given sourcecode into a bytecode object.
26-
pub fn compile(
27-
source: &str,
28-
mode: &Mode,
29-
source_path: Option<String>,
30-
) -> Result<CodeObject, CompileError> {
26+
pub fn compile(source: &str, mode: &Mode, source_path: String) -> Result<CodeObject, CompileError> {
3127
match mode {
3228
Mode::Exec => {
3329
let ast = parser::parse_program(source)?;
@@ -46,11 +42,11 @@ pub fn compile(
4642

4743
/// A helper function for the shared code of the different compile functions
4844
fn with_compiler(
49-
source_path: Option<String>,
45+
source_path: String,
5046
f: impl FnOnce(&mut Compiler) -> Result<(), CompileError>,
5147
) -> Result<CodeObject, CompileError> {
5248
let mut compiler = Compiler::new();
53-
compiler.source_path = source_path;
49+
compiler.source_path = Some(source_path);
5450
compiler.push_new_code_object("<module>".to_string());
5551
f(&mut compiler)?;
5652
let code = compiler.pop_code_object();
@@ -59,10 +55,7 @@ fn with_compiler(
5955
}
6056

6157
/// Compile a standard Python program to bytecode
62-
pub fn compile_program(
63-
ast: ast::Program,
64-
source_path: Option<String>,
65-
) -> Result<CodeObject, CompileError> {
58+
pub fn compile_program(ast: ast::Program, source_path: String) -> Result<CodeObject, CompileError> {
6659
with_compiler(source_path, |compiler| {
6760
let symbol_table = make_symbol_table(&ast)?;
6861
compiler.compile_program(&ast, symbol_table)
@@ -72,7 +65,7 @@ pub fn compile_program(
7265
/// Compile a single Python expression to bytecode
7366
pub fn compile_statement_eval(
7467
statement: Vec<ast::LocatedStatement>,
75-
source_path: Option<String>,
68+
source_path: String,
7669
) -> Result<CodeObject, CompileError> {
7770
with_compiler(source_path, |compiler| {
7871
let symbol_table = statements_to_symbol_table(&statement)?;
@@ -83,7 +76,7 @@ pub fn compile_statement_eval(
8376
/// Compile a Python program to bytecode for the context of a REPL
8477
pub fn compile_program_single(
8578
ast: ast::Program,
86-
source_path: Option<String>,
79+
source_path: String,
8780
) -> Result<CodeObject, CompileError> {
8881
with_compiler(source_path, |compiler| {
8982
let symbol_table = make_symbol_table(&ast)?;
@@ -126,7 +119,7 @@ impl Compiler {
126119
Varargs::None,
127120
Vec::new(),
128121
Varargs::None,
129-
self.source_path.clone(),
122+
self.source_path.clone().unwrap(),
130123
line_number,
131124
obj_name,
132125
));
@@ -603,7 +596,7 @@ impl Compiler {
603596
Varargs::from(&args.vararg),
604597
args.kwonlyargs.iter().map(|a| a.arg.clone()).collect(),
605598
Varargs::from(&args.kwarg),
606-
self.source_path.clone(),
599+
self.source_path.clone().unwrap(),
607600
line_number,
608601
name.to_string(),
609602
));
@@ -856,7 +849,7 @@ impl Compiler {
856849
Varargs::None,
857850
vec![],
858851
Varargs::None,
859-
self.source_path.clone(),
852+
self.source_path.clone().unwrap(),
860853
line_number,
861854
name.to_string(),
862855
));
@@ -1578,7 +1571,7 @@ impl Compiler {
15781571
Varargs::None,
15791572
vec![],
15801573
Varargs::None,
1581-
self.source_path.clone(),
1574+
self.source_path.clone().unwrap(),
15821575
line_number,
15831576
name.clone(),
15841577
));

derive/src/compile_bytecode.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,7 @@ struct CompilationSource {
3535
}
3636

3737
impl CompilationSource {
38-
fn compile(
39-
self,
40-
mode: &compile::Mode,
41-
source_path: Option<String>,
42-
) -> Result<CodeObject, Diagnostic> {
38+
fn compile(self, mode: &compile::Mode, source_path: String) -> Result<CodeObject, Diagnostic> {
4339
let compile = |source| {
4440
compile::compile(source, mode, source_path).map_err(|err| {
4541
Diagnostic::spans_error(self.span, format!("Compile error: {}", err))
@@ -139,7 +135,10 @@ impl PyCompileInput {
139135
"Must have either file or source in py_compile_bytecode!()",
140136
)
141137
})?
142-
.compile(&mode.unwrap_or(compile::Mode::Exec), source_path)
138+
.compile(
139+
&mode.unwrap_or(compile::Mode::Exec),
140+
source_path.unwrap_or_else(|| "frozen".to_string()),
141+
)
143142
}
144143
}
145144

src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ fn main() {
7676

7777
fn _run_string(vm: &VirtualMachine, source: &str, source_path: String) -> PyResult {
7878
let code_obj = vm
79-
.compile(source, &compile::Mode::Exec, Some(source_path.clone()))
79+
.compile(source, &compile::Mode::Exec, source_path.clone())
8080
.map_err(|err| vm.new_syntax_error(&err))?;
8181
// trace!("Code object: {:?}", code_obj.borrow());
8282
let attrs = vm.ctx.new_dict();
@@ -161,7 +161,7 @@ fn test_run_script() {
161161
}
162162

163163
fn shell_exec(vm: &VirtualMachine, source: &str, scope: Scope) -> Result<(), CompileError> {
164-
match vm.compile(source, &compile::Mode::Single, Some("<stdin>".to_string())) {
164+
match vm.compile(source, &compile::Mode::Single, "<stdin>".to_string()) {
165165
Ok(code) => {
166166
match vm.run_code_obj(code, scope.clone()) {
167167
Ok(value) => {

vm/src/builtins.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ fn builtin_compile(args: CompileArgs, vm: &VirtualMachine) -> PyResult<PyCodeRef
122122
}
123123
};
124124

125-
vm.compile(&source, &mode, Some(args.filename.value.to_string()))
125+
vm.compile(&source, &mode, args.filename.value.to_string())
126126
.map_err(|err| vm.new_syntax_error(&err))
127127
}
128128

@@ -171,7 +171,7 @@ fn builtin_eval(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
171171
let source = objstr::get_value(source);
172172
// TODO: fix this newline bug:
173173
let source = format!("{}\n", source);
174-
vm.compile(&source, &mode, Some("<string>".to_string()))
174+
vm.compile(&source, &mode, "<string>".to_string())
175175
.map_err(|err| vm.new_syntax_error(&err))?
176176
} else {
177177
return Err(vm.new_type_error("code argument must be str or code object".to_string()));
@@ -199,7 +199,7 @@ fn builtin_exec(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
199199
let source = objstr::get_value(source);
200200
// TODO: fix this newline bug:
201201
let source = format!("{}\n", source);
202-
vm.compile(&source, &mode, Some("<string>".to_string()))
202+
vm.compile(&source, &mode, "<string>".to_string())
203203
.map_err(|err| vm.new_syntax_error(&err))?
204204
} else if let Ok(code_obj) = PyCodeRef::try_from_object(vm, source.clone()) {
205205
code_obj

vm/src/eval.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1+
extern crate rustpython_parser;
2+
13
use crate::compile;
24
use crate::frame::Scope;
35
use crate::pyobject::PyResult;
46
use crate::vm::VirtualMachine;
57

6-
pub fn eval(
7-
vm: &VirtualMachine,
8-
source: &str,
9-
scope: Scope,
10-
source_path: Option<String>,
11-
) -> PyResult {
12-
match vm.compile(source, &compile::Mode::Eval, source_path) {
8+
pub fn eval(vm: &VirtualMachine, source: &str, scope: Scope, source_path: &str) -> PyResult {
9+
match vm.compile(source, &compile::Mode::Eval, source_path.to_string()) {
1310
Ok(bytecode) => {
1411
debug!("Code object: {:?}", bytecode);
1512
vm.run_code_obj(bytecode, scope)
@@ -28,7 +25,7 @@ mod tests {
2825
let source = String::from("print('Hello world')\n");
2926
let mut vm = VirtualMachine::new();
3027
let vars = vm.new_scope_with_builtins();
31-
let _result = eval(&mut vm, &source, vars, Some("<unittest>".to_string()));
28+
let _result = eval(&mut vm, &source, vars, "<unittest>");
3229

3330
// TODO: check result?
3431
//assert_eq!(

vm/src/frame.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -271,10 +271,7 @@ impl Frame {
271271
}
272272

273273
pub fn run(&self, vm: &VirtualMachine) -> Result<ExecutionResult, PyObjectRef> {
274-
let filename = match self.code.source_path.clone() {
275-
Some(s) => vm.ctx.new_str(s),
276-
None => vm.get_none(),
277-
};
274+
let filename = &self.code.source_path.to_string();
278275

279276
// This is the name of the object being run:
280277
let run_obj_name = &self.code.obj_name.to_string();
@@ -302,7 +299,7 @@ impl Frame {
302299
.unwrap();
303300
trace!("Adding to traceback: {:?} {:?}", traceback, lineno);
304301
let raise_location = vm.ctx.new_tuple(vec![
305-
filename.clone(),
302+
vm.ctx.new_str(filename.clone()),
306303
vm.ctx.new_int(lineno.get_row()),
307304
vm.ctx.new_str(run_obj_name.clone()),
308305
]);

vm/src/import.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@ pub fn init_importlib(vm: &VirtualMachine) -> PyResult {
2525
}
2626

2727
pub fn import_frozen(vm: &VirtualMachine, module_name: &str) -> PyResult {
28-
vm.frozen
29-
.borrow()
30-
.get(module_name)
31-
.cloned()
32-
.ok_or_else(|| vm.new_import_error(format!("Cannot import frozen module {}", module_name)))
33-
.and_then(|frozen| import_codeobj(vm, module_name, frozen))
28+
if let Some(frozen) = vm.frozen.borrow().get(module_name) {
29+
let mut frozen = frozen.clone();
30+
frozen.source_path = format!("frozen {}", module_name);
31+
import_codeobj(vm, module_name, frozen)
32+
} else {
33+
Err(vm.new_import_error(format!("Cannot import frozen module {}", module_name)))
34+
}
3435
}
3536

3637
pub fn import_builtin(vm: &VirtualMachine, module_name: &str) -> PyResult {
@@ -68,7 +69,7 @@ pub fn import_module(vm: &VirtualMachine, current_path: PathBuf, module_name: &s
6869
import_file(
6970
vm,
7071
module_name,
71-
Some(file_path.to_str().unwrap().to_string()),
72+
file_path.to_str().unwrap().to_string(),
7273
source,
7374
)
7475
}
@@ -77,7 +78,7 @@ pub fn import_module(vm: &VirtualMachine, current_path: PathBuf, module_name: &s
7778
pub fn import_file(
7879
vm: &VirtualMachine,
7980
module_name: &str,
80-
file_path: Option<String>,
81+
file_path: String,
8182
content: String,
8283
) -> PyResult {
8384
let code_obj = compile::compile(&content, &compile::Mode::Exec, file_path)
@@ -88,8 +89,10 @@ pub fn import_file(
8889
pub fn import_codeobj(vm: &VirtualMachine, module_name: &str, code_obj: CodeObject) -> PyResult {
8990
let attrs = vm.ctx.new_dict();
9091
attrs.set_item("__name__", vm.new_str(module_name.to_string()), vm)?;
91-
if let Some(source_path) = &code_obj.source_path {
92-
attrs.set_item("__file__", vm.new_str(source_path.to_owned()), vm)?;
92+
let file_path = &code_obj.source_path;
93+
if !file_path.starts_with("frozen") {
94+
// TODO: Should be less hacky, not depend on source_path
95+
attrs.set_item("__file__", vm.new_str(file_path.to_owned()), vm)?;
9396
}
9497
let module = vm.ctx.new_module(module_name, attrs.clone());
9598

vm/src/obj/objcode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ impl PyCodeRef {
5454
self.code.arg_names.len()
5555
}
5656

57-
fn co_filename(self, _vm: &VirtualMachine) -> Option<String> {
57+
fn co_filename(self, _vm: &VirtualMachine) -> String {
5858
self.code.source_path.clone()
5959
}
6060

vm/src/stdlib/imp.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,11 @@ fn imp_get_frozen_object(name: PyStringRef, vm: &VirtualMachine) -> PyResult<PyC
5858
vm.frozen
5959
.borrow()
6060
.get(name.as_str())
61-
.cloned()
62-
.map(PyCode::new)
61+
.map(|frozen| {
62+
let mut frozen = frozen.clone();
63+
frozen.source_path = format!("frozen {}", name.as_str());
64+
PyCode::new(frozen)
65+
})
6366
.ok_or_else(|| {
6467
vm.new_import_error(format!("No such frozen object named {}", name.as_str()))
6568
})

0 commit comments

Comments
 (0)