Skip to content

Commit 4cf8ff7

Browse files
committed
Various bug fixes/inference improvements
Found with JET.jl.
1 parent 65997f7 commit 4cf8ff7

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
@@ -240,6 +240,10 @@ This consists of log messages printed to the terminal window where
240240
or received by the kernel. Used for debugging IJulia.
241241
"""
242242
function set_verbose(v::Bool=true, kernel=_default_kernel)
243+
if isnothing(kernel)
244+
error("Kernel has not been initialized, cannot set its verbosity.")
245+
end
246+
243247
kernel.verbose = v
244248
end
245249

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

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

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

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

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

402420
"""
403421
push_preexecute_hook(f::Function)
@@ -412,8 +430,7 @@ push_preexecute_hook(f::Function; kernel=_default_kernel) = push!(kernel.preexec
412430
Remove a function `f()` from the list of functions to
413431
execute before executing any notebook cell.
414432
"""
415-
pop_preexecute_hook(f::Function; kernel=_default_kernel) =
416-
splice!(kernel.preexecute_hooks, findlast(isequal(f), kernel.preexecute_hooks))
433+
pop_preexecute_hook(f::Function; kernel=_default_kernel) = _pop_hook!(f, kernel.preexecute_hooks)
417434

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

435451
#######################################################################
436452

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)