Skip to content

Commit 8d05e50

Browse files
committed
Various bug fixes/inference improvements
Found with JET.jl.
1 parent 143e99e commit 8d05e50

File tree

8 files changed

+48
-25
lines changed

8 files changed

+48
-25
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
@@ -243,6 +243,10 @@ This consists of log messages printed to the terminal window where
243243
or received by the kernel. Used for debugging IJulia.
244244
"""
245245
function set_verbose(v::Bool=true, kernel=_default_kernel)
246+
if isnothing(kernel)
247+
error("Kernel has not been initialized, cannot set its verbosity.")
248+
end
249+
246250
kernel.verbose = v
247251
end
248252

@@ -254,7 +258,13 @@ whether you are in an IJulia notebook, therefore, you can check
254258
"""
255259
inited::Bool = false
256260

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

259269
_shutting_down::Threads.Atomic{Bool} = Threads.Atomic{Bool}(false)
260270

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

399+
function _pop_hook!(f, hooks)
400+
hook_idx = findlast(isequal(f), hooks)
401+
if isnothing(hook_idx)
402+
error("Could not find hook: $(f)")
403+
else
404+
splice!(hooks, hook_idx)
405+
end
406+
end
407+
389408
"""
390409
push_postexecute_hook(f::Function)
391410
@@ -399,8 +418,7 @@ push_postexecute_hook(f::Function; kernel=_default_kernel) = push!(kernel.postex
399418
Remove a function `f()` from the list of functions to
400419
execute after executing any notebook cell.
401420
"""
402-
pop_postexecute_hook(f::Function; kernel=_default_kernel) =
403-
splice!(kernel.postexecute_hooks, findlast(isequal(f), kernel.postexecute_hooks))
421+
pop_postexecute_hook(f::Function; kernel=_default_kernel) = _pop_hook!(f, kernel.postexecute_hooks)
404422

405423
"""
406424
push_preexecute_hook(f::Function)
@@ -415,8 +433,7 @@ push_preexecute_hook(f::Function; kernel=_default_kernel) = push!(kernel.preexec
415433
Remove a function `f()` from the list of functions to
416434
execute before executing any notebook cell.
417435
"""
418-
pop_preexecute_hook(f::Function; kernel=_default_kernel) =
419-
splice!(kernel.preexecute_hooks, findlast(isequal(f), kernel.preexecute_hooks))
436+
pop_preexecute_hook(f::Function; kernel=_default_kernel) = _pop_hook!(f, kernel.preexecute_hooks)
420437

421438
# similar, but called after an error (e.g. to reset plotting state)
422439
"""
@@ -432,8 +449,7 @@ push_posterror_hook(f::Function; kernel=_default_kernel) = push!(kernel.posterro
432449
Remove a function `f()` from the list of functions to
433450
execute after an error occurs when a notebook cell is evaluated.
434451
"""
435-
pop_posterror_hook(f::Function; kernel=_default_kernel) =
436-
splice!(kernel.posterror_hooks, findlast(isequal(f), kernel.posterror_hooks))
452+
pop_posterror_hook(f::Function; kernel=_default_kernel) = _pop_hook!(f, kernel.posterror_hooks)
437453

438454
#######################################################################
439455

src/comm_manager.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function Comm(target,
1919
kernel=IJulia._default_kernel,
2020
data=Dict(),
2121
metadata=Dict())
22-
comm = Comm{Symbol(target)}(id, primary, on_msg, on_close; kernel)
22+
comm = Comm{Symbol(target)}(id, primary, on_msg, on_close, kernel)
2323
if primary
2424
# Request a secondary object be created at the front end
2525
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

0 commit comments

Comments
 (0)