Skip to content

Commit 650139b

Browse files
authored
Merge pull request RustPython#1048 from RustPython/revert-1037-coolreader18/option-source_path
Revert "Make CodeObject.source_path an Option<String>", just add a parameter to import_codeobj
2 parents ba8b733 + c359c43 commit 650139b

File tree

14 files changed

+73
-75
lines changed

14 files changed

+73
-75
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: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
//!
99
//! // the mode to compile the code in
1010
//! mode = "exec", // or "eval" or "single"
11-
//! // the path put into the CodeObject, defaults to `None`
12-
//! source_path = "frozen",
11+
//! // the path put into the CodeObject, defaults to "frozen"
12+
//! module_name = "frozen",
1313
//! )
1414
//! ```
1515
@@ -35,13 +35,9 @@ 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, module_name: String) -> Result<CodeObject, Diagnostic> {
4339
let compile = |source| {
44-
compile::compile(source, mode, source_path).map_err(|err| {
40+
compile::compile(source, mode, module_name).map_err(|err| {
4541
Diagnostic::spans_error(self.span, format!("Compile error: {}", err))
4642
})
4743
};
@@ -73,7 +69,7 @@ struct PyCompileInput {
7369

7470
impl PyCompileInput {
7571
fn compile(&self) -> Result<CodeObject, Diagnostic> {
76-
let mut source_path = None;
72+
let mut module_name = None;
7773
let mut mode = None;
7874
let mut source: Option<CompilationSource> = None;
7975

@@ -101,10 +97,10 @@ impl PyCompileInput {
10197
},
10298
_ => bail_span!(name_value.lit, "mode must be a string"),
10399
})
104-
} else if name_value.ident == "source_path" {
105-
source_path = Some(match &name_value.lit {
100+
} else if name_value.ident == "module_name" {
101+
module_name = Some(match &name_value.lit {
106102
Lit::Str(s) => s.value(),
107-
_ => bail_span!(name_value.lit, "source_path must be string"),
103+
_ => bail_span!(name_value.lit, "module_name must be string"),
108104
})
109105
} else if name_value.ident == "source" {
110106
assert_source_empty(&source)?;
@@ -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+
module_name.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/frozen.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ pub fn get_module_inits() -> HashMap<String, CodeObject> {
55
hashmap! {
66
"__hello__".into() => py_compile_bytecode!(
77
source = "initialized = True; print(\"Hello world!\")\n",
8+
module_name = "__hello__",
89
),
910
"_frozen_importlib".into() => py_compile_bytecode!(
1011
file = "../Lib/importlib/_bootstrap.py",
12+
module_name = "_frozen_importlib",
1113
),
1214
"_frozen_importlib_external".into() => py_compile_bytecode!(
1315
file = "../Lib/importlib/_bootstrap_external.py",
16+
module_name = "_frozen_importlib_external",
1417
),
1518
}
1619
}

vm/src/import.rs

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,21 @@ pub fn import_frozen(vm: &VirtualMachine, module_name: &str) -> PyResult {
2828
vm.frozen
2929
.borrow()
3030
.get(module_name)
31-
.cloned()
3231
.ok_or_else(|| vm.new_import_error(format!("Cannot import frozen module {}", module_name)))
33-
.and_then(|frozen| import_codeobj(vm, module_name, frozen))
32+
.and_then(|frozen| import_codeobj(vm, module_name, frozen.clone(), false))
3433
}
3534

3635
pub fn import_builtin(vm: &VirtualMachine, module_name: &str) -> PyResult {
37-
let sys_modules = vm.get_attribute(vm.sys_module.clone(), "modules").unwrap();
38-
if let Some(make_module_func) = vm.stdlib_inits.borrow().get(module_name) {
39-
let module = make_module_func(vm);
40-
sys_modules.set_item(module_name, module.clone(), vm)?;
41-
Ok(module)
42-
} else {
43-
Err(vm.new_import_error(format!("Cannot import bultin module {}", module_name)))
44-
}
36+
vm.stdlib_inits
37+
.borrow()
38+
.get(module_name)
39+
.ok_or_else(|| vm.new_import_error(format!("Cannot import bultin module {}", module_name)))
40+
.and_then(|make_module_func| {
41+
let module = make_module_func(vm);
42+
let sys_modules = vm.get_attribute(vm.sys_module.clone(), "modules")?;
43+
sys_modules.set_item(module_name, module.clone(), vm)?;
44+
Ok(module)
45+
})
4546
}
4647

4748
pub fn import_module(vm: &VirtualMachine, current_path: PathBuf, module_name: &str) -> PyResult {
@@ -56,8 +57,8 @@ pub fn import_module(vm: &VirtualMachine, current_path: PathBuf, module_name: &s
5657
} else if vm.stdlib_inits.borrow().contains_key(module_name) {
5758
import_builtin(vm, module_name)
5859
} else {
59-
let notfound_error = vm.context().exceptions.module_not_found_error.clone();
60-
let import_error = vm.context().exceptions.import_error.clone();
60+
let notfound_error = &vm.ctx.exceptions.module_not_found_error;
61+
let import_error = &vm.ctx.exceptions.import_error;
6162

6263
// Time to search for module in any place:
6364
let file_path = find_source(vm, current_path, module_name)
@@ -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,24 +78,29 @@ 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)
8485
.map_err(|err| vm.new_syntax_error(&err))?;
85-
import_codeobj(vm, module_name, code_obj)
86+
import_codeobj(vm, module_name, code_obj, true)
8687
}
8788

88-
pub fn import_codeobj(vm: &VirtualMachine, module_name: &str, code_obj: CodeObject) -> PyResult {
89+
pub fn import_codeobj(
90+
vm: &VirtualMachine,
91+
module_name: &str,
92+
code_obj: CodeObject,
93+
set_file_attr: bool,
94+
) -> PyResult {
8995
let attrs = vm.ctx.new_dict();
9096
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)?;
97+
if set_file_attr {
98+
attrs.set_item("__file__", vm.new_str(code_obj.source_path.to_owned()), vm)?;
9399
}
94100
let module = vm.ctx.new_module(module_name, attrs.clone());
95101

96102
// Store module in cache to prevent infinite loop with mutual importing libs:
97-
let sys_modules = vm.get_attribute(vm.sys_module.clone(), "modules").unwrap();
103+
let sys_modules = vm.get_attribute(vm.sys_module.clone(), "modules")?;
98104
sys_modules.set_item(module_name, module.clone(), vm)?;
99105

100106
// Execute main code in module:

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

0 commit comments

Comments
 (0)