Skip to content

use getptr a lot less #618

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jul 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
name: Test (${{ matrix.os }}, julia ${{ matrix.jlversion }})
runs-on: ${{ matrix.os }}
strategy:
fail-fast: true
fail-fast: false
matrix:
arch: [x64] # x86 unsupported by MicroMamba
os: [ubuntu-latest, windows-latest, macos-latest]
Expand Down Expand Up @@ -53,7 +53,7 @@ jobs:
name: Test (${{ matrix.os }}, python ${{ matrix.pyversion }})
runs-on: ${{ matrix.os }}
strategy:
fail-fast: true
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
pyversion: [">=3.8", "3.8"]
Expand Down
29 changes: 16 additions & 13 deletions src/C/extras.jl
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
Py_Type(x::PyPtr) = PyPtr(UnsafePtr(x).type[!])
asptr(x) = Base.unsafe_convert(PyPtr, x)

PyObject_Type(x::PyPtr) = (t = Py_Type(x); Py_IncRef(t); t)
Py_Type(x) = Base.GC.@preserve x PyPtr(UnsafePtr(asptr(x)).type[!])

Py_TypeCheck(o::PyPtr, t::PyPtr) = PyType_IsSubtype(Py_Type(o), t)
Py_TypeCheckFast(o::PyPtr, f::Integer) = PyType_IsSubtypeFast(Py_Type(o), f)
PyObject_Type(x) = Base.GC.@preserve x (t = Py_Type(asptr(x)); Py_IncRef(t); t)

PyType_IsSubtypeFast(t::PyPtr, f::Integer) =
Cint(!iszero(UnsafePtr{PyTypeObject}(t).flags[] & f))
Py_TypeCheck(o, t) = Base.GC.@preserve o t PyType_IsSubtype(Py_Type(asptr(o)), asptr(t))
Py_TypeCheckFast(o, f::Integer) = Base.GC.@preserve o PyType_IsSubtypeFast(Py_Type(asptr(o)), f)

PyMemoryView_GET_BUFFER(m::PyPtr) = Ptr{Py_buffer}(UnsafePtr{PyMemoryViewObject}(m).view)
PyType_IsSubtypeFast(t, f::Integer) =
Base.GC.@preserve t Cint(!iszero(UnsafePtr{PyTypeObject}(asptr(t)).flags[] & f))

PyType_CheckBuffer(t::PyPtr) = begin
p = UnsafePtr{PyTypeObject}(t).as_buffer[]
PyMemoryView_GET_BUFFER(m) = Base.GC.@preserve m Ptr{Py_buffer}(UnsafePtr{PyMemoryViewObject}(asptr(m)).view)

PyType_CheckBuffer(t) = Base.GC.@preserve t begin
p = UnsafePtr{PyTypeObject}(asptr(t)).as_buffer[]
return p != C_NULL && p.get[!] != C_NULL
end

PyObject_CheckBuffer(o::PyPtr) = PyType_CheckBuffer(Py_Type(o))
PyObject_CheckBuffer(o) = Base.GC.@preserve o PyType_CheckBuffer(Py_Type(asptr(o)))

