Skip to content

Commit 6284581

Browse files
author
Christopher Doris
committed
adds input hook for julia event loop
1 parent 8616711 commit 6284581

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed

pysrc/juliacall/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ def convert(T, x):
2121
_convert = PythonCall.seval("pyjlcallback((T,x)->pyjl(pyconvert(pyjlvalue(T)::Type,x)))")
2222
return _convert(T, x)
2323

24+
def interactive(enable=True):
25+
"Allow the Julia event loop to run in the background of the Python REPL."
26+
if enable:
27+
PythonCall._set_python_input_hook()
28+
else:
29+
PythonCall._unset_python_input_hook()
30+
2431
class JuliaError(Exception):
2532
"An error arising in Julia code."
2633
def __init__(self, exception, backtrace=None):

src/compat/gui.jl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,30 @@ function event_loop_on(g::Symbol; interval::Real = 0.04, fix::Bool = false)
165165
callback = new_event_loop_callback(string(g), Float64(interval))
166166
EVENT_LOOPS[g] = Timer(t -> callback(), 0; interval = interval)
167167
end
168+
169+
function _python_input_hook()
170+
try
171+
while true
172+
yield()
173+
@static if Sys.iswindows()
174+
if ccall(:_kbhit, Cint, ()) != 0
175+
break
176+
end
177+
end
178+
sleep(0.001)
179+
end
180+
catch
181+
return Cint(1)
182+
end
183+
return Cint(0)
184+
end
185+
186+
function _set_python_input_hook()
187+
C.PyOS_SetInputHook(@cfunction(_python_input_hook, Cint, ()))
188+
return
189+
end
190+
191+
function _unset_python_input_hook()
192+
C.PyOS_SetInputHook(C_NULL)
193+
return
194+
end

src/cpython/extras.jl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,17 @@ PyBuffer_Release(_b) = begin
4141
return
4242
end
4343

44+
function PyOS_SetInputHook(hook::Ptr{Cvoid})
45+
Base.unsafe_store!(POINTERS.PyOS_InputHookPtr, hook)
46+
return
47+
end
48+
49+
function PyOS_GetInputHook()
50+
return Base.unsafe_load(POINTERS.PyOS_InputHookPtr)
51+
end
52+
4453
function PyOS_RunInputHook()
45-
hook = Base.unsafe_load(Ptr{Ptr{Cvoid}}(dlsym(CTX.lib_ptr, :PyOS_InputHook)))
54+
hook = PyOS_GetInputHook()
4655
if hook == C_NULL
4756
return false
4857
else

src/cpython/pointers.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ const CAPI_OBJECTS = Set([
273273
$([:($name :: Ptr{Cvoid} = C_NULL) for name in CAPI_FUNCS]...)
274274
$([:($name :: PyPtr = C_NULL) for name in CAPI_EXCEPTIONS]...)
275275
$([:($name :: PyPtr = C_NULL) for name in CAPI_OBJECTS]...)
276+
PyOS_InputHookPtr :: Ptr{Ptr{Cvoid}} = C_NULL
276277
PyJuliaBase_Type :: PyPtr = C_NULL
277278
PyExc_JuliaError :: PyPtr = C_NULL
278279
end
@@ -290,6 +291,7 @@ const POINTERS = CAPIPointers()
290291
]...)
291292
$([:(p.$name = Base.unsafe_load(Ptr{PyPtr}(dlsym(lib, $(QuoteNode(name)))))) for name in CAPI_EXCEPTIONS]...)
292293
$([:(p.$name = dlsym(lib, $(QuoteNode(name)))) for name in CAPI_OBJECTS]...)
294+
p.PyOS_InputHookPtr = dlsym(CTX.lib_ptr, :PyOS_InputHook)
293295
end
294296

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

0 commit comments

Comments
 (0)