Skip to content

Commit 0f736a6

Browse files
author
Christopher Doris
committed
add and use PyTypeObject_DEREF for dereferencing things
1 parent d3384ff commit 0f736a6

File tree

3 files changed

+71
-7
lines changed

3 files changed

+71
-7
lines changed

src/C/consts.jl

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,70 @@ end
322322
tp_versions_used::Cushort = 0
323323
end
324324

325+
# shortened struct for dereferencing purposes, only includes fields common to all
326+
# supported python versions
327+
@kwdef struct PyTypeObject_DEREF
328+
ob_base::PyVarObject = PyVarObject()
329+
name::Cstring = C_NULL # USED
330+
331+
basicsize::Py_ssize_t = 0
332+
itemsize::Py_ssize_t = 0
333+
334+
dealloc::Ptr{Cvoid} = C_NULL
335+
vectorcall_offset::Py_ssize_t = 0
336+
getattr::Ptr{Cvoid} = C_NULL
337+
setattr::Ptr{Cvoid} = C_NULL
338+
as_async::Ptr{Cvoid} = C_NULL
339+
repr::Ptr{Cvoid} = C_NULL
340+
341+
as_number::Ptr{PyNumberMethods} = C_NULL
342+
as_sequence::Ptr{PySequenceMethods} = C_NULL
343+
as_mapping::Ptr{PyMappingMethods} = C_NULL
344+
345+
hash::Ptr{Cvoid} = C_NULL
346+
call::Ptr{Cvoid} = C_NULL
347+
str::Ptr{Cvoid} = C_NULL
348+
getattro::Ptr{Cvoid} = C_NULL
349+
setattro::Ptr{Cvoid} = C_NULL
350+
351+
as_buffer::Ptr{PyBufferProcs} = C_NULL # KEEP
352+
353+
flags::Culong = 0 # USED
354+
355+
doc::Cstring = C_NULL
356+
357+
traverse::Ptr{Cvoid} = C_NULL
358+
359+
clear::Ptr{Cvoid} = C_NULL
360+
361+
richcompare::Ptr{Cvoid} = C_NULL
362+
363+
weaklistoffset::Py_ssize_t = 0
364+
365+
iter::Ptr{Cvoid} = C_NULL
366+
iternext::Ptr{Cvoid} = C_NULL
367+
368+
methods::Ptr{PyMethodDef} = C_NULL
369+
members::Ptr{PyMemberDef} = C_NULL
370+
getset::Ptr{PyGetSetDef} = C_NULL
371+
base::PyPtr = C_NULL
372+
dict::PyPtr = C_NULL
373+
descr_get::Ptr{Cvoid} = C_NULL
374+
descr_set::Ptr{Cvoid} = C_NULL
375+
dictoffset::Py_ssize_t = 0
376+
init::Ptr{Cvoid} = C_NULL
377+
alloc::Ptr{Cvoid} = C_NULL # USED
378+
new::Ptr{Cvoid} = C_NULL
379+
free::Ptr{Cvoid} = C_NULL # USED
380+
is_gc::Ptr{Cvoid} = C_NULL
381+
bases::PyPtr = C_NULL
382+
mro::PyPtr = C_NULL
383+
cache::PyPtr = C_NULL
384+
subclasses::PyPtr = C_NULL
385+
weaklist::PyPtr = C_NULL
386+
del::Ptr{Cvoid} = C_NULL
387+
end
388+
325389
@kwdef struct PySimpleObject{T}
326390
ob_base::PyObject = PyObject()
327391
value::T

src/C/extras.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,24 @@ Py_TypeCheck(o, t) = Base.GC.@preserve o t PyType_IsSubtype(Py_Type(asptr(o)), a
88
Py_TypeCheckFast(o, f::Integer) = Base.GC.@preserve o PyType_IsSubtypeFast(Py_Type(asptr(o)), f)
99

1010
PyType_IsSubtypeFast(t, f::Integer) =
11-
Base.GC.@preserve t Cint(!iszero(UnsafePtr{PyTypeObject}(asptr(t)).flags[] & f))
11+
Base.GC.@preserve t Cint(!iszero(UnsafePtr{PyTypeObject_DEREF}(asptr(t)).flags[] & f))
1212

1313
PyMemoryView_GET_BUFFER(m) = Base.GC.@preserve m Ptr{Py_buffer}(UnsafePtr{PyMemoryViewObject}(asptr(m)).view)
1414

1515
PyType_CheckBuffer(t) = Base.GC.@preserve t begin
16-
p = UnsafePtr{PyTypeObject}(asptr(t)).as_buffer[]
16+
p = UnsafePtr{PyTypeObject_DEREF}(asptr(t)).as_buffer[]
1717
return p != C_NULL && p.get[!] != C_NULL
1818
end
1919

2020
PyObject_CheckBuffer(o) = Base.GC.@preserve o PyType_CheckBuffer(Py_Type(asptr(o)))
2121

2222
PyObject_GetBuffer(_o, b, flags) = Base.GC.@preserve _o begin
2323
o = asptr(_o)
24-
p = UnsafePtr{PyTypeObject}(Py_Type(o)).as_buffer[]
24+
p = UnsafePtr{PyTypeObject_DEREF}(Py_Type(o)).as_buffer[]
2525
if p == C_NULL || p.get[!] == C_NULL
2626
PyErr_SetString(
2727
POINTERS.PyExc_TypeError,
28-
"a bytes-like object is required, not '$(String(UnsafePtr{PyTypeObject}(Py_Type(o)).name[]))'",
28+
"a bytes-like object is required, not '$(String(UnsafePtr{PyTypeObject_DEREF}(Py_Type(o)).name[]))'",
2929
)
3030
return Cint(-1)
3131
end
@@ -36,7 +36,7 @@ PyBuffer_Release(_b) = begin
3636
b = UnsafePtr(Base.unsafe_convert(Ptr{Py_buffer}, _b))
3737
o = b.obj[]
3838
o == C_NULL && return
39-
p = UnsafePtr{PyTypeObject}(Py_Type(o)).as_buffer[]
39+
p = UnsafePtr{PyTypeObject_DEREF}(Py_Type(o)).as_buffer[]
4040
if (p != C_NULL && p.release[!] != C_NULL)
4141
ccall(p.release[!], Cvoid, (PyPtr, Ptr{Py_buffer}), o, b)
4242
end

src/JlWrap/C.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const PYJLVALUES = []
2121
const PYJLFREEVALUES = Int[]
2222

2323
function _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)
24+
o = ccall(UnsafePtr{C.PyTypeObject_DEREF}(t).alloc[!], C.PyPtr, (C.PyPtr, C.Py_ssize_t), t, 0)
2525
o == C.PyNULL && return C.PyNULL
2626
UnsafePtr{PyJuliaValueObject}(o).weaklist[] = C.PyNULL
2727
UnsafePtr{PyJuliaValueObject}(o).value[] = 0
@@ -35,7 +35,7 @@ function _pyjl_dealloc(o::C.PyPtr)
3535
push!(PYJLFREEVALUES, idx)
3636
end
3737
UnsafePtr{PyJuliaValueObject}(o).weaklist[!] == C.PyNULL || C.PyObject_ClearWeakRefs(o)
38-
ccall(UnsafePtr{C.PyTypeObject}(C.Py_Type(o)).free[!], Cvoid, (C.PyPtr,), o)
38+
ccall(UnsafePtr{C.PyTypeObject_DEREF}(C.Py_Type(o)).free[!], Cvoid, (C.PyPtr,), o)
3939
nothing
4040
end
4141

0 commit comments

Comments
 (0)