@@ -14,26 +14,17 @@ internal CLRObject(object ob, IntPtr tp)
1414 System . Diagnostics . Debug . Assert ( tp != IntPtr . Zero ) ;
1515 IntPtr py = Runtime . PyType_GenericAlloc ( tp , 0 ) ;
1616
17- var flags = ( TypeFlags ) Util . ReadCLong ( tp , TypeOffset . tp_flags ) ;
18- if ( ( flags & TypeFlags . Subclass ) != 0 )
19- {
20- IntPtr dict = Marshal . ReadIntPtr ( py , ObjectOffset . TypeDictOffset ( tp ) ) ;
21- if ( dict == IntPtr . Zero )
22- {
23- dict = Runtime . PyDict_New ( ) ;
24- Marshal . WriteIntPtr ( py , ObjectOffset . TypeDictOffset ( tp ) , dict ) ;
25- }
26- }
27-
2817 GCHandle gc = AllocGCHandle ( TrackTypes . Wrapper ) ;
29- Marshal . WriteIntPtr ( py , ObjectOffset . magic ( tp ) , ( IntPtr ) gc ) ;
18+
3019 tpHandle = tp ;
3120 pyHandle = py ;
3221 inst = ob ;
3322
23+ InitGCHandle ( ObjectReference , type : TypeReference , gc ) ;
24+
3425 // Fix the BaseException args (and __cause__ in case of Python 3)
3526 // slot if wrapping a CLR exception
36- Exceptions . SetArgsAndCause ( py ) ;
27+ if ( ob is Exception e ) Exceptions . SetArgsAndCause ( e , py ) ;
3728 }
3829
3930 protected CLRObject ( )
@@ -78,6 +69,9 @@ internal static IntPtr GetInstHandle(object ob)
7869 return co . pyHandle ;
7970 }
8071
72+ internal static NewReference GetReference ( object ob )
73+ => NewReference . DangerousFromPointer ( GetInstHandle ( ob ) ) ;
74+
8175 internal static CLRObject Restore ( object ob , IntPtr pyHandle , InterDomainContext context )
8276 {
8377 CLRObject co = new CLRObject ( )
@@ -101,7 +95,7 @@ protected override void OnLoad(InterDomainContext context)
10195 {
10296 base . OnLoad ( context ) ;
10397 GCHandle gc = AllocGCHandle ( TrackTypes . Wrapper ) ;
104- Marshal . WriteIntPtr ( pyHandle , ObjectOffset . magic ( tpHandle ) , ( IntPtr ) gc ) ;
98+ ReplaceGCHandle ( ObjectReference , TypeReference , gc ) ;
10599 }
106100 }
107101}
0 commit comments