Skip to content

Commit d55d884

Browse files
authored
Merge pull request RustPython#854 from sanxiyn/delete-tuple
Implement deleting tuples
2 parents 801c864 + 2aeb0ad commit d55d884

File tree

2 files changed

+40
-24
lines changed

2 files changed

+40
-24
lines changed

tests/snippets/delete.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from testutils import assert_raises
12

23
a = 1
34
del a
@@ -10,3 +11,8 @@ class MyObject: pass
1011

1112
assert not hasattr(foo, 'bar')
1213

14+
x = 1
15+
y = 2
16+
del (x, y)
17+
assert_raises(NameError, lambda: x)
18+
assert_raises(NameError, lambda: y)

vm/src/compile.rs

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -456,30 +456,7 @@ impl Compiler {
456456
}
457457
ast::Statement::Delete { targets } => {
458458
for target in targets {
459-
match target {
460-
ast::Expression::Identifier { name } => {
461-
self.emit(Instruction::DeleteName {
462-
name: name.to_string(),
463-
});
464-
}
465-
ast::Expression::Attribute { value, name } => {
466-
self.compile_expression(value)?;
467-
self.emit(Instruction::DeleteAttr {
468-
name: name.to_string(),
469-
});
470-
}
471-
ast::Expression::Subscript { a, b } => {
472-
self.compile_expression(a)?;
473-
self.compile_expression(b)?;
474-
self.emit(Instruction::DeleteSubscript);
475-
}
476-
_ => {
477-
return Err(CompileError {
478-
error: CompileErrorType::Delete(target.name()),
479-
location: self.current_source_location.clone(),
480-
});
481-
}
482-
}
459+
self.compile_delete(target)?;
483460
}
484461
}
485462
ast::Statement::Pass => {
@@ -489,6 +466,39 @@ impl Compiler {
489466
Ok(())
490467
}
491468

469+
fn compile_delete(&mut self, expression: &ast::Expression) -> Result<(), CompileError> {
470+
match expression {
471+
ast::Expression::Identifier { name } => {
472+
self.emit(Instruction::DeleteName {
473+
name: name.to_string(),
474+
});
475+
}
476+
ast::Expression::Attribute { value, name } => {
477+
self.compile_expression(value)?;
478+
self.emit(Instruction::DeleteAttr {
479+
name: name.to_string(),
480+
});
481+
}
482+
ast::Expression::Subscript { a, b } => {
483+
self.compile_expression(a)?;
484+
self.compile_expression(b)?;
485+
self.emit(Instruction::DeleteSubscript);
486+
}
487+
ast::Expression::Tuple { elements } => {
488+
for element in elements {
489+
self.compile_delete(element)?;
490+
}
491+
}
492+
_ => {
493+
return Err(CompileError {
494+
error: CompileErrorType::Delete(expression.name()),
495+
location: self.current_source_location.clone(),
496+
});
497+
}
498+
}
499+
Ok(())
500+
}
501+
492502
fn enter_function(
493503
&mut self,
494504
name: &str,

0 commit comments

Comments
 (0)