Skip to content

Commit

Permalink
Added weak references support to jlwrap types (for JuliaPy#21 and Jul…
Browse files Browse the repository at this point in the history
  • Loading branch information
amyascwk committed Oct 30, 2015
1 parent 0e0343f commit 72e5017
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/pyinit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ function __init__()
PyMemberDef[ PyMemberDef(pyjlwrap_membername,
T_PYSSIZET, sizeof_PyObject_HEAD, READONLY,
pyjlwrap_doc),
PyMemberDef(weakreflist_membername,
T_OBJECT_EX, sizeof_PyObject_HEAD+sizeof(PyPtr), 0,
weakreflist_doc),
PyMemberDef(C_NULL,0,0,0,C_NULL) ]

init_datetime()
Expand Down
12 changes: 11 additions & 1 deletion src/pytype.jl
Original file line number Diff line number Diff line change
Expand Up @@ -340,12 +340,17 @@ immutable Py_jlWrap
# PyObject_HEAD (for non-Py_TRACE_REFS build):
ob_refcnt::Int
ob_type::PyPtr

jl_value::Any
ob_weakreflist::PyPtr #list of weak references
end

# destructor for jlwrap instance, assuming it was created with pyjlwrap_new
function pyjlwrap_dealloc(o::PyPtr)
#Delete weak reference list
ccall((@pysym :PyObject_ClearWeakRefs), Void,
(PyPtr,), o)

delete!(pycall_gc, o)
return nothing
end
Expand Down Expand Up @@ -379,6 +384,8 @@ end
# constant strings (must not be gc'ed) for pyjlwrap_members
const pyjlwrap_membername = "jl_value"
const pyjlwrap_doc = "Julia jl_value_t* (Any object)"
const weakreflist_membername = "ob_weakreflist"
const weakreflist_doc = "List of weak references"

# called in __init__
function pyjlwrap_init()
Expand All @@ -391,6 +398,8 @@ function pyjlwrap_init()
t.tp_repr = pyjlwrap_repr_ptr
t.tp_hash = sizeof(Py_hash_t) < sizeof(Int) ?
pyjlwrap_hash32_ptr : pyjlwrap_hash_ptr
t.tp_weaklistoffset = fieldoffsets(Py_jlWrap)[4] #set to actual offset
t.tp_flags |= Py_TPFLAGS_HAVE_WEAKREFS #set weak refs bit
end)
end

Expand All @@ -416,6 +425,7 @@ function pyjlwrap_new(pyT::PyTypeObject, value::Any)
pycall_gc[o.o] = value
p = convert(Ptr{Ptr{Void}}, o.o)
unsafe_store!(p, ccall(:jl_value_ptr, Ptr{Void}, (Any,), value), 3)
unsafe_store!(p, C_NULL, 4) #initiate weakreflist as NULL
return o
end

Expand Down

0 comments on commit 72e5017

Please sign in to comment.