Skip to content

Commit a5b3925

Browse files
author
Christopher Doris
committed
replace some extras functions with functions from the stable abi
1 parent 6f3d45e commit a5b3925

File tree

4 files changed

+36
-33
lines changed

4 files changed

+36
-33
lines changed

src/C/extras.jl

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,45 @@ asptr(x) = Base.unsafe_convert(PyPtr, x)
22

33
Py_Type(x) = Base.GC.@preserve x PyPtr(UnsafePtr(asptr(x)).type[!])
44

5-
PyObject_Type(x) = Base.GC.@preserve x (t = Py_Type(asptr(x)); Py_IncRef(t); t)
6-
75
Py_TypeCheck(o, t) = Base.GC.@preserve o t PyType_IsSubtype(Py_Type(asptr(o)), asptr(t))
86
Py_TypeCheckFast(o, f::Integer) = Base.GC.@preserve o PyType_IsSubtypeFast(Py_Type(asptr(o)), f)
97

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

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

1512
PyType_CheckBuffer(t) = Base.GC.@preserve t begin
16-
p = UnsafePtr{PyTypeObject}(asptr(t)).as_buffer[]
17-
return p != C_NULL && p.get[!] != C_NULL
13+
o = Ref{PyObject}(PyObject(0, asptr(t)))
14+
PyObject_CheckBuffer(o)
1815
end
1916

20-
PyObject_CheckBuffer(o) = Base.GC.@preserve o PyType_CheckBuffer(Py_Type(asptr(o)))
17+
# PyObject_CheckBuffer(o) = Base.GC.@preserve o PyType_CheckBuffer(Py_Type(asptr(o)))
2118

22-
PyObject_GetBuffer(_o, b, flags) = Base.GC.@preserve _o begin
23-
o = asptr(_o)
24-
p = UnsafePtr{PyTypeObject}(Py_Type(o)).as_buffer[]
25-
if p == C_NULL || p.get[!] == C_NULL
26-
PyErr_SetString(
27-
POINTERS.PyExc_TypeError,
28-
"a bytes-like object is required, not '$(String(UnsafePtr{PyTypeObject}(Py_Type(o)).name[]))'",
29-
)
30-
return Cint(-1)
31-
end
32-
return ccall(p.get[!], Cint, (PyPtr, Ptr{Py_buffer}, Cint), o, b, flags)
33-
end
19+
# PyObject_GetBuffer(_o, b, flags) = Base.GC.@preserve _o begin
20+
# o = asptr(_o)
21+
# p = UnsafePtr{PyTypeObject}(Py_Type(o)).as_buffer[]
22+
# if p == C_NULL || p.get[!] == C_NULL
23+
# PyErr_SetString(
24+
# POINTERS.PyExc_TypeError,
25+
# "a bytes-like object is required, not '$(String(UnsafePtr{PyTypeObject}(Py_Type(o)).name[]))'",
26+
# )
27+
# return Cint(-1)
28+
# end
29+
# return ccall(p.get[!], Cint, (PyPtr, Ptr{Py_buffer}, Cint), o, b, flags)
30+
# end
3431

35-
PyBuffer_Release(_b) = begin
36-
b = UnsafePtr(Base.unsafe_convert(Ptr{Py_buffer}, _b))
37-
o = b.obj[]
38-
o == C_NULL && return
39-
p = UnsafePtr{PyTypeObject}(Py_Type(o)).as_buffer[]
40-
if (p != C_NULL && p.release[!] != C_NULL)
41-
ccall(p.release[!], Cvoid, (PyPtr, Ptr{Py_buffer}), o, b)
42-
end
43-
b.obj[] = C_NULL
44-
Py_DecRef(o)
45-
return
46-
end
32+
# PyBuffer_Release(_b) = begin
33+
# b = UnsafePtr(Base.unsafe_convert(Ptr{Py_buffer}, _b))
34+
# o = b.obj[]
35+
# o == C_NULL && return
36+
# p = UnsafePtr{PyTypeObject}(Py_Type(o)).as_buffer[]
37+
# if (p != C_NULL && p.release[!] != C_NULL)
38+
# ccall(p.release[!], Cvoid, (PyPtr, Ptr{Py_buffer}), o, b)
39+
# end
40+
# b.obj[] = C_NULL
41+
# Py_DecRef(o)
42+
# return
43+
# end
4744

4845
function PyOS_SetInputHook(hook::Ptr{Cvoid})
4946
Base.unsafe_store!(POINTERS.PyOS_InputHookPtr, hook)

src/C/pointers.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,15 @@ const CAPI_FUNC_SIGS = Dict{Symbol,Pair{Tuple,Type}}(
7272
:PyObject_GetIter => (PyPtr,) => PyPtr,
7373
:PyObject_Call => (PyPtr, PyPtr, PyPtr) => PyPtr,
7474
:PyObject_CallObject => (PyPtr, PyPtr) => PyPtr,
75+
:PyObject_Type => (PyPtr,) => PyPtr,
76+
:PyObject_CheckBuffer => (PyPtr,) => Cint,
77+
:PyObject_GetBuffer => (PyPtr, Ptr{Py_buffer}, Cint) => Cint,
7578
# TYPE
7679
:PyType_IsSubtype => (PyPtr, PyPtr) => Cint,
7780
:PyType_Ready => (PyPtr,) => Cint,
7881
:PyType_GenericNew => (PyPtr, PyPtr, PyPtr) => PyPtr,
7982
:PyType_FromSpec => (Ptr{Cvoid},) => PyPtr,
83+
:PyType_GetFlags => (PyPtr,) => Culong,
8084
# MAPPING
8185
:PyMapping_HasKeyString => (PyPtr, Ptr{Cchar}) => Cint,
8286
:PyMapping_SetItemString => (PyPtr, Ptr{Cchar}, PyPtr) => Cint,
@@ -175,6 +179,8 @@ const CAPI_FUNC_SIGS = Dict{Symbol,Pair{Tuple,Type}}(
175179
:PyCapsule_SetName => (PyPtr, Ptr{Cchar}) => Cint,
176180
:PyCapsule_GetPointer => (PyPtr, Ptr{Cchar}) => Ptr{Cvoid},
177181
:PyCapsule_SetDestructor => (PyPtr, Ptr{Cvoid}) => Cint,
182+
# BUFFER
183+
:PyBuffer_Release => (Ptr{Py_buffer},) => Cvoid,
178184
)
179185

180186
const CAPI_EXCEPTIONS = Set([

src/Convert/pyconvert.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ function _pyconvert_get_rules(pytype::Py)
230230
end
231231
end
232232
for (t, x) in reverse(collect(zip(mro, xmro)))
233-
if C.PyType_CheckBuffer(t)
233+
if C.PyType_CheckBuffer(t) == 1
234234
push!(x, "<buffer>")
235235
break
236236
end

src/Wrap/PyArray.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ function pyarray_make(
8383
@debug "failed to make PyArray from __array_interface__" exc = exc
8484
end
8585
end
86-
if buffer && C.PyObject_CheckBuffer(x)
86+
if buffer && C.PyObject_CheckBuffer(x) == 1
8787
try
8888
return pyarray_make(A, x, PyArraySource_Buffer(x))
8989
catch exc

0 commit comments

Comments
 (0)