@@ -30,7 +30,7 @@ use crate::{
3030 function:: { ArgMapping , Either , FuncArgs , PyMethodFlags } ,
3131 object:: PyAtomicBorrow ,
3232 object:: { Traverse , TraverseFn } ,
33- protocol:: { PyIter , PyIterReturn } ,
33+ protocol:: { PyIter , PyIterReturn , PyMapping } ,
3434 scope:: Scope ,
3535 sliceable:: SliceableSequenceOp ,
3636 stdlib:: { _typing, builtins, sys:: monitoring} ,
@@ -609,6 +609,9 @@ pub struct InterpreterFrame {
609609 pub ( crate ) owner : atomic:: AtomicI8 ,
610610 /// Set when f_locals is accessed. Cleared after locals_to_fast() sync.
611611 pub ( crate ) locals_dirty : atomic:: AtomicBool ,
612+ /// Persistent overlay for `frame.f_locals` when hidden locals need a
613+ /// snapshot separate from the backing locals mapping.
614+ pub ( crate ) f_locals_hidden_overlay : PyMutex < Option < PyDictRef > > ,
612615 /// Number of stack entries to pop after set_f_lineno returns to the
613616 /// execution loop. set_f_lineno cannot pop directly because the
614617 /// execution loop holds the state mutex.
@@ -660,6 +663,7 @@ unsafe impl Traverse for Frame {
660663 iframe. builtins . traverse ( tracer_fn) ;
661664 iframe. trace . traverse ( tracer_fn) ;
662665 iframe. temporary_refs . traverse ( tracer_fn) ;
666+ iframe. f_locals_hidden_overlay . traverse ( tracer_fn) ;
663667 }
664668}
665669
@@ -739,6 +743,7 @@ impl Frame {
739743 previous : AtomicPtr :: new ( core:: ptr:: null_mut ( ) ) ,
740744 owner : atomic:: AtomicI8 :: new ( FrameOwner :: FrameObject as i8 ) ,
741745 locals_dirty : atomic:: AtomicBool :: new ( false ) ,
746+ f_locals_hidden_overlay : PyMutex :: new ( None ) ,
742747 pending_stack_pops : Default :: default ( ) ,
743748 pending_unwind_from_stack : Default :: default ( ) ,
744749 } ;
@@ -797,6 +802,7 @@ impl Frame {
797802 for slot in fastlocals. iter_mut ( ) {
798803 * slot = None ;
799804 }
805+ self . f_locals_hidden_overlay . lock ( ) . take ( ) ;
800806 }
801807
802808 /// Get cell contents by localsplus index.
@@ -867,7 +873,14 @@ impl Frame {
867873 let code = & * * self . code ;
868874 // SAFETY: Called before generator resume; no concurrent access.
869875 let fastlocals = unsafe { ( * self . iframe . get ( ) ) . localsplus . fastlocals_mut ( ) } ;
870- let locals_map = self . locals . mapping ( vm) ;
876+ let overlay_locals = self
877+ . has_active_hidden_locals ( )
878+ . then ( || self . f_locals_hidden_overlay . lock ( ) . clone ( ) )
879+ . flatten ( )
880+ . map ( ArgMapping :: from_dict_exact) ;
881+ let locals_map = overlay_locals
882+ . as_ref ( )
883+ . map_or_else ( || self . locals . mapping ( vm) , ArgMapping :: mapping) ;
871884 for ( i, & varname) in code. varnames . iter ( ) . enumerate ( ) {
872885 if i >= fastlocals. len ( ) {
873886 break ;
@@ -882,23 +895,12 @@ impl Frame {
882895 Ok ( ( ) )
883896 }
884897
885- pub fn locals ( & self , vm : & VirtualMachine ) -> PyResult < ArgMapping > {
886- use rustpython_compiler_core:: bytecode:: {
887- CO_FAST_CELL , CO_FAST_FREE , CO_FAST_HIDDEN , CO_FAST_LOCAL ,
888- } ;
889- // SAFETY: Either the frame is not executing (caller checked owner),
890- // or we're in a trace callback on the same thread that's executing.
891- let locals = & self . locals ;
898+ fn has_active_hidden_locals ( & self ) -> bool {
899+ use rustpython_compiler_core:: bytecode:: { CO_FAST_CELL , CO_FAST_FREE , CO_FAST_HIDDEN } ;
892900 let code = & * * self . code ;
893- let locals_map = locals. mapping ( vm) ;
894901 let fastlocals = unsafe { ( * self . iframe . get ( ) ) . localsplus . fastlocals ( ) } ;
895-
896- // Iterate through all localsplus slots using localspluskinds
897- let nlocalsplus = code. localspluskinds . len ( ) ;
898- let nfrees = code. freevars . len ( ) ;
899- let free_start = nlocalsplus - nfrees;
900902 let is_optimized = code. flags . contains ( bytecode:: CodeFlags :: OPTIMIZED ) ;
901- let has_active_hidden_locals = !is_optimized
903+ !is_optimized
902904 && code. localspluskinds . iter ( ) . enumerate ( ) . any ( |( i, & kind) | {
903905 if kind & CO_FAST_HIDDEN == 0 {
904906 return false ;
@@ -914,16 +916,27 @@ impl Frame {
914916 }
915917 }
916918 }
917- } ) ;
918- let overlay_locals = if has_active_hidden_locals {
919- // Match CPython's PyEval_GetLocals() behavior for frames with
920- // PEP 709 hidden locals: locals() inside an inlined comprehension
921- // returns a snapshot of the active fast locals only, not the
922- // backing module/class/eval mapping.
923- Some ( vm. ctx . new_dict ( ) )
924- } else {
925- None
919+ } )
920+ }
921+
922+ fn sync_visible_locals_to_mapping (
923+ & self ,
924+ locals_map : PyMapping < ' _ > ,
925+ vm : & VirtualMachine ,
926+ ) -> PyResult < ( ) > {
927+ use rustpython_compiler_core:: bytecode:: {
928+ CO_FAST_CELL , CO_FAST_FREE , CO_FAST_HIDDEN , CO_FAST_LOCAL ,
926929 } ;
930+ // SAFETY: Either the frame is not executing (caller checked owner),
931+ // or we're in a trace callback on the same thread that's executing.
932+ let code = & * * self . code ;
933+ let fastlocals = unsafe { ( * self . iframe . get ( ) ) . localsplus . fastlocals ( ) } ;
934+
935+ // Iterate through all localsplus slots using localspluskinds
936+ let nlocalsplus = code. localspluskinds . len ( ) ;
937+ let nfrees = code. freevars . len ( ) ;
938+ let free_start = nlocalsplus - nfrees;
939+ let is_optimized = code. flags . contains ( bytecode:: CodeFlags :: OPTIMIZED ) ;
927940
928941 // Track which non-merged cellvar index we're at
929942 let mut nonmerged_cell_idx = 0 ;
@@ -1001,24 +1014,52 @@ impl Frame {
10011014 fastlocals[ i] . clone ( )
10021015 } ;
10031016
1004- let result = if let Some ( dict) = & overlay_locals {
1005- match value {
1006- Some ( value) => dict. set_item ( name, value, vm) ,
1007- None => dict. del_item ( name, vm) ,
1008- }
1009- } else {
1010- locals_map. ass_subscript ( name, value, vm)
1011- } ;
1017+ let result = locals_map. ass_subscript ( name, value, vm) ;
10121018 match result {
10131019 Ok ( ( ) ) => { }
10141020 Err ( e) if e. fast_isinstance ( vm. ctx . exceptions . key_error ) => { }
10151021 Err ( e) => return Err ( e) ,
10161022 }
10171023 }
1018- if let Some ( dict) = overlay_locals {
1019- Ok ( ArgMapping :: from_dict_exact ( dict) )
1024+ Ok ( ( ) )
1025+ }
1026+
1027+ pub fn f_locals_mapping ( & self , vm : & VirtualMachine ) -> PyResult < ArgMapping > {
1028+ if !self . has_active_hidden_locals ( ) {
1029+ self . f_locals_hidden_overlay . lock ( ) . take ( ) ;
1030+ return self . locals ( vm) ;
1031+ }
1032+
1033+ let needs_refresh = !self . locals_dirty . load ( atomic:: Ordering :: Acquire ) ;
1034+ let overlay_dict = {
1035+ let mut overlay = self . f_locals_hidden_overlay . lock ( ) ;
1036+ match overlay. as_ref ( ) {
1037+ Some ( dict) => dict. clone ( ) ,
1038+ None => {
1039+ let dict = vm. ctx . new_dict ( ) ;
1040+ * overlay = Some ( dict. clone ( ) ) ;
1041+ dict
1042+ }
1043+ }
1044+ } ;
1045+ if needs_refresh {
1046+ PyDict :: clear ( & overlay_dict) ;
1047+ let overlay = ArgMapping :: from_dict_exact ( overlay_dict. clone ( ) ) ;
1048+ self . sync_visible_locals_to_mapping ( overlay. mapping ( ) , vm) ?;
1049+ }
1050+ Ok ( ArgMapping :: from_dict_exact ( overlay_dict) )
1051+ }
1052+
1053+ pub fn locals ( & self , vm : & VirtualMachine ) -> PyResult < ArgMapping > {
1054+ if self . has_active_hidden_locals ( ) {
1055+ // Match CPython's locals() behavior for frames with PEP 709 hidden
1056+ // locals: return a fresh snapshot instead of the backing mapping.
1057+ let overlay = ArgMapping :: from_dict_exact ( vm. ctx . new_dict ( ) ) ;
1058+ self . sync_visible_locals_to_mapping ( overlay. mapping ( ) , vm) ?;
1059+ Ok ( overlay)
10201060 } else {
1021- Ok ( locals. clone_mapping ( vm) )
1061+ self . sync_visible_locals_to_mapping ( self . locals . mapping ( vm) , vm) ?;
1062+ Ok ( self . locals . clone_mapping ( vm) )
10221063 }
10231064 }
10241065}
0 commit comments