Skip to content

Commit 89aa4cd

Browse files
committed
Various bug fixes/inference improvements
Found with JET.jl.
1 parent 7dbae36 commit 89aa4cd

File tree

9 files changed

+49
-26
lines changed

9 files changed

+49
-26
lines changed

deps/kspec.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import JSON
33
#######################################################################
44
# Install Jupyter kernel-spec files.
55

6-
copy_config(src, dest) = cp(src, joinpath(dest, basename(src)), force=true)
6+
copy_config(src::AbstractString, dest::AbstractString) = cp(src, joinpath(dest, basename(src)), force=true)
77

88
# return the user kernelspec directory, according to
99
# https://jupyter-client.readthedocs.io/en/latest/kernels.html#kernelspecs

src/IJulia.jl

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ mutable struct Comm{target}
7878
primary::Bool
7979
on_msg::Function
8080
on_close::Function
81-
function (::Type{Comm{target}})(id, primary, on_msg, on_close; kernel=_default_kernel) where {target}
81+
function (::Type{Comm{target}})(id, primary, on_msg, on_close, kernel) where {target}
8282
comm = new{target}(id, primary, on_msg, on_close)
8383
kernel.comms[id] = comm
8484
return comm
@@ -241,6 +241,10 @@ This consists of log messages printed to the terminal window where
241241
or received by the kernel. Used for debugging IJulia.
242242
"""
243243
function set_verbose(v::Bool=true, kernel=_default_kernel)
244+
if isnothing(kernel)
245+
error("Kernel has not been initialized, cannot set its verbosity.")
246+
end
247+
244248
kernel.verbose = v
245249
end
246250

@@ -252,7 +256,13 @@ whether you are in an IJulia notebook, therefore, you can check
252256
"""
253257
inited::Bool = false
254258

255-
set_current_module(m::Module; kernel=_default_kernel) = kernel.current_module = m
259+
function set_current_module(m::Module; kernel=_default_kernel)
260+
if isnothing(kernel)
261+
error("Kernel has not been initialized, cannot set the current module.")
262+
end
263+
264+
kernel.current_module = m
265+
end
256266

257267
_shutting_down::Threads.Atomic{Bool} = Threads.Atomic{Bool}(false)
258268

@@ -384,6 +394,15 @@ history
384394
# executing an input cell, e.g. to "close" the current plot in Pylab.
385395
# Modules should only use these if isdefined(Main, IJulia) is true.
386396

397+
function _pop_hook!(f, hooks)
398+
hook_idx = findlast(isequal(f), hooks)
399+
if isnothing(hook_idx)
400+
error("Could not find hook: $(f)")
401+
else
402+
splice!(hooks, hook_idx)
403+
end
404+
end
405+
387406
"""
388407
push_postexecute_hook(f::Function)
389408
@@ -397,8 +416,7 @@ push_postexecute_hook(f::Function; kernel=_default_kernel) = push!(kernel.postex
397416
Remove a function `f()` from the list of functions to
398417
execute after executing any notebook cell.
399418
"""
400-
pop_postexecute_hook(f::Function; kernel=_default_kernel) =
401-
splice!(kernel.postexecute_hooks, findlast(isequal(f), kernel.postexecute_hooks))
419+
pop_postexecute_hook(f::Function; kernel=_default_kernel) = _pop_hook!(f, kernel.postexecute_hooks)
402420

403421
"""
404422
push_preexecute_hook(f::Function)
@@ -413,8 +431,7 @@ push_preexecute_hook(f::Function; kernel=_default_kernel) = push!(kernel.preexec
413431
Remove a function `f()` from the list of functions to
414432
execute before executing any notebook cell.
415433
"""
416-
pop_preexecute_hook(f::Function; kernel=_default_kernel) =
417-
splice!(kernel.preexecute_hooks, findlast(isequal(f), kernel.preexecute_hooks))
434+
pop_preexecute_hook(f::Function; kernel=_default_kernel) = _pop_hook!(f, kernel.preexecute_hooks)
418435

419436
# similar, but called after an error (e.g. to reset plotting state)
420437
"""
@@ -430,8 +447,7 @@ push_posterror_hook(f::Function; kernel=_default_kernel) = push!(kernel.posterro
430447
Remove a function `f()` from the list of functions to
431448
execute after an error occurs when a notebook cell is evaluated.
432449
"""
433-
pop_posterror_hook(f::Function; kernel=_default_kernel) =
434-
splice!(kernel.posterror_hooks, findlast(isequal(f), kernel.posterror_hooks))
450+
pop_posterror_hook(f::Function; kernel=_default_kernel) = _pop_hook!(f, kernel.posterror_hooks)
435451

436452
#######################################################################
437453

