@@ -33,7 +33,7 @@ internal class ClassManager
3333 BindingFlags . Public |
3434 BindingFlags . NonPublic ;
3535
36- private static Dictionary < MaybeType , PyType > cache = new ( capacity : 128 ) ;
36+ internal static Dictionary < MaybeType , ReflectedClrType > cache = new ( capacity : 128 ) ;
3737 private static readonly Type dtype ;
3838
3939 private ClassManager ( )
@@ -98,7 +98,7 @@ private static int TraverseTypeClear(BorrowedReference ob, IntPtr arg)
9898
9999 internal static ClassManagerState SaveRuntimeData ( )
100100 {
101- var contexts = new Dictionary < PyType , InterDomainContext > ( PythonReferenceComparer . Instance ) ;
101+ var contexts = new Dictionary < ReflectedClrType , InterDomainContext > ( ) ;
102102 foreach ( var cls in cache )
103103 {
104104 if ( ! cls . Key . Valid )
@@ -147,7 +147,7 @@ internal static ClassManagerState SaveRuntimeData()
147147 internal static void RestoreRuntimeData ( ClassManagerState storage )
148148 {
149149 cache = storage . Cache ;
150- var invalidClasses = new List < KeyValuePair < MaybeType , PyType > > ( ) ;
150+ var invalidClasses = new List < KeyValuePair < MaybeType , ReflectedClrType > > ( ) ;
151151 var contexts = storage . Contexts ;
152152 foreach ( var pair in cache )
153153 {
@@ -156,20 +156,8 @@ internal static void RestoreRuntimeData(ClassManagerState storage)
156156 invalidClasses . Add ( pair ) ;
157157 continue ;
158158 }
159- // Ensure, that matching Python type exists first.
160- // It is required for self-referential classes
161- // (e.g. with members, that refer to the same class)
162- var cb = ( ClassBase ) ManagedType . GetManagedObject ( pair . Value ) ! ;
163- var pyType = InitPyType ( pair . Key . Value , cb ) ;
164- // re-init the class
165- InitClassBase ( pair . Key . Value , cb , pyType ) ;
166- // We modified the Type object, notify it we did.
167- Runtime . PyType_Modified ( pair . Value ) ;
168- var context = contexts [ pair . Value ] ;
169- cb . Load ( pyType , context ) ;
170- var slotsHolder = TypeManager . GetSlotsHolder ( pyType ) ;
171- cb . InitializeSlots ( pyType , slotsHolder ) ;
172- Runtime . PyType_Modified ( pair . Value ) ;
159+
160+ pair . Value . Restore ( contexts [ pair . Value ] ) ;
173161 }
174162
175163 foreach ( var pair in invalidClasses )
@@ -183,33 +171,10 @@ internal static void RestoreRuntimeData(ClassManagerState storage)
183171 /// Return the ClassBase-derived instance that implements a particular
184172 /// reflected managed type, creating it if it doesn't yet exist.
185173 /// </summary>
186- internal static PyType GetClass ( Type type , out ClassBase cb )
187- {
188- cache . TryGetValue ( type , out var pyType ) ;
189- if ( pyType != null )
190- {
191- cb = ( ClassBase ) ManagedType . GetManagedObject ( pyType ) ! ;
192- return pyType ;
193- }
194- cb = CreateClass ( type ) ;
195- // Ensure, that matching Python type exists first.
196- // It is required for self-referential classes
197- // (e.g. with members, that refer to the same class)
198- pyType = InitPyType ( type , cb ) ;
199- cache . Add ( type , pyType ) ;
200- // Initialize the object later, as this might call this GetClass method
201- // recursively (for example when a nested class inherits its declaring class...)
202- InitClassBase ( type , cb , pyType ) ;
203- return pyType ;
204- }
205- /// <summary>
206- /// Return the ClassBase-derived instance that implements a particular
207- /// reflected managed type, creating it if it doesn't yet exist.
208- /// </summary>
209- internal static PyType GetClass ( Type type ) => GetClass ( type , out _ ) ;
174+ internal static ReflectedClrType GetClass ( Type type ) => ReflectedClrType . GetOrCreate ( type , out _ ) ;
210175 internal static ClassBase GetClassImpl ( Type type )
211176 {
212- GetClass ( type , out var cb ) ;
177+ ReflectedClrType . GetOrCreate ( type , out var cb ) ;
213178 return cb ;
214179 }
215180
@@ -219,7 +184,7 @@ internal static ClassBase GetClassImpl(Type type)
219184 /// managed type. The new object will be associated with a generated
220185 /// Python type object.
221186 /// </summary>
222- private static ClassBase CreateClass ( Type type )
187+ internal static ClassBase CreateClass ( Type type )
223188 {
224189 // Next, select the appropriate managed implementation class.
225190 // Different kinds of types, such as array types or interface
@@ -272,12 +237,7 @@ private static ClassBase CreateClass(Type type)
272237 return impl ;
273238 }
274239
275- private static PyType InitPyType ( Type type , ClassBase impl )
276- {
277- return TypeManager . GetOrCreateClass ( type ) ;
278- }
279-
280- private static void InitClassBase ( Type type , ClassBase impl , PyType pyType )
240+ internal static void InitClassBase ( Type type , ClassBase impl , PyType pyType )
281241 {
282242 // First, we introspect the managed type and build some class
283243 // information, including generating the member descriptors
@@ -286,14 +246,9 @@ private static void InitClassBase(Type type, ClassBase impl, PyType pyType)
286246 ClassInfo info = GetClassInfo ( type ) ;
287247
288248 impl . indexer = info . indexer ;
249+ impl . richcompare . Clear ( ) ;
289250
290- // Now we force initialize the Python type object to reflect the given
291- // managed type, filling the Python type slots with thunks that
292- // point to the managed methods providing the implementation.
293-
294-
295- TypeManager . GetOrInitializeClass ( impl , type ) ;
296-
251+
297252 // Finally, initialize the class __dict__ and return the object.
298253 using var newDict = Runtime . PyObject_GenericGetDict ( pyType . Reference ) ;
299254 BorrowedReference dict = newDict . Borrow ( ) ;
@@ -317,9 +272,10 @@ private static void InitClassBase(Type type, ClassBase impl, PyType pyType)
317272 default :
318273 throw new NotSupportedException ( ) ;
319274 }
320- if ( ClassBase . CilToPyOpMap . TryGetValue ( name , out var pyOp ) )
275+ if ( ClassBase . CilToPyOpMap . TryGetValue ( name , out var pyOp )
276+ && item is MethodObject method )
321277 {
322- impl . richcompare . Add ( pyOp , ( MethodObject ) item ) ;
278+ impl . richcompare . Add ( pyOp , method ) ;
323279 }
324280 }
325281
@@ -570,7 +526,6 @@ private static ClassInfo GetClassInfo(Type type)
570526 }
571527 // Note the given instance might be uninitialized
572528 var pyType = GetClass ( tp ) ;
573- TypeManager . GetOrCreateClass ( tp ) ;
574529 ob = ManagedType . GetManagedObject ( pyType ) ! ;
575530 Debug . Assert ( ob is not null ) ;
576531 ci . members [ mi . Name ] = ob ;
0 commit comments