Skip to content

Commit ce1523c

Browse files
committed
Fix panic on import statement with levels but no module name.
1 parent 19e783d commit ce1523c

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

bytecode/src/bytecode.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,12 @@ pub enum ConversionFlag {
8080
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
8181
pub enum Instruction {
8282
Import {
83-
name: String,
83+
name: Option<String>,
8484
symbols: Vec<String>,
8585
level: usize,
8686
},
8787
ImportStar {
88-
name: String,
88+
name: Option<String>,
8989
level: usize,
9090
},
9191
ImportFrom {
@@ -380,8 +380,13 @@ impl Instruction {
380380
name,
381381
symbols,
382382
level,
383-
} => w!(Import, name, format!("{:?}", symbols), level),
384-
ImportStar { name, level } => w!(ImportStar, name, level),
383+
} => w!(
384+
Import,
385+
format!("{:?}", name),
386+
format!("{:?}", symbols),
387+
level
388+
),
389+
ImportStar { name, level } => w!(ImportStar, format!("{:?}", name), level),
385390
ImportFrom { name } => w!(ImportFrom, name),
386391
LoadName { name, scope } => w!(LoadName, name, format!("{:?}", scope)),
387392
StoreName { name, scope } => w!(StoreName, name, format!("{:?}", scope)),

compiler/src/compile.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ impl Compiler {
271271
// import a, b, c as d
272272
for name in names {
273273
self.emit(Instruction::Import {
274-
name: name.symbol.clone(),
274+
name: Some(name.symbol.clone()),
275275
symbols: vec![],
276276
level: 0,
277277
});
@@ -293,7 +293,7 @@ impl Compiler {
293293
if import_star {
294294
// from .... import *
295295
self.emit(Instruction::ImportStar {
296-
name: module.clone().unwrap(),
296+
name: module.clone(),
297297
level: *level,
298298
});
299299
} else {
@@ -303,7 +303,7 @@ impl Compiler {
303303

304304
// Load module once:
305305
self.emit(Instruction::Import {
306-
name: module.clone().unwrap(),
306+
name: module.clone(),
307307
symbols: from_list,
308308
level: *level,
309309
});

vm/src/frame.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -923,15 +923,16 @@ impl Frame {
923923
fn import(
924924
&self,
925925
vm: &VirtualMachine,
926-
module: &str,
926+
module: &Option<String>,
927927
symbols: &[String],
928928
level: usize,
929929
) -> FrameResult {
930+
let module = module.clone().unwrap_or_default();
930931
let from_list = symbols
931932
.iter()
932933
.map(|symbol| vm.ctx.new_str(symbol.to_string()))
933934
.collect();
934-
let module = vm.import(module, &vm.ctx.new_tuple(from_list), level)?;
935+
let module = vm.import(&module, &vm.ctx.new_tuple(from_list), level)?;
935936

936937
self.push_value(module);
937938
Ok(None)
@@ -949,8 +950,14 @@ impl Frame {
949950
}
950951

951952
#[cfg_attr(feature = "flame-it", flame("Frame"))]
952-
fn import_star(&self, vm: &VirtualMachine, module: &str, level: usize) -> FrameResult {
953-
let module = vm.import(module, &vm.ctx.new_tuple(vec![]), level)?;
953+
fn import_star(
954+
&self,
955+
vm: &VirtualMachine,
956+
module: &Option<String>,
957+
level: usize,
958+
) -> FrameResult {
959+
let module = module.clone().unwrap_or_default();
960+
let module = vm.import(&module, &vm.ctx.new_tuple(vec![]), level)?;
954961

955962
// Grab all the names from the module and put them in the context
956963
if let Some(dict) = &module.dict {

0 commit comments

Comments
 (0)