Skip to content

Commit

Permalink
Merge pull request #230 from stevengj/revert-219-use-global-python-sy…
Browse files Browse the repository at this point in the history
…mbols

Revert "Avoid loading libpython.so if Python symbols are already loaded globally"
  • Loading branch information
stevengj committed Feb 5, 2016
2 parents 2720a09 + 849003f commit fcbe0be
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 55 deletions.
47 changes: 3 additions & 44 deletions src/PyCall.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,55 +32,14 @@ const depfile = joinpath(dirname(@__FILE__), "..", "deps", "deps.jl")
isfile(depfile) || error("PyCall not properly installed. Please run Pkg.build(\"PyCall\")")
include(depfile) # generated by Pkg.build("PyCall")

hassym(lib, sym) = Libdl.dlsym_e(lib, sym) != C_NULL

function get_globaldl()
if !isdefined(:globaldl)
global const globaldl = unsafe_load(cglobal((@windows ? :jl_exe_handle : :jl_dl_handle), Ptr{Void}))
end
return globaldl
end


function get_libpythondl()
if !isdefined(:libpythondl)
global const libpythondl = Libdl.dlopen(libpython, Libdl.RTLD_GLOBAL)
end
return libpythondl
end


macro pysym(func)
z, zlocal = gensym(string(func)), gensym()
eval(current_module(),:(global $z = C_NULL))
z = esc(z)

quote
let $zlocal::Ptr{Void} = $z::Ptr{Void}
if $zlocal == C_NULL
if hassym(get_globaldl(), $(esc(func)))
$zlocal = Libdl.dlsym(globaldl::Ptr{Void}, $(esc(func)))
else
$zlocal = Libdl.dlsym(get_libpythondl()::Ptr{Void}, $(esc(func)))
end
global $z = $zlocal
end
$zlocal
end
end
:(($func, libpython))
end

macro pyglobal(name)
quote
if hassym(get_globaldl(), $(esc(name)))
Libdl.dlsym(globaldl::Ptr{Void}, $(esc(name)))
else
Libdl.dlsym(get_libpythondl()::Ptr{Void}, $(esc(name)))
end
end
:(cglobal(($name, libpython)))
end
macro pyglobalobj(name)
:(convert(PyPtr, @pyglobal $(esc(name))))
:(cglobal(($name, libpython), PyObject_struct))
end

#########################################################################
Expand Down
6 changes: 1 addition & 5 deletions src/exception.jl
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,7 @@ const pyexc = Dict{DataType, PyPtr}()
type PyIOError <: Exception end

macro pyglobalobjptr(name)
quote
sym = @pyglobal $name
ptr = convert(Ptr{PyPtr}, sym)
unsafe_load( ptr )
end
:(unsafe_load(cglobal(($name, libpython), Ptr{PyObject_struct})))
end

function pyexc_initialize()
Expand Down
9 changes: 3 additions & 6 deletions src/pyinit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,12 @@ const MethodWrapperType = PyNULL()
const ufuncType = PyNULL()
const format_traceback = PyNULL()


#########################################################################

function __init__()
# Check to see if the Python symbols are already defined in the global namespace of the current
# process before we try to load them from a dynamic library.
if !hassym(get_globaldl(), :Py_GetVersion)
Libdl.dlopen(libpython, Libdl.RTLD_LAZY|Libdl.RTLD_DEEPBIND|Libdl.RTLD_GLOBAL)
end
# issue #189
Libdl.dlopen(libpython, Libdl.RTLD_LAZY|Libdl.RTLD_DEEPBIND|Libdl.RTLD_GLOBAL)

already_inited = 0 != ccall((@pysym :Py_IsInitialized), Cint, ())

if !already_inited
Expand Down

0 comments on commit fcbe0be

Please sign in to comment.