@@ -407,7 +407,7 @@ impl ExecutingFrame<'_> {
407407 ..
408408 } ) = self . code . instructions . get ( self . lasti ( ) as usize )
409409 {
410- Some ( self . last_value_ref ( ) )
410+ Some ( self . top_value ( ) )
411411 } else {
412412 None
413413 }
@@ -670,16 +670,15 @@ impl ExecutingFrame<'_> {
670670 }
671671 bytecode:: Instruction :: Duplicate => {
672672 // Duplicate top of stack
673- let value = self . last_value ( ) ;
674- self . push_value ( value) ;
673+ let value = self . top_value ( ) ;
674+ self . push_value ( value. to_owned ( ) ) ;
675675 Ok ( None )
676676 }
677677 bytecode:: Instruction :: Duplicate2 => {
678678 // Duplicate top 2 of stack
679- let top = self . last_value ( ) ;
680- let second_to_top = self . nth_value ( 1 ) . to_owned ( ) ;
681- self . push_value ( second_to_top) ;
682- self . push_value ( top) ;
679+ let len = self . state . stack . len ( ) ;
680+ self . push_value ( self . state . stack [ len - 2 ] . clone ( ) ) ;
681+ self . push_value ( self . state . stack [ len - 1 ] . clone ( ) ) ;
683682 Ok ( None )
684683 }
685684 // splitting the instructions like this offloads the cost of "dynamic" dispatch (on the
@@ -749,7 +748,7 @@ impl ExecutingFrame<'_> {
749748 bytecode:: Instruction :: DictUpdate => {
750749 let other = self . pop_value ( ) ;
751750 let dict = self
752- . last_value_ref ( )
751+ . top_value ( )
753752 . downcast_ref :: < PyDict > ( )
754753 . expect ( "exact dict expected" ) ;
755754 dict. merge_object ( other, vm) ?;
@@ -994,7 +993,7 @@ impl ExecutingFrame<'_> {
994993 Ok ( None )
995994 }
996995 bytecode:: Instruction :: GetANext => {
997- let aiter = self . last_value ( ) ;
996+ let aiter = self . top_value ( ) ;
998997 let awaitable = vm. call_special_method ( & aiter, identifier ! ( vm, __anext__) , ( ) ) ?;
999998 let awaitable = if awaitable. payload_is :: < PyCoroutine > ( ) {
1000999 awaitable
@@ -1156,11 +1155,11 @@ impl ExecutingFrame<'_> {
11561155
11571156 #[ cfg_attr( feature = "flame-it" , flame( "Frame" ) ) ]
11581157 fn import_from ( & mut self , vm : & VirtualMachine , idx : bytecode:: NameIdx ) -> PyResult {
1159- let module = self . last_value ( ) ;
1158+ let module = self . top_value ( ) ;
11601159 let name = self . code . names [ idx as usize ] ;
11611160 let err = || vm. new_import_error ( format ! ( "cannot import name '{name}'" ) , name. to_owned ( ) ) ;
11621161 // Load attribute, and transform any error into import error.
1163- if let Some ( obj) = vm. get_attribute_opt ( module. clone ( ) , name) ? {
1162+ if let Some ( obj) = vm. get_attribute_opt ( module. to_owned ( ) , name) ? {
11641163 return Ok ( obj) ;
11651164 }
11661165 // fallback to importing '{module.__name__}.{name}' from sys.modules
@@ -1485,7 +1484,7 @@ impl ExecutingFrame<'_> {
14851484 fn execute_yield_from ( & mut self , vm : & VirtualMachine ) -> FrameResult {
14861485 // Value send into iterator:
14871486 let val = self . pop_value ( ) ;
1488- let coro = self . last_value_ref ( ) ;
1487+ let coro = self . top_value ( ) ;
14891488 let result = self . _send ( coro, val, vm) ?;
14901489
14911490 // PyIterReturn returned from e.g. gen.__next__() or gen.send()
@@ -1558,8 +1557,8 @@ impl ExecutingFrame<'_> {
15581557 target : bytecode:: Label ,
15591558 flag : bool ,
15601559 ) -> FrameResult {
1561- let obj = self . last_value ( ) ;
1562- let value = obj. try_to_bool ( vm) ?;
1560+ let obj = self . top_value ( ) ;
1561+ let value = obj. to_owned ( ) . try_to_bool ( vm) ?;
15631562 if value == flag {
15641563 self . jump ( target) ;
15651564 } else {
@@ -1570,7 +1569,7 @@ impl ExecutingFrame<'_> {
15701569
15711570 /// The top of stack contains the iterator, lets push it forward
15721571 fn execute_for_iter ( & mut self , vm : & VirtualMachine , target : bytecode:: Label ) -> FrameResult {
1573- let top_of_stack = PyIter :: new ( self . last_value ( ) ) ;
1572+ let top_of_stack = PyIter :: new ( self . top_value ( ) ) ;
15741573 let next_obj = top_of_stack. next ( vm) ;
15751574
15761575 // Check the next object:
@@ -1946,12 +1945,8 @@ impl ExecutingFrame<'_> {
19461945 }
19471946
19481947 #[ inline]
1949- fn last_value ( & self ) -> PyObjectRef {
1950- self . last_value_ref ( ) . to_owned ( )
1951- }
1952-
1953- #[ inline]
1954- fn last_value_ref ( & self ) -> & PyObject {
1948+ #[ track_caller] // not a real track_caller but pop_value is not very useful
1949+ fn top_value ( & self ) -> & PyObject {
19551950 match & * self . state . stack {
19561951 [ .., last] => last,
19571952 [ ] => self . fatal ( "tried to get top of stack but stack is empty" ) ,
0 commit comments