@@ -9,7 +9,6 @@ using Serialization: serialize, deserialize
99@kwdef struct PyJuliaValueObject
1010 ob_base:: C.PyObject = C. PyObject ()
1111 value:: Int = 0
12- weaklist:: C.PyPtr = C_NULL
1312end
1413
1514const PyJuliaBase_Type = Ref (C. PyNULL)
@@ -21,9 +20,9 @@ const PYJLVALUES = []
2120const PYJLFREEVALUES = Int[]
2221
2322function _pyjl_new (t:: C.PyPtr , :: C.PyPtr , :: C.PyPtr )
24- o = ccall (UnsafePtr {C.PyTypeObject} (t). alloc[! ], C. PyPtr, (C. PyPtr, C. Py_ssize_t), t, 0 )
23+ allocptr = C. PyType_GetSlot (t, C. Py_tp_alloc)
24+ o = ccall (allocptr, C. PyPtr, (C. PyPtr, C. Py_ssize_t), t, 0 )
2525 o == C. PyNULL && return C. PyNULL
26- UnsafePtr {PyJuliaValueObject} (o). weaklist[] = C. PyNULL
2726 UnsafePtr {PyJuliaValueObject} (o). value[] = 0
2827 return o
2928end
@@ -34,8 +33,8 @@ function _pyjl_dealloc(o::C.PyPtr)
3433 PYJLVALUES[idx] = nothing
3534 push! (PYJLFREEVALUES, idx)
3635 end
37- UnsafePtr {PyJuliaValueObject} (o) . weaklist[ ! ] == C. PyNULL || C . PyObject_ClearWeakRefs (o )
38- ccall (UnsafePtr {C.PyTypeObject} (C . Py_Type (o)) . free[ ! ] , Cvoid, (C. PyPtr,), o)
36+ freeptr = C. PyType_GetSlot (C . Py_Type (o), C . Py_tp_free )
37+ ccall (freeptr , Cvoid, (C. PyPtr,), o)
3938 nothing
4039end
4140
@@ -269,14 +268,12 @@ function _pyjl_deserialize(t::C.PyPtr, v::C.PyPtr)
269268end
270269
271270const _pyjlbase_name = " juliacall.ValueBase"
272- const _pyjlbase_type = fill (C. PyTypeObject ())
273271const _pyjlbase_isnull_name = " _jl_isnull"
274272const _pyjlbase_callmethod_name = " _jl_callmethod"
275273const _pyjlbase_reduce_name = " __reduce__"
276274const _pyjlbase_serialize_name = " _jl_serialize"
277275const _pyjlbase_deserialize_name = " _jl_deserialize"
278276const _pyjlbase_methods = Vector {C.PyMethodDef} ()
279- const _pyjlbase_as_buffer = fill (C. PyBufferProcs ())
280277
281278function init_c ()
282279 empty! (_pyjlbase_methods)
@@ -309,28 +306,37 @@ function init_c()
309306 ),
310307 C. PyMethodDef (),
311308 )
312- _pyjlbase_as_buffer[] = C. PyBufferProcs (
313- get = @cfunction (_pyjl_get_buffer, Cint, (C. PyPtr, Ptr{C. Py_buffer}, Cint)),
314- release = @cfunction (_pyjl_release_buffer, Cvoid, (C. PyPtr, Ptr{C. Py_buffer})),
315- )
316- _pyjlbase_type[] = C. PyTypeObject (
309+ slots = C. PyType_Slot[
310+ C. PyType_Slot (slot = C. Py_tp_dealloc,
311+ pfunc = @cfunction (_pyjl_dealloc, Cvoid, (C. PyPtr,))),
312+ C. PyType_Slot (slot = C. Py_tp_new,
313+ pfunc = @cfunction (_pyjl_new, C. PyPtr,
314+ (C. PyPtr, C. PyPtr, C. PyPtr))),
315+ C. PyType_Slot (slot = C. Py_tp_methods, pfunc = pointer (_pyjlbase_methods)),
316+ C. PyType_Slot (slot = C. Py_bf_getbuffer,
317+ pfunc = @cfunction (_pyjl_get_buffer, Cint,
318+ (C. PyPtr, Ptr{C. Py_buffer}, Cint))),
319+ C. PyType_Slot (slot = C. Py_bf_releasebuffer,
320+ pfunc = @cfunction (_pyjl_release_buffer, Cvoid,
321+ (C. PyPtr, Ptr{C. Py_buffer}))),
322+ C. PyType_Slot (),
323+ ]
324+ spec = C. PyType_Spec (
317325 name = pointer (_pyjlbase_name),
318326 basicsize = sizeof (PyJuliaValueObject),
319- # new = C.POINTERS.PyType_GenericNew,
320- new = @cfunction (_pyjl_new, C. PyPtr, (C. PyPtr, C. PyPtr, C. PyPtr)),
321- dealloc = @cfunction (_pyjl_dealloc, Cvoid, (C. PyPtr,)),
327+ itemsize = 0 ,
322328 flags = C. Py_TPFLAGS_BASETYPE | C. Py_TPFLAGS_HAVE_VERSION_TAG,
323- weaklistoffset = fieldoffset (PyJuliaValueObject, 3 ),
324- # getattro = C.POINTERS.PyObject_GenericGetAttr,
325- # setattro = C.POINTERS.PyObject_GenericSetAttr,
326- methods = pointer (_pyjlbase_methods),
327- as_buffer = pointer (_pyjlbase_as_buffer),
329+ slots = pointer (slots),
328330 )
329- o = PyJuliaBase_Type[] = C. PyPtr (pointer (_pyjlbase_type))
330- if C. PyType_Ready (o) == - 1
331+ spec_ref = Ref (spec)
332+ o = GC. @preserve spec slots _pyjlbase_methods spec_ref begin
333+ C. PyType_FromSpec (Base. unsafe_convert (Ptr{C. PyType_Spec}, spec_ref))
334+ end
335+ if o == C. PyNULL
331336 C. PyErr_Print ()
332337 error (" Error initializing 'juliacall.ValueBase'" )
333338 end
339+ PyJuliaBase_Type[] = o
334340end
335341
336342function __init__ ()
0 commit comments