PyObject_GetBuffer(o::PyPtr, b, flags) = begin
PyObject_GetBuffer(_o, b, flags) = Base.GC.@preserve _o begin
o = asptr(_o)
p = UnsafePtr{PyTypeObject}(Py_Type(o)).as_buffer[]
if p == C_NULL || p.get[!] == C_NULL
PyErr_SetString(
Expand Down Expand Up @@ -61,8 +64,8 @@ function PyOS_RunInputHook()
end
end

function PySimpleObject_GetValue(::Type{T}, o::PyPtr) where {T}
UnsafePtr{PySimpleObject{T}}(o).value[!]
function PySimpleObject_GetValue(::Type{T}, o) where {T}
Base.GC.@preserve o UnsafePtr{PySimpleObject{T}}(asptr(o)).value[!]
end

# FAST REFCOUNTING
Expand Down
2 changes: 1 addition & 1 deletion src/Compat/pycall.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ function init_pycall(PyCall::Module)
end
@eval function PyCall.PyObject(x::Py)
C.CTX.matches_pycall::Bool || error($errmsg)
return $PyCall.PyObject($PyCall.PyPtr(incref(getptr(x))))
return $PyCall.PyObject($PyCall.PyPtr(getptr(incref(x))))
end
end
14 changes: 8 additions & 6 deletions src/Convert/ctypes.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
struct pyconvert_rule_ctypessimplevalue{R,S} <: Function end

function (::pyconvert_rule_ctypessimplevalue{R,SAFE})(::Type{T}, x::Py) where {R,SAFE,T}
ptr = Base.GC.@preserve x C.PySimpleObject_GetValue(Ptr{R}, getptr(x))
ans = unsafe_load(ptr)
if SAFE
pyconvert_return(convert(T, ans))
else
pyconvert_tryconvert(T, ans)
Base.GC.@preserve x begin
ptr = C.PySimpleObject_GetValue(Ptr{R}, x)
ans = unsafe_load(ptr)
if SAFE
pyconvert_return(convert(T, ans))
else
pyconvert_tryconvert(T, ans)
end
end
end

Expand Down
2 changes: 1 addition & 1 deletion src/Convert/numpy.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
struct pyconvert_rule_numpysimplevalue{R,S} <: Function end

function (::pyconvert_rule_numpysimplevalue{R,SAFE})(::Type{T}, x::Py) where {R,SAFE,T}
ans = Base.GC.@preserve x C.PySimpleObject_GetValue(R, getptr(x))
ans = C.PySimpleObject_GetValue(R, x)
if SAFE
pyconvert_return(convert(T, ans))
else
Expand Down
4 changes: 2 additions & 2 deletions src/Convert/pyconvert.jl
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ function _pyconvert_get_rules(pytype::Py)
end
end
for (t, x) in reverse(collect(zip(mro, xmro)))
if C.PyType_CheckBuffer(getptr(t))
if C.PyType_CheckBuffer(t)
push!(x, "<buffer>")
break
end
Expand Down Expand Up @@ -350,7 +350,7 @@ function pytryconvert(::Type{T}, x_) where {T}

# get rules from the cache
# TODO: we should hold weak references and clear the cache if types get deleted
tptr = C.Py_Type(getptr(x))
tptr = C.Py_Type(x)
trules = pyconvert_rules_cache(T)
rules = get!(trules, tptr) do
t = pynew(incref(tptr))
Expand Down
4 changes: 2 additions & 2 deletions src/Convert/rules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ pyconvert_rule_bytes(::Type{Base.CodeUnits{UInt8,String}}, x::Py) =
pyconvert_rule_int(::Type{T}, x::Py) where {T<:Number} = begin
# first try to convert to Clonglong (or Culonglong if unsigned)
v =
T <: Unsigned ? C.PyLong_AsUnsignedLongLong(getptr(x)) :
C.PyLong_AsLongLong(getptr(x))
T <: Unsigned ? C.PyLong_AsUnsignedLongLong(x) :
C.PyLong_AsLongLong(x)
if !iserrset_ambig(v)
# success
return pyconvert_tryconvert(T, v)
Expand Down
11 changes: 8 additions & 3 deletions src/Core/Py.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ pyconvert(::Type{Py}, x::Py) = x

setptr!(x::Py, ptr::C.PyPtr) = (setfield!(x, :ptr, ptr); x)

incref(x::Py) = Base.GC.@preserve x (incref(getptr(x)); x)
decref(x::Py) = Base.GC.@preserve x (decref(getptr(x)); x)

Base.unsafe_convert(::Type{C.PyPtr}, x::Py) = getptr(x)

const PYNULL_CACHE = Py[]

"""
Expand All @@ -75,7 +80,7 @@ const PyNULL = pynew()

pynew(ptr::C.PyPtr) = setptr!(pynew(), ptr)

pynew(x::Py) = pynew(incref(getptr(x)))
pynew(x::Py) = Base.GC.@preserve x pynew(incref(getptr(x)))

"""
pycopy!(dst::Py, src)
Expand Down Expand Up @@ -164,13 +169,13 @@ Base.print(io::IO, x::Py) = print(io, string(x))

function Base.show(io::IO, x::Py)
if get(io, :typeinfo, Any) == Py
if getptr(x) == C.PyNULL
if pyisnull(x)
print(io, "NULL")
else
print(io, pyrepr(String, x))
end
else
if getptr(x) == C.PyNULL
if pyisnull(x)
print(io, "<py NULL>")
else
s = pyrepr(String, x)
Expand Down
Loading
Loading