Skip to content

Commit 9ba6027

Browse files
authored
Merge pull request RustPython#4979 from patrickzbhe/del-local-scope
Check variables in local scope in DeleteFast
2 parents 050f0bd + 8312831 commit 9ba6027

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

extra_tests/snippets/syntax_del.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,11 @@ def __del__(self):
2929
def test_del_panic():
3030
mytest = MyTest()
3131
del mytest
32+
33+
# see https://github.com/RustPython/RustPython/issues/4910
34+
35+
def f():
36+
del b # noqa
37+
38+
b = 'a'
39+
assert_raises(UnboundLocalError, f)

vm/src/frame.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,18 @@ impl ExecutingFrame<'_> {
607607
Ok(None)
608608
}
609609
bytecode::Instruction::DeleteFast(idx) => {
610-
self.fastlocals.lock()[idx.get(arg) as usize] = None;
610+
let mut fastlocals = self.fastlocals.lock();
611+
let idx = idx.get(arg) as usize;
612+
if fastlocals[idx].is_none() {
613+
return Err(vm.new_exception_msg(
614+
vm.ctx.exceptions.unbound_local_error.to_owned(),
615+
format!(
616+
"local variable '{}' referenced before assignment",
617+
self.code.varnames[idx]
618+
),
619+
));
620+
}
621+
fastlocals[idx] = None;
611622
Ok(None)
612623
}
613624
bytecode::Instruction::DeleteLocal(idx) => {

0 commit comments

Comments
 (0)