Skip to content

Commit

Permalink
Make pydecref more idiomatic
Browse files Browse the repository at this point in the history
  • Loading branch information
MilesCranmer committed Jan 8, 2024
1 parent 0a7de84 commit d7181d5
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion benchmarks/pywrapfn.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ See check_pyargsptr(nargs::Int) above
"""
function check_pyargsptr(pf::PyWrapFn{N, RT}) where {N, RT}
if unsafe_load(pf.pyargsptr).ob_refcnt > 1
pydecref_(pf.pyargsptr)
pydecref_unsafe_(pf.pyargsptr)
pf.pyargsptr =
@pycheckn ccall((@pysym :PyTuple_New), PyPtr, (Int,), nargs)
end
Expand Down
12 changes: 6 additions & 6 deletions src/PyCall.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,21 +76,21 @@ mutable struct PyObject
o::PyPtr # the actual PyObject*
function PyObject(o::PyPtr)
po = new(o)
finalizer(_pydecref_locked, po)
finalizer(pydecref_safe_, po)
return po
end
end

const PYDECREF_LOCK = ReentrantLock()

function _pydecref_locked(po::PyObject)
function pydecref_safe_(po::PyObject)
# If available, we lock and decref
!islocked(PYDECREF_LOCK) &&
trylock(() -> (pydecref_(po); true), PYDECREF_LOCK) &&
trylock(() -> (pydecref_unsafe_(po); true), PYDECREF_LOCK) &&
return nothing

# Add back to queue to be decref'd later
finalizer(_pydecref_locked, po)
finalizer(pydecref_safe_, po)
return nothing
end

Expand Down Expand Up @@ -127,13 +127,13 @@ it is equivalent to a `PyNULL()` object.
"""
ispynull(o::PyObject) = o PyPtr_NULL

function pydecref_(o::Union{PyPtr,PyObject})
function pydecref_unsafe_(o::Union{PyPtr,PyObject})
_finalized[] || ccall(@pysym(:Py_DecRef), Cvoid, (PyPtr,), o)
return o
end

function pydecref(o::PyObject)
pydecref_(o)
pydecref_unsafe_(o)
setfield!(o, :o, PyPtr_NULL)
return o
end
Expand Down
4 changes: 2 additions & 2 deletions src/pyfncall.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function _pycall!(ret::PyObject, o::Union{PyObject,PyPtr}, args, nargs::Int=leng
end
return __pycall!(ret, pyargsptr, o, kw) #::PyObject
finally
pydecref_(pyargsptr)
pydecref_unsafe_(pyargsptr)
end
end

Expand All @@ -42,7 +42,7 @@ function __pycall!(ret::PyObject, pyargsptr::PyPtr, o::Union{PyObject,PyPtr},
disable_sigint() do
retptr = @pycheckn ccall((@pysym :PyObject_Call), PyPtr, (PyPtr,PyPtr,PyPtr), o,
pyargsptr, kw)
pydecref_(ret)
pydecref_unsafe_(ret)
setfield!(ret, :o, retptr)
end
return ret #::PyObject
Expand Down
2 changes: 1 addition & 1 deletion src/pyinit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ end
#########################################################################

const _finalized = Ref(false)
# This flag is set via `Py_AtExit` to avoid calling `pydecref_` after
# This flag is set via `Py_AtExit` to avoid calling `pydecref_unsafe_` after
# Python is finalized.

function _set_finalized()
Expand Down

0 comments on commit d7181d5

Please sign in to comment.