Skip to content

Commit b17b9d3

Browse files
committed
Set __classcell__ if it exists
If `PyCell_Set` is not called with a cell it will raise an exception, which is perfectly valid here.
1 parent d4eac3a commit b17b9d3

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/runtime/runtime.cs

+9
Original file line numberDiff line numberDiff line change
@@ -1966,6 +1966,15 @@ internal static IntPtr PyMem_Realloc(IntPtr ptr, long size)
19661966
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
19671967
internal static extern void PyErr_Print();
19681968

1969+
//====================================================================
1970+
// Cell API
1971+
//====================================================================
1972+
1973+
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
1974+
internal static extern NewReference PyCell_Get(IntPtr cell);
1975+
1976+
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
1977+
internal static extern int PyCell_Set(IntPtr cell, IntPtr value);
19691978

19701979
//====================================================================
19711980
// Miscellaneous

src/runtime/typemanager.cs

+8
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,14 @@ internal static IntPtr CreateSubType(IntPtr py_name, IntPtr py_base_type, IntPtr
280280
IntPtr cls_dict = Marshal.ReadIntPtr(py_type, TypeOffset.tp_dict);
281281
Runtime.PyDict_Update(cls_dict, py_dict);
282282

283+
// Update the __classcell__ if it exists
284+
IntPtr cell = Runtime.PyDict_GetItemString(cls_dict, "__classcell__");
285+
if (cell != IntPtr.Zero)
286+
{
287+
Runtime.PyCell_Set(cell, py_type);
288+
Runtime.PyDict_DelItemString(cls_dict, "__classcell__");
289+
}
290+
283291
return py_type;
284292
}
285293
catch (Exception e)

0 commit comments

Comments
 (0)