Skip to content

Commit cf9c186

Browse files
author
Christopher Rowley
committed
load python symbols globally when embedded rather than using lib_ptr (which is now NULL in this case)
1 parent 6aeeba0 commit cf9c186

3 files changed

Lines changed: 22 additions & 12 deletions

File tree

pysrc/juliacall/__init__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,8 @@ def jlstr(x):
269269
return 'raw"' + x + '"'
270270
script = '''
271271
try
272-
Base.require(Main, :CompilerSupportLibraries_jll)
273-
global __PythonCall_libptr = Ptr{{Cvoid}}(UInt({}))
272+
import CompilerSupportLibraries_jll as _
273+
global __PythonCall_embedded__ = nothing
274274
ENV["JULIA_PYTHONCALL_EXE"] = {}
275275
using PythonCall
276276
catch err
@@ -280,7 +280,6 @@ def jlstr(x):
280280
rethrow()
281281
end
282282
'''.format(
283-
hex(c.pythonapi._handle),
284283
jlstr(sys.executable or ''),
285284
)
286285
res = jl_eval(script.encode('utf8'))

src/C/context.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,12 @@ on_main_thread
105105

106106
function init_context()
107107

108-
CTX.is_embedded = hasproperty(Base.Main, :__PythonCall_libptr)
108+
CTX.is_embedded = hasproperty(Base.Main, :__PythonCall_embedded__)
109109

110110
if CTX.is_embedded
111111
# In this case, getting a handle to libpython is easy
112-
CTX.lib_ptr = Base.Main.__PythonCall_libptr::Ptr{Cvoid}
113-
init_pointers()
112+
CTX.lib_ptr = C_NULL
113+
init_pointers(true)
114114
# Check Python is initialized
115115
Py_IsInitialized() == 0 && error("Python is not already initialized.")
116116
CTX.is_initialized = true
@@ -236,7 +236,7 @@ function init_context()
236236
end
237237

238238
# Get function pointers from the library
239-
init_pointers()
239+
init_pointers(false)
240240

241241
# Initialize the interpreter
242242
CTX.is_preinitialized = Py_IsInitialized() != 0

src/C/pointers.jl

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -282,19 +282,30 @@ end
282282

283283
const POINTERS = CAPIPointers()
284284

285-
@eval init_pointers(p::CAPIPointers = POINTERS, lib::Ptr = CTX.lib_ptr) = begin
285+
function _dlsym(lib::Ptr{Cvoid}, sym::Symbol)
286+
if lib == C_NULL
287+
# embedded case, python symbols available globally
288+
cglobal(sym)::Ptr{Cvoid}
289+
else
290+
dlsym(lib, sym)::Ptr{Cvoid}
291+
end
292+
end
293+
294+
@eval function init_pointers(embedded::Bool, p::CAPIPointers = POINTERS, lib::Ptr = CTX.lib_ptr)
295+
@assert (lib == C_NULL) == embedded
286296
$([
287-
:(p.$name = dlsym(lib, $(QuoteNode(name))))
297+
:(p.$name = _dlsym(lib, $(QuoteNode(name))))
288298
for name in CAPI_FUNCS
289299
]...)
290300
$(
291301
[
292302
:(p.$name =
293-
Base.unsafe_load(Ptr{PyPtr}(dlsym(lib, $(QuoteNode(name)))::Ptr))) for name in CAPI_EXCEPTIONS
303+
Base.unsafe_load(Ptr{PyPtr}(_dlsym(lib, $(QuoteNode(name)))::Ptr))) for name in CAPI_EXCEPTIONS
294304
]...
295305
)
296-
$([:(p.$name = dlsym(lib, $(QuoteNode(name)))) for name in CAPI_OBJECTS]...)
297-
p.PyOS_InputHookPtr = dlsym(CTX.lib_ptr, :PyOS_InputHook)
306+
$([:(p.$name = _dlsym(lib, $(QuoteNode(name)))) for name in CAPI_OBJECTS]...)
307+
p.PyOS_InputHookPtr = _dlsym(CTX.lib_ptr, :PyOS_InputHook)
308+
nothing
298309
end
299310

300311
for (name, (argtypes, rettype)) in CAPI_FUNC_SIGS

0 commit comments

Comments
 (0)