Skip to content

Commit b583c94

Browse files
authored
default to soft global scope (JuliaLang#720)
1 parent 4b1286a commit b583c94

File tree

5 files changed

+16
-2
lines changed

5 files changed

+16
-2
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,14 @@ IJulia.set_max_stdio(1 << 20)
235235
The module that code in an input cell is evaluated in can be set using `Main.IJulia.set_current_module(::Module)`.
236236
It defaults to `Main`.
237237

238+
### Opting out of soft scope
239+
240+
By default, IJulia evaluates user code using "soft" global scope, via the [SoftGlobalScope.jl package](https://github.com/stevengj/SoftGlobalScope.jl): this means that you don't need explicit `global` declarations to modify global variables in `for` loops and similar, which is convenient for interactive use.
241+
242+
To opt out of this behavior, making notebooks behave similarly to global code in Julia `.jl` files,
243+
you can set `IJulia.SOFTSCOPE[] = false` at runtime, or include the environment variable `IJULIA_SOFTSCOPE=no`
244+
environment of the IJulia kernel when it is launched.
245+
238246
## Low-level Information
239247

240248
### Using older IPython versions

REQUIRE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ JSON 0.17
44
ZMQ 0.6.0
55
Compat 0.69.0
66
Conda 0.1.5
7+
SoftGlobalScope

src/IJulia.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ The `IJulia` module is used in three ways:
3535
module IJulia
3636
export notebook, installkernel
3737

38-
using ZMQ, JSON, Compat
38+
using ZMQ, JSON, Compat, SoftGlobalScope
3939
import Compat.invokelatest
4040
using Compat.Unicode: uppercase, lowercase
4141
import Compat.Dates

src/execute_request.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ showerror_nobt(io, e, bt) = showerror(io, e, bt, backtrace=false)
9898
end
9999

100100
# return the content of a pyerr message for exception e
101-
function error_content(e, bt=catch_backtrace(); backtrace_top::Symbol=:include_string, msg::AbstractString="")
101+
function error_content(e, bt=catch_backtrace();
102+
backtrace_top::Symbol=SOFTSCOPE[] ? :softscope_include_string : :include_string,
103+
msg::AbstractString="")
102104
tb = map(x->String(x), split(sprint(show_bt,
103105
backtrace_top,
104106
bt, 1:typemax(Int)),
@@ -189,6 +191,7 @@ function execute_request(socket, msg)
189191
else
190192
#run the code!
191193
occursin(magics_regex, code) ? magics_help(code) :
194+
SOFTSCOPE[] ? softscope_include_string(current_module[], code, "In[$n]") :
192195
include_string(current_module[], code, "In[$n]")
193196
end
194197

src/init.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@ end
2424
const orig_stdin = Ref{IO}()
2525
const orig_stdout = Ref{IO}()
2626
const orig_stderr = Ref{IO}()
27+
const SOFTSCOPE = Ref{Bool}()
2728
function __init__()
2829
seed!(IJulia_RNG)
2930
orig_stdin[] = stdin
3031
orig_stdout[] = stdout
3132
orig_stderr[] = stderr
33+
SOFTSCOPE[] = lowercase(get(ENV, "IJULIA_SOFTSCOPE", "yes")) in ("yes", "true")
3234
end
3335

3436
# the following constants need to be initialized in init().

0 commit comments

Comments
 (0)