@@ -14,26 +14,17 @@ internal CLRObject(object ob, IntPtr tp)
14
14
System . Diagnostics . Debug . Assert ( tp != IntPtr . Zero ) ;
15
15
IntPtr py = Runtime . PyType_GenericAlloc ( tp , 0 ) ;
16
16
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
-
28
17
GCHandle gc = AllocGCHandle ( TrackTypes . Wrapper ) ;
29
- Marshal . WriteIntPtr ( py , ObjectOffset . magic ( tp ) , ( IntPtr ) gc ) ;
18
+
30
19
tpHandle = tp ;
31
20
pyHandle = py ;
32
21
inst = ob ;
33
22
23
+ InitGCHandle ( ObjectReference , type : TypeReference , gc ) ;
24
+
34
25
// Fix the BaseException args (and __cause__ in case of Python 3)
35
26
// slot if wrapping a CLR exception
36
- Exceptions . SetArgsAndCause ( py ) ;
27
+ if ( ob is Exception e ) Exceptions . SetArgsAndCause ( e , py ) ;
37
28
}
38
29
39
30
protected CLRObject ( )
@@ -78,6 +69,9 @@ internal static IntPtr GetInstHandle(object ob)
78
69
return co . pyHandle ;
79
70
}
80
71
72
+ internal static NewReference GetReference ( object ob )
73
+ => NewReference . DangerousFromPointer ( GetInstHandle ( ob ) ) ;
74
+
81
75
internal static CLRObject Restore ( object ob , IntPtr pyHandle , InterDomainContext context )
82
76
{
83
77
CLRObject co = new CLRObject ( )
@@ -101,7 +95,7 @@ protected override void OnLoad(InterDomainContext context)
101
95
{
102
96
base . OnLoad ( context ) ;
103
97
GCHandle gc = AllocGCHandle ( TrackTypes . Wrapper ) ;
104
- Marshal . WriteIntPtr ( pyHandle , ObjectOffset . magic ( tpHandle ) , ( IntPtr ) gc ) ;
98
+ ReplaceGCHandle ( ObjectReference , TypeReference , gc ) ;
105
99
}
106
100
}
107
101
}
0 commit comments