@@ -316,6 +316,20 @@ impl VirtualMachine {
316316 None
317317 }
318318
319+ fn delete_name ( & mut self , name : & str ) -> Option < PyResult > {
320+ let locals = match self . current_frame ( ) . locals . borrow ( ) . kind {
321+ PyObjectKind :: Scope { ref scope } => scope. locals . clone ( ) ,
322+ _ => panic ! ( "We really expect our scope to be a scope!" ) ,
323+ } ;
324+
325+ // Assume here that locals is a dict
326+ let name = self . ctx . new_str ( name. to_string ( ) ) ;
327+ match self . call_method ( & locals, "__delitem__" , vec ! [ name] ) {
328+ Ok ( _) => None ,
329+ err => Some ( err) ,
330+ }
331+ }
332+
319333 fn load_name ( & mut self , name : & str ) -> Option < PyResult > {
320334 // Lookup name in scope and put it onto the stack!
321335 let mut scope = self . current_frame ( ) . locals . clone ( ) ;
@@ -432,6 +446,15 @@ impl VirtualMachine {
432446 }
433447 }
434448
449+ fn execute_delete_subscript ( & mut self ) -> Option < PyResult > {
450+ let idx = self . pop_value ( ) ;
451+ let obj = self . pop_value ( ) ;
452+ match self . call_method ( & obj, "__delitem__" , vec ! [ idx] ) {
453+ Ok ( _) => None ,
454+ err => Some ( err) ,
455+ }
456+ }
457+
435458 fn _sub ( & mut self , a : PyObjectRef , b : PyObjectRef ) -> PyResult {
436459 self . call_method ( & a, "__sub__" , vec ! [ b] )
437460 }
@@ -779,6 +802,15 @@ impl VirtualMachine {
779802 None
780803 }
781804
805+ fn delete_attr ( & mut self , attr_name : & str ) -> Option < PyResult > {
806+ let parent = self . pop_value ( ) ;
807+ let name = self . ctx . new_str ( attr_name. to_string ( ) ) ;
808+ match self . call_method ( & parent, "__delattr__" , vec ! [ name] ) {
809+ Ok ( _) => None ,
810+ err => Some ( err) ,
811+ }
812+ }
813+
782814 fn unwrap_constant ( & self , value : & bytecode:: Constant ) -> PyObjectRef {
783815 match * value {
784816 bytecode:: Constant :: Integer { ref value } => self . ctx . new_int ( * value) ,
@@ -823,11 +855,10 @@ impl VirtualMachine {
823855 ref symbol,
824856 } => self . import ( name, symbol) ,
825857 bytecode:: Instruction :: LoadName { ref name } => self . load_name ( name) ,
826- bytecode:: Instruction :: StoreName { ref name } => {
827- // take top of stack and assign in scope:
828- self . store_name ( name)
829- }
858+ bytecode:: Instruction :: StoreName { ref name } => self . store_name ( name) ,
859+ bytecode:: Instruction :: DeleteName { ref name } => self . delete_name ( name) ,
830860 bytecode:: Instruction :: StoreSubscript => self . execute_store_subscript ( ) ,
861+ bytecode:: Instruction :: DeleteSubscript => self . execute_delete_subscript ( ) ,
831862 bytecode:: Instruction :: Pop => {
832863 // Pop value from stack and ignore.
833864 self . pop_value ( ) ;
@@ -957,6 +988,7 @@ impl VirtualMachine {
957988 bytecode:: Instruction :: BinaryOperation { ref op } => self . execute_binop ( op) ,
958989 bytecode:: Instruction :: LoadAttr { ref name } => self . load_attr ( name) ,
959990 bytecode:: Instruction :: StoreAttr { ref name } => self . store_attr ( name) ,
991+ bytecode:: Instruction :: DeleteAttr { ref name } => self . delete_attr ( name) ,
960992 bytecode:: Instruction :: UnaryOperation { ref op } => self . execute_unop ( op) ,
961993 bytecode:: Instruction :: CompareOperation { ref op } => self . execute_compare ( op) ,
962994 bytecode:: Instruction :: ReturnValue => {
0 commit comments