@@ -14,13 +14,8 @@ namespace Python.Runtime
1414 [ Serializable ]
1515 internal class ModuleObject : ExtensionType
1616 {
17- [ NonSerialized ]
1817 private Dictionary < string , ManagedType > cache ;
1918
20- [ NonSerialized ]
21- // FIXME: Used by reload mode, remove it after implement a delay load handler.
22- private bool _cacheInited ;
23-
2419 internal string moduleName ;
2520 internal IntPtr dict ;
2621 protected string _namespace ;
@@ -33,7 +28,6 @@ public ModuleObject(string name)
3328 }
3429 moduleName = name ;
3530 cache = new Dictionary < string , ManagedType > ( ) ;
36- _cacheInited = true ;
3731 _namespace = name ;
3832
3933 // Use the filename from any of the assemblies just so there's something for
@@ -63,7 +57,7 @@ public ModuleObject(string name)
6357 Runtime . XDecref ( pydocstring ) ;
6458
6559 Runtime . XIncref ( dict ) ;
66- Marshal . WriteIntPtr ( pyHandle , ObjectOffset . DictOffset ( pyHandle ) , dict ) ;
60+ SetObjectDict ( pyHandle , dict ) ;
6761
6862 InitializeModuleMembers ( ) ;
6963 }
@@ -77,11 +71,6 @@ public ModuleObject(string name)
7771 /// </summary>
7872 public ManagedType GetAttribute ( string name , bool guess )
7973 {
80- if ( ! _cacheInited )
81- {
82- // XXX: Used by reload mode.
83- SetupCacheByDict ( ) ;
84- }
8574 ManagedType cached = null ;
8675 cache . TryGetValue ( name , out cached ) ;
8776 if ( cached != null )
@@ -360,46 +349,24 @@ public static int tp_clear(IntPtr ob)
360349 return 0 ;
361350 }
362351
363- protected override void OnSave ( )
352+ protected override void OnSave ( PyObjectSerializeContext context )
364353 {
365- base . OnSave ( ) ;
354+ base . OnSave ( context ) ;
366355 System . Diagnostics . Debug . Assert ( dict == GetObjectDict ( pyHandle ) ) ;
356+ foreach ( var attr in cache . Values )
357+ {
358+ Runtime . XIncref ( attr . pyHandle ) ;
359+ }
367360 // Decref twice in tp_clear, equilibrate them.
368361 Runtime . XIncref ( dict ) ;
369362 Runtime . XIncref ( dict ) ;
370363 }
371364
372- protected override void OnLoad ( )
365+ protected override void OnLoad ( PyObjectSerializeContext context )
373366 {
374- base . OnLoad ( ) ;
375- // XXX: Set the cache after all objects loaded.
376- cache = new Dictionary < string , ManagedType > ( ) ;
377- _cacheInited = false ;
367+ base . OnLoad ( context ) ;
378368 SetObjectDict ( pyHandle , dict ) ;
379369 }
380-
381- private void SetupCacheByDict ( )
382- {
383- System . Diagnostics . Debug . Assert ( ! _cacheInited ) ;
384- _cacheInited = true ;
385- IntPtr key , value ;
386- IntPtr pos ;
387- while ( Runtime . PyDict_Next ( dict , out pos , out key , out value ) != 0 )
388- {
389- ManagedType obj = GetManagedObject ( value ) ;
390- if ( obj == null )
391- {
392- continue ;
393- }
394- string name = Runtime . GetManagedString ( key ) ;
395- if ( cache . ContainsKey ( name ) )
396- {
397- continue ;
398- }
399- Runtime . XIncref ( value ) ;
400- cache . Add ( name , obj ) ;
401- }
402- }
403370 }
404371
405372 /// <summary>
0 commit comments