Skip to content

Commit a4e9138

Browse files
authored
Merge pull request #1188 from JuliaLang/hooks
Allow registering hooks at any time
2 parents 8287856 + aea228e commit a4e9138

File tree

6 files changed

+33
-28
lines changed

6 files changed

+33
-28
lines changed

docs/src/_changelog.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ CurrentModule = IJulia
77
This documents notable changes in IJulia.jl. The format is based on [Keep a
88
Changelog](https://keepachangelog.com).
99

10-
## Unreleased
10+
## [v1.30.6] - 2025-10-06
11+
12+
### Fixed
13+
- It's now possible to register hooks at any time, even if an IJulia kernel is
14+
not running ([#1188]). This was accidentally broken in v1.30.
1115

1216
### Changed
1317
- Implemented lazy loading for Conda.jl ([#1187]), which shaves off about 60% of

docs/src/library/internals.md

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,6 @@
77
IJulia.init
88
```
99

10-
11-
## Cell execution hooks
12-
13-
```@docs
14-
IJulia.pop_posterror_hook
15-
IJulia.pop_postexecute_hook
16-
IJulia.pop_preexecute_hook
17-
IJulia.push_posterror_hook
18-
IJulia.push_postexecute_hook
19-
IJulia.push_preexecute_hook
20-
```
21-
22-
2310
## Messaging
2411

2512
```@docs

docs/src/library/public.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,14 @@ IJulia.readprompt
4747
IJulia.set_max_stdio
4848
IJulia.reset_stdio_count
4949
```
50+
51+
## Cell execution hooks
52+
53+
```@docs
54+
IJulia.push_preexecute_hook
55+
IJulia.pop_preexecute_hook
56+
IJulia.push_postexecute_hook
57+
IJulia.pop_postexecute_hook
58+
IJulia.push_posterror_hook
59+
IJulia.pop_posterror_hook
60+
```

src/IJulia.jl

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,6 @@ REPL.REPLDisplay(repl::MiniREPL) = repl.display
117117
# from the front-end.
118118
comms::Dict{String, Comm} = Dict{String, Comm}()
119119

120-
postexecute_hooks::Vector{Function} = Function[]
121-
preexecute_hooks::Vector{Function} = Function[]
122-
posterror_hooks::Vector{Function} = Function[]
123120
shutdown::Function = exit
124121

125122
# the following constants need to be initialized in init().
@@ -405,7 +402,10 @@ history
405402
# Similar to the ipython kernel, we provide a mechanism by
406403
# which modules can register thunk functions to be called after
407404
# executing an input cell, e.g. to "close" the current plot in Pylab.
408-
# Modules should only use these if isdefined(Main, IJulia) is true.
405+
406+
const _preexecute_hooks = Function[]
407+
const _postexecute_hooks = Function[]
408+
const _posterror_hooks = Function[]
409409

410410
function _pop_hook!(f, hooks)
411411
hook_idx = findlast(isequal(f), hooks)
@@ -422,29 +422,31 @@ end
422422
Push a function `f()` onto the end of a list of functions to
423423
execute after executing any notebook cell.
424424
"""
425-
push_postexecute_hook(f::Function; kernel=_default_kernel) = push!(kernel.postexecute_hooks, f)
425+
push_postexecute_hook(f::Function) = push!(IJulia._postexecute_hooks, f)
426+
426427
"""
427428
pop_postexecute_hook(f::Function)
428429
429430
Remove a function `f()` from the list of functions to
430431
execute after executing any notebook cell.
431432
"""
432-
pop_postexecute_hook(f::Function; kernel=_default_kernel) = _pop_hook!(f, kernel.postexecute_hooks)
433+
pop_postexecute_hook(f::Function) = _pop_hook!(f, IJulia._postexecute_hooks)
433434

434435
"""
435436
push_preexecute_hook(f::Function)
436437
437438
Push a function `f()` onto the end of a list of functions to
438439
execute before executing any notebook cell.
439440
"""
440-
push_preexecute_hook(f::Function; kernel=_default_kernel) = push!(kernel.preexecute_hooks, f)
441+
push_preexecute_hook(f::Function) = push!(IJulia._preexecute_hooks, f)
442+
441443
"""
442444
pop_preexecute_hook(f::Function)
443445
444446
Remove a function `f()` from the list of functions to
445447
execute before executing any notebook cell.
446448
"""
447-
pop_preexecute_hook(f::Function; kernel=_default_kernel) = _pop_hook!(f, kernel.preexecute_hooks)
449+
pop_preexecute_hook(f::Function) = _pop_hook!(f, IJulia._preexecute_hooks)
448450

449451
# similar, but called after an error (e.g. to reset plotting state)
450452
"""
@@ -453,14 +455,15 @@ pop_preexecute_hook(f::Function; kernel=_default_kernel) = _pop_hook!(f, kernel.
453455
Remove a function `f()` from the list of functions to
454456
execute after an error occurs when a notebook cell is evaluated.
455457
"""
456-
push_posterror_hook(f::Function; kernel=_default_kernel) = push!(kernel.posterror_hooks, f)
458+
push_posterror_hook(f::Function) = push!(IJulia._posterror_hooks, f)
459+
457460
"""
458461
pop_posterror_hook(f::Function)
459462
460463
Remove a function `f()` from the list of functions to
461464
execute after an error occurs when a notebook cell is evaluated.
462465
"""
463-
pop_posterror_hook(f::Function; kernel=_default_kernel) = _pop_hook!(f, kernel.posterror_hooks)
466+
pop_posterror_hook(f::Function) = _pop_hook!(f, IJulia._posterror_hooks)
464467

465468
#######################################################################
466469

src/execute_request.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ function execute_request(socket, kernel, msg)
7575
hcode = replace(code, r"^\s*\?" => "")
7676

7777
try
78-
foreach(invokelatest, kernel.preexecute_hooks)
78+
foreach(invokelatest, IJulia._preexecute_hooks)
7979

8080
kernel.ans = result = if hcode != code # help request
8181
Core.eval(Main, helpmode(hcode))
@@ -114,7 +114,7 @@ function execute_request(socket, kernel, msg)
114114
end
115115
end
116116

117-
foreach(invokelatest, kernel.postexecute_hooks)
117+
foreach(invokelatest, IJulia._postexecute_hooks)
118118

119119
# flush pending stdio
120120
flush_all()
@@ -151,7 +151,7 @@ function execute_request(socket, kernel, msg)
151151
try
152152
# flush pending stdio
153153
flush_all()
154-
foreach(invokelatest, kernel.posterror_hooks)
154+
foreach(invokelatest, IJulia._posterror_hooks)
155155
catch
156156
end
157157
empty!(kernel.displayqueue) # discard pending display requests on an error

test/kernel.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ end
407407
shutdown(client; wait=false)
408408
end
409409

410-
@test timedwait(() -> process_exited(kernel_proc), 30) == :ok
410+
@test timedwait(() -> process_exited(kernel_proc), 60) == :ok
411411
finally
412412
kill(kernel_proc)
413413
end

0 commit comments

Comments
 (0)