src/comm_manager.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function Comm(target,
1616
kernel=IJulia._default_kernel,
1717
data=Dict(),
1818
metadata=Dict())
19-
comm = Comm{Symbol(target)}(id, primary, on_msg, on_close; kernel)
19+
comm = Comm{Symbol(target)}(id, primary, on_msg, on_close, kernel)
2020
if primary
2121
# Request a secondary object be created at the front end
2222
send_ipython(kernel.publish[], kernel,

src/display.jl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const ijulia_jsonmime_types = Vector{Union{MIME, Vector{MIME}}}([
4747
Register a new MIME type.
4848
"""
4949
register_mime(x::Union{MIME, Vector{MIME}}) = push!(ijulia_mime_types, x)
50-
register_mime(x::AbstractVector{<:MIME}) = push!(ijulia_mime_types, Vector{Mime}(x))
50+
register_mime(x::AbstractVector{<:MIME}) = push!(ijulia_mime_types, Vector{MIME}(x))
5151

5252
"""
5353
register_jsonmime(x::Union{MIME, Vector{MIME}})
@@ -56,7 +56,7 @@ register_mime(x::AbstractVector{<:MIME}) = push!(ijulia_mime_types, Vector{Mime}
5656
Register a new JSON MIME type.
5757
"""
5858
register_jsonmime(x::Union{MIME, Vector{MIME}}) = push!(ijulia_jsonmime_types, x)
59-
register_jsonmime(x::AbstractVector{<:MIME}) = push!(ijulia_jsonmime_types, Vector{Mime}(x))
59+
register_jsonmime(x::AbstractVector{<:MIME}) = push!(ijulia_jsonmime_types, Vector{MIME}(x))
6060

6161
# return a String=>Any dictionary to attach as metadata
6262
# in Jupyter display_data and pyout messages
@@ -178,8 +178,11 @@ function error_content(e, bt=catch_backtrace();
178178
if !isempty(msg)
179179
pushfirst!(tb, msg)
180180
end
181-
Dict("ename" => ename, "evalue" => evalue,
182-
"traceback" => tb)
181+
182+
# Specify the value type as Any because types other than String may be in
183+
# the returned JSON.
184+
Dict{String, Any}("ename" => ename, "evalue" => evalue,
185+
"traceback" => tb)
183186
end
184187

185188
#######################################################################

src/handlers.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ function complete_type(T::DataType)
8787
end
8888

8989
#Get typeMap for Jupyter completions
90-
function complete_types(comps)
90+
function complete_types(comps, kernel=_default_kernel)
9191
typeMap = []
9292
for c in comps
9393
ctype = ""
@@ -102,7 +102,7 @@ function complete_types(comps)
102102
expr = Meta.parse(c, raise=false)
103103
if typeof(expr) == Symbol
104104
try
105-
ctype = complete_type(Core.eval(current_module[], :(typeof($expr))))
105+
ctype = complete_type(Core.eval(kernel.current_module, :(typeof($expr))))
106106
catch
107107
end
108108
elseif !isa(expr, Expr)
@@ -156,7 +156,7 @@ function complete_request(socket, kernel, msg)
156156
cursor_start = ind2chr(msg, code, prevind(code, first(positions)))
157157
cursor_end = ind2chr(msg, code, last(positions))
158158
if should_complete
159-
metadata["_jupyter_types_experimental"] = complete_types(comps)
159+
metadata["_jupyter_types_experimental"] = complete_types(comps, kernel)
160160
else
161161
# should_complete is false for cases where we only want to show
162162
# a list of possible completions but not complete, e.g. foo(\t
@@ -212,10 +212,10 @@ request](https://jupyter-client.readthedocs.io/en/latest/messaging.html#connect)
212212
function connect_request(socket, kernel, msg)
213213
send_ipython(kernel.requests[], kernel,
214214
msg_reply(msg, "connect_reply",
215-
Dict("shell_port" => profile["shell_port"],
216-
"iopub_port" => profile["iopub_port"],
217-
"stdin_port" => profile["stdin_port"],
218-
"hb_port" => profile["hb_port"])))
215+
Dict("shell_port" => kernel.profile["shell_port"],
216+
"iopub_port" => kernel.profile["iopub_port"],
217+
"stdin_port" => kernel.profile["stdin_port"],
218+
"hb_port" => kernel.profile["hb_port"])))
219219
end
220220

221221
"""

src/inline.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ function display(d::InlineDisplay, M::MIME, x)
103103
end
104104
flush_all() # so that previous stream output appears in order
105105
send_ipython(kernel.publish[], kernel,
106-
msg_pub(execute_msg, "display_data",
106+
msg_pub(kernel.execute_msg, "display_data",
107107
Dict("metadata" => metadata(x), # optional
108108
"transient" => transient(x), # optional
109109
"data" => d)))

src/jupyter.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,14 @@ end
151151
Launches [qtconsole](https://qtconsole.readthedocs.io) for the current
152152
kernel. IJulia must be initialized already.
153153
"""
154-
function qtconsole()
154+
function qtconsole(kernel=_default_kernel)
155+
if isnothing(kernel)
156+
error("IJulia has not been started, cannot run qtconsole")
157+
end
158+
155159
qtconsole = find_jupyter_subcommand("qtconsole")
156160
if inited
157-
run(`$qtconsole --existing $connection_file`; wait=false)
161+
run(`$qtconsole --existing $(kernel.connection_file)`; wait=false)
158162
else
159163
error("IJulia is not running. qtconsole must be called from an IJulia session.")
160164
end

src/kernel.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ function run_kernel()
2424
end
2525
end
2626

27-
wait(IJulia._default_kernel)
27+
wait(IJulia._default_kernel::Kernel)
2828
end

src/stdio.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ function watch_stream(rd::IO, name::AbstractString, kernel)
8080
if kernel.stdio_bytes[] >= kernel.max_output_per_request[]
8181
read(rd, nb) # read from libuv/os buffer and discard
8282
if kernel.stdio_bytes[] - nb < kernel.max_output_per_request[]
83-
send_ipython(kernel.publish[], kernel, msg_pub(execute_msg, "stream",
83+
send_ipython(kernel.publish[], kernel, msg_pub(kernel.execute_msg, "stream",
8484
Dict("name" => "stderr", "text" => "Excessive output truncated after $(kernel.stdio_bytes[]) bytes.")))
8585
end
8686
else

0 commit comments

Comments
 (0)