Skip to content

Commit 9682dc6

Browse files
committed
allocate space for GCHandle in instances of CLR Metatype (which are types themselves)
1 parent 08ea6f3 commit 9682dc6

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

src/runtime/typemanager.cs

+10-7
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ internal static IntPtr GetTypeHandle(ManagedType obj, Type type)
143143
/// </summary>
144144
internal static IntPtr CreateType(Type impl)
145145
{
146-
IntPtr type = AllocateTypeObject(impl.Name);
146+
IntPtr type = AllocateTypeObject(impl.Name, metatype: Runtime.PyTypeType);
147147
int ob_size = ObjectOffset.Size(type);
148148

149149
// Set tp_basicsize to the size of our managed instance objects.
@@ -212,7 +212,7 @@ internal static IntPtr CreateType(ManagedType impl, Type clrType)
212212
base_ = bc.pyHandle;
213213
}
214214

215-
IntPtr type = AllocateTypeObject(name);
215+
IntPtr type = AllocateTypeObject(name, Runtime.PyCLRMetaType);
216216

217217
Marshal.WriteIntPtr(type, TypeOffset.ob_type, Runtime.PyCLRMetaType);
218218
Runtime.XIncref(Runtime.PyCLRMetaType);
@@ -427,12 +427,15 @@ internal static IntPtr CreateMetaType(Type impl, out SlotsHolder slotsHolder)
427427
// the standard type slots, and has to subclass PyType_Type for
428428
// certain functions in the C runtime to work correctly with it.
429429

430-
IntPtr type = AllocateTypeObject("CLR Metatype");
431-
IntPtr py_type = Runtime.PyTypeType;
430+
IntPtr type = AllocateTypeObject("CLR Metatype", metatype: Runtime.PyTypeType);
432431

432+
IntPtr py_type = Runtime.PyTypeType;
433433
Marshal.WriteIntPtr(type, TypeOffset.tp_base, py_type);
434434
Runtime.XIncref(py_type);
435435

436+
int size = TypeOffset.magic() + IntPtr.Size;
437+
Marshal.WriteIntPtr(type, TypeOffset.tp_basicsize, new IntPtr(size));
438+
436439
const int flags = TypeFlags.Default
437440
| TypeFlags.Managed
438441
| TypeFlags.HeapType
@@ -522,7 +525,7 @@ internal static IntPtr BasicSubType(string name, IntPtr base_, Type impl)
522525
// Utility to create a subtype of a std Python type, but with
523526
// a managed type able to override implementation
524527

525-
IntPtr type = AllocateTypeObject(name);
528+
IntPtr type = AllocateTypeObject(name, metatype: Runtime.PyTypeType);
526529
//Marshal.WriteIntPtr(type, TypeOffset.tp_basicsize, (IntPtr)obSize);
527530
//Marshal.WriteIntPtr(type, TypeOffset.tp_itemsize, IntPtr.Zero);
528531

@@ -564,9 +567,9 @@ internal static IntPtr BasicSubType(string name, IntPtr base_, Type impl)
564567
/// <summary>
565568
/// Utility method to allocate a type object &amp; do basic initialization.
566569
/// </summary>
567-
internal static IntPtr AllocateTypeObject(string name)
570+
internal static IntPtr AllocateTypeObject(string name, IntPtr metatype)
568571
{
569-
IntPtr type = Runtime.PyType_GenericAlloc(Runtime.PyTypeType, 0);
572+
IntPtr type = Runtime.PyType_GenericAlloc(metatype, 0);
570573
// Clr type would not use __slots__,
571574
// and the PyMemberDef after PyHeapTypeObject will have other uses(e.g. type handle),
572575
// thus set the ob_size to 0 for avoiding slots iterations.

0 commit comments

Comments
 (0)