-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
Copy pathinteractiveutil.jl
754 lines (665 loc) · 26.5 KB
/
interactiveutil.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
# This file is a part of Julia. License is MIT: https://julialang.org/license
# editing files
"""
editor()
Determine the editor to use when running functions like `edit`. Return an `Array` compatible
for use within backticks. You can change the editor by setting `JULIA_EDITOR`, `VISUAL` or
`EDITOR` as an environment variable.
"""
function editor()
if Sys.iswindows() || Sys.isapple()
default_editor = "open"
elseif isfile("/etc/alternatives/editor")
default_editor = "/etc/alternatives/editor"
else
default_editor = "emacs"
end
# Note: the editor path can include spaces (if escaped) and flags.
args = shell_split(get(ENV,"JULIA_EDITOR", get(ENV,"VISUAL", get(ENV,"EDITOR", default_editor))))
isempty(args) && error("editor is empty")
return args
end
"""
edit(path::AbstractString, line::Integer=0)
Edit a file or directory optionally providing a line number to edit the file at.
Return to the `julia` prompt when you quit the editor. The editor can be changed
by setting `JULIA_EDITOR`, `VISUAL` or `EDITOR` as an environment variable.
"""
function edit(path::AbstractString, line::Integer=0)
command = editor()
name = basename(first(command))
if endswith(path, ".jl")
f = find_source_file(path)
f !== nothing && (path = f)
end
background = true
line_unsupported = false
if startswith(name, "vim.") || name == "vi" || name == "vim" || name == "nvim" ||
name == "mvim" || name == "nano" ||
name == "emacs" && contains(in, command, ["-nw", "--no-window-system" ]) ||
name == "emacsclient" && contains(in, command, ["-nw", "-t", "-tty"])
cmd = line != 0 ? `$command +$line $path` : `$command $path`
background = false
elseif startswith(name, "emacs") || name == "gedit" || startswith(name, "gvim")
cmd = line != 0 ? `$command +$line $path` : `$command $path`
elseif name == "textmate" || name == "mate" || name == "kate"
cmd = line != 0 ? `$command $path -l $line` : `$command $path`
elseif startswith(name, "subl") || startswith(name, "atom")
cmd = line != 0 ? `$command $path:$line` : `$command $path`
elseif name == "code" || (Sys.iswindows() && Unicode.uppercase(name) == "CODE.EXE")
cmd = line != 0 ? `$command -g $path:$line` : `$command -g $path`
elseif startswith(name, "notepad++")
cmd = line != 0 ? `$command $path -n$line` : `$command $path`
elseif Sys.isapple() && name == "open"
cmd = `open -t $path`
line_unsupported = true
else
cmd = `$command $path`
background = false
line_unsupported = true
end
if Sys.iswindows() && name == "open"
@static Sys.iswindows() && # don't emit this ccall on other platforms
systemerror(:edit, ccall((:ShellExecuteW, "shell32"), stdcall, Int,
(Ptr{Void}, Cwstring, Cwstring, Ptr{Void}, Ptr{Void}, Cint),
C_NULL, "open", path, C_NULL, C_NULL, 10) ≤ 32)
elseif background
spawn(pipeline(cmd, stderr=STDERR))
else
run(cmd)
end
line != 0 && line_unsupported && println("Unknown editor: no line number information passed.\nThe method is defined at line $line.")
nothing
end
"""
edit(function, [types])
Edit the definition of a function, optionally specifying a tuple of types to
indicate which method to edit. The editor can be changed by setting `JULIA_EDITOR`,
`VISUAL` or `EDITOR` as an environment variable.
"""
edit(f) = edit(functionloc(f)...)
edit(f, @nospecialize t) = edit(functionloc(f,t)...)
edit(file, line::Integer) = error("could not find source file for function")
# terminal pager
if Sys.iswindows()
function less(file::AbstractString, line::Integer)
pager = shell_split(get(ENV, "PAGER", "more"))
g = pager[1] == "more" ? "" : "g"
run(Cmd(`$pager +$(line)$(g) \"$file\"`, windows_verbatim = true))
end
else
function less(file::AbstractString, line::Integer)
pager = shell_split(get(ENV, "PAGER", "less"))
run(`$pager +$(line)g $file`)
end
end
"""
less(file::AbstractString, [line::Integer])
Show a file using the default pager, optionally providing a starting line number. Returns to
the `julia` prompt when you quit the pager.
"""
less(file::AbstractString) = less(file, 1)
"""
less(function, [types])
Show the definition of a function using the default pager, optionally specifying a tuple of
types to indicate which method to see.
"""
less(f) = less(functionloc(f)...)
less(f, @nospecialize t) = less(functionloc(f,t)...)
less(file, line::Integer) = error("could not find source file for function")
# clipboard copy and paste
if Sys.isapple()
function clipboard(x)
open(pipeline(`pbcopy`, stderr=STDERR), "w") do io
print(io, x)
end
end
clipboard() = read(`pbpaste`, String)
elseif Sys.islinux()
_clipboardcmd = nothing
function clipboardcmd()
global _clipboardcmd
_clipboardcmd !== nothing && return _clipboardcmd
for cmd in (:xclip, :xsel)
success(pipeline(`which $cmd`, DevNull)) && return _clipboardcmd = cmd
end
error("no clipboard command found, please install xsel or xclip")
end
function clipboard(x)
c = clipboardcmd()
cmd = c == :xsel ? `xsel --nodetach --input --clipboard` :
c == :xclip ? `xclip -silent -in -selection clipboard` :
error("unexpected clipboard command: $c")
open(pipeline(cmd, stderr=STDERR), "w") do io
print(io, x)
end
end
function clipboard()
c = clipboardcmd()
cmd = c == :xsel ? `xsel --nodetach --output --clipboard` :
c == :xclip ? `xclip -quiet -out -selection clipboard` :
error("unexpected clipboard command: $c")
read(pipeline(cmd, stderr=STDERR), String)
end
elseif Sys.iswindows()
# TODO: these functions leak memory and memory locks if they throw an error
function clipboard(x::AbstractString)
if containsnul(x)
throw(ArgumentError("Windows clipboard strings cannot contain NUL character"))
end
systemerror(:OpenClipboard, 0==ccall((:OpenClipboard, "user32"), stdcall, Cint, (Ptr{Void},), C_NULL))
systemerror(:EmptyClipboard, 0==ccall((:EmptyClipboard, "user32"), stdcall, Cint, ()))
x_u16 = cwstring(x)
# copy data to locked, allocated space
p = ccall((:GlobalAlloc, "kernel32"), stdcall, Ptr{UInt16}, (UInt16, Int32), 2, sizeof(x_u16))
systemerror(:GlobalAlloc, p==C_NULL)
plock = ccall((:GlobalLock, "kernel32"), stdcall, Ptr{UInt16}, (Ptr{UInt16},), p)
systemerror(:GlobalLock, plock==C_NULL)
ccall(:memcpy, Ptr{UInt16}, (Ptr{UInt16},Ptr{UInt16},Int), plock, x_u16, sizeof(x_u16))
systemerror(:GlobalUnlock, 0==ccall((:GlobalUnlock, "kernel32"), stdcall, Cint, (Ptr{Void},), plock))
pdata = ccall((:SetClipboardData, "user32"), stdcall, Ptr{UInt16}, (UInt32, Ptr{UInt16}), 13, p)
systemerror(:SetClipboardData, pdata!=p)
ccall((:CloseClipboard, "user32"), stdcall, Void, ())
end
clipboard(x) = clipboard(sprint(print, x)::String)
function clipboard()
systemerror(:OpenClipboard, 0==ccall((:OpenClipboard, "user32"), stdcall, Cint, (Ptr{Void},), C_NULL))
pdata = ccall((:GetClipboardData, "user32"), stdcall, Ptr{UInt16}, (UInt32,), 13)
systemerror(:SetClipboardData, pdata==C_NULL)
systemerror(:CloseClipboard, 0==ccall((:CloseClipboard, "user32"), stdcall, Cint, ()))
plock = ccall((:GlobalLock, "kernel32"), stdcall, Ptr{UInt16}, (Ptr{UInt16},), pdata)
systemerror(:GlobalLock, plock==C_NULL)
# find NUL terminator (0x0000 16-bit code unit)
len = 0
while unsafe_load(plock, len+1) != 0; len += 1; end
# get Vector{UInt16}, transcode data to UTF-8, make a String of it
s = transcode(String, unsafe_wrap(Array, plock, len))
systemerror(:GlobalUnlock, 0==ccall((:GlobalUnlock, "kernel32"), stdcall, Cint, (Ptr{UInt16},), plock))
return s
end
else
clipboard(x="") = error("`clipboard` function not implemented for $(Sys.KERNEL)")
end
"""
clipboard(x)
Send a printed form of `x` to the operating system clipboard ("copy").
"""
clipboard(x)
"""
clipboard() -> AbstractString
Return a string with the contents of the operating system clipboard ("paste").
"""
clipboard()
# system information
# used by sysinfo.jl
function _show_cpuinfo(io::IO, info::Sys.CPUinfo, header::Bool=true, prefix::AbstractString=" ")
tck = Sys.SC_CLK_TCK
if header
println(io, info.model, ": ")
print(io, " "^length(prefix))
if tck > 0
@printf(io, " %5s %9s %9s %9s %9s %9s\n",
"speed", "user", "nice", "sys", "idle", "irq")
else
@printf(io, " %5s %9s %9s %9s %9s %9s ticks\n",
"speed", "user", "nice", "sys", "idle", "irq")
end
end
print(io, prefix)
if tck > 0
@printf(io, "%5d MHz %9d s %9d s %9d s %9d s %9d s",
info.speed, info.cpu_times!user / tck, info.cpu_times!nice / tck,
info.cpu_times!sys / tck, info.cpu_times!idle / tck, info.cpu_times!irq / tck)
else
@printf(io, "%5d MHz %9d %9d %9d %9d %9d ticks",
info.speed, info.cpu_times!user, info.cpu_times!nice,
info.cpu_times!sys, info.cpu_times!idle, info.cpu_times!irq)
end
end
"""
versioninfo(io::IO=STDOUT; verbose::Bool=false, packages::Bool=false)
Print information about the version of Julia in use. The output is
controlled with boolean keyword arguments:
- `packages`: print information about installed packages
- `verbose`: print all additional information
"""
function versioninfo(io::IO=STDOUT; verbose::Bool=false, packages::Bool=false)
println(io, "Julia Version $VERSION")
if !isempty(GIT_VERSION_INFO.commit_short)
println(io, "Commit $(GIT_VERSION_INFO.commit_short) ($(GIT_VERSION_INFO.date_string))")
end
if ccall(:jl_is_debugbuild, Cint, ())!=0
println(io, "DEBUG build")
end
println(io, "Platform Info:")
println(io, " OS: ", Sys.iswindows() ? "Windows" : Sys.isapple() ?
"macOS" : Sys.KERNEL, " (", Sys.MACHINE, ")")
if verbose
lsb = ""
if Sys.islinux()
try lsb = readchomp(pipeline(`lsb_release -ds`, stderr=DevNull)) end
end
if Sys.iswindows()
try lsb = strip(read(`$(ENV["COMSPEC"]) /c ver`, String)) end
end
if !isempty(lsb)
println(io, " ", lsb)
end
if Sys.isunix()
println(io, " uname: ", readchomp(`uname -mprsv`))
end
end
if verbose
cpuio = IOBuffer() # print cpu_summary with correct alignment
Sys.cpu_summary(cpuio)
for (i, line) in enumerate(split(String(take!(cpuio)), "\n"))
prefix = i == 1 ? " CPU: " : " "
println(io, prefix, line)
end
else
cpu = Sys.cpu_info()
println(io, " CPU: ", cpu[1].model)
end
if verbose
println(io, " Memory: $(Sys.total_memory()/2^30) GB ($(Sys.free_memory()/2^20) MB free)")
try println(io, " Uptime: $(Sys.uptime()) sec") end
print(io, " Load Avg: ")
print_matrix(io, Sys.loadavg()')
println(io)
end
println(io, " WORD_SIZE: ", Sys.WORD_SIZE)
if Base.libblas_name == "libopenblas" || BLAS.vendor() == :openblas || BLAS.vendor() == :openblas64
openblas_config = BLAS.openblas_get_config()
println(io, " BLAS: libopenblas (", openblas_config, ")")
else
println(io, " BLAS: ",libblas_name)
end
println(io, " LAPACK: ",liblapack_name)
println(io, " LIBM: ",libm_name)
println(io, " LLVM: libLLVM-",libllvm_version," (", Sys.JIT, ", ", Sys.cpu_name, ")")
println(io, "Environment:")
for (k,v) in ENV
if ismatch(r"JULIA", String(k))
println(io, " $(k) = $(v)")
end
end
if verbose
for (k,v) in ENV
if ismatch(r"PATH|FLAG|^TERM$|HOME", String(k))
println(io, " $(k) = $(v)")
end
end
end
if packages || verbose
println(io, "Packages:")
println(io, " Package Directory: ", Pkg.dir())
print(io, " Package Status:")
if isdir(Pkg.dir())
println(io, "")
Pkg.status(io)
else
println(io, " no packages installed")
end
end
end
# displaying type-ambiguity warnings
"""
code_warntype([io::IO], f, types)
Prints lowered and type-inferred ASTs for the methods matching the given generic function
and type signature to `io` which defaults to `STDOUT`. The ASTs are annotated in such a way
as to cause "non-leaf" types to be emphasized (if color is available, displayed in red).
This serves as a warning of potential type instability. Not all non-leaf types are particularly
problematic for performance, so the results need to be used judiciously.
See [`@code_warntype`](@ref man-code-warntype) for more information.
"""
function code_warntype(io::IO, f, @nospecialize(t))
function slots_used(ci, slotnames)
used = falses(length(slotnames))
scan_exprs!(used, ci.code)
return used
end
function scan_exprs!(used, exprs)
for ex in exprs
if isa(ex, Slot)
used[ex.id] = true
elseif isa(ex, Expr)
scan_exprs!(used, ex.args)
end
end
end
emph_io = IOContext(io, :TYPEEMPHASIZE => true)
for (src, rettype) in code_typed(f, t)
println(emph_io, "Variables:")
slotnames = sourceinfo_slotnames(src)
used_slotids = slots_used(src, slotnames)
for i = 1:length(slotnames)
if used_slotids[i]
print(emph_io, " ", slotnames[i])
if isa(src.slottypes, Array)
show_expr_type(emph_io, src.slottypes[i], true)
end
print(emph_io, '\n')
elseif !('#' in slotnames[i] || '@' in slotnames[i])
print(emph_io, " ", slotnames[i], "<optimized out>\n")
end
end
print(emph_io, "\nBody:\n ")
body = Expr(:body)
body.args = src.code
body.typ = rettype
# Fix slot names and types in function body
show_unquoted(IOContext(emph_io, :SOURCEINFO => src, :SOURCE_SLOTNAMES => slotnames),
body, 2)
print(emph_io, '\n')
end
nothing
end
code_warntype(f, @nospecialize(t)) = code_warntype(STDOUT, f, t)
typesof(args...) = Tuple{Any[ Core.Typeof(a) for a in args ]...}
function gen_call_with_extracted_types(__module__, fcn, ex0)
if isa(ex0, Expr)
if any(a->(Meta.isexpr(a, :kw) || Meta.isexpr(a, :parameters)), ex0.args)
# remove keyword args, but call the kwfunc
args = filter(a->!(Meta.isexpr(a, :kw) || Meta.isexpr(a, :parameters)), ex0.args)
return quote
local arg1 = $(esc(args[1]))
$(fcn)(Core.kwfunc(arg1),
Tuple{NamedTuple, Core.Typeof(arg1),
$(typesof)($(map(esc, args[2:end])...)).parameters...})
end
elseif ex0.head == :call
return Expr(:call, fcn, esc(ex0.args[1]),
Expr(:call, typesof, map(esc, ex0.args[2:end])...))
end
end
if isa(ex0, Expr) && ex0.head == :macrocall # Make @edit @time 1+2 edit the macro by using the types of the *expressions*
return Expr(:call, fcn, esc(ex0.args[1]), Tuple{#=__source__=#LineNumberNode, #=__module__=#Module, Any[ Core.Typeof(a) for a in ex0.args[3:end] ]...})
end
ex = Meta.lower(__module__, ex0)
exret = Expr(:none)
if !isa(ex, Expr)
exret = Expr(:call, :error, "expression is not a function call or symbol")
elseif ex.head == :call
if any(e->(isa(e, Expr) && e.head==:(...)), ex0.args) &&
(ex.args[1] === GlobalRef(Core,:_apply) ||
ex.args[1] === GlobalRef(Base,:_apply))
# check for splatting
exret = Expr(:call, ex.args[1], fcn,
Expr(:tuple, esc(ex.args[2]),
Expr(:call, typesof, map(esc, ex.args[3:end])...)))
else
exret = Expr(:call, fcn, esc(ex.args[1]),
Expr(:call, typesof, map(esc, ex.args[2:end])...))
end
elseif ex.head == :body
a1 = ex.args[1]
if isa(a1, Expr) && a1.head == :call
a11 = a1.args[1]
if a11 == :setindex!
exret = Expr(:call, fcn, a11,
Expr(:call, typesof, map(esc, a1.args[2:end])...))
end
end
end
if ex.head == :thunk || exret.head == :none
exret = Expr(:call, :error, "expression is not a function call, "
* "or is too complex for @$fcn to analyze; "
* "break it down to simpler parts if possible")
end
return exret
end
for fname in [:which, :less, :edit, :functionloc, :code_warntype,
:code_llvm, :code_llvm_raw, :code_native]
@eval begin
macro ($fname)(ex0)
gen_call_with_extracted_types(__module__, $(Expr(:quote, fname)), ex0)
end
end
end
macro which(ex0::Symbol)
ex0 = QuoteNode(ex0)
return :(which_module($__module__, $ex0))
end
for fname in [:code_typed, :code_lowered]
@eval begin
macro ($fname)(ex0)
thecall = gen_call_with_extracted_types(__module__, $(Expr(:quote, fname)), ex0)
quote
results = $thecall
length(results) == 1 ? results[1] : results
end
end
end
end
"""
@which
Applied to a function or macro call, it evaluates the arguments to the specified call, and
returns the `Method` object for the method that would be called for those arguments. Applied
to a variable, it returns the module in which the variable was bound. It calls out to the
`which` function.
"""
:@which
"""
@less
Evaluates the arguments to the function or macro call, determines their types, and calls the `less`
function on the resulting expression.
"""
:@less
"""
@edit
Evaluates the arguments to the function or macro call, determines their types, and calls the `edit`
function on the resulting expression.
"""
:@edit
"""
@functionloc
Applied to a function or macro call, it evaluates the arguments to the specified call, and
returns a tuple `(filename,line)` giving the location for the method that would be called for those arguments.
It calls out to the `functionloc` function.
"""
:@functionloc
"""
@code_typed
Evaluates the arguments to the function or macro call, determines their types, and calls
[`code_typed`](@ref) on the resulting expression.
"""
:@code_typed
"""
@code_warntype
Evaluates the arguments to the function or macro call, determines their types, and calls
[`code_warntype`](@ref) on the resulting expression.
"""
:@code_warntype
"""
@code_lowered
Evaluates the arguments to the function or macro call, determines their types, and calls
[`code_lowered`](@ref) on the resulting expression.
"""
:@code_lowered
"""
@code_llvm
Evaluates the arguments to the function or macro call, determines their types, and calls
[`code_llvm`](@ref) on the resulting expression.
"""
:@code_llvm
"""
@code_native
Evaluates the arguments to the function or macro call, determines their types, and calls
[`code_native`](@ref) on the resulting expression.
"""
:@code_native
function type_close_enough(@nospecialize(x), @nospecialize(t))
x == t && return true
return (isa(x,DataType) && isa(t,DataType) && x.name === t.name &&
!_isleaftype(t) && x <: t) ||
(isa(x,Union) && isa(t,DataType) && (type_close_enough(x.a, t) || type_close_enough(x.b, t)))
end
# `methodswith` -- shows a list of methods using the type given
"""
methodswith(typ[, module or function][, showparents::Bool=false])
Return an array of methods with an argument of type `typ`.
The optional second argument restricts the search to a particular module or function
(the default is all top-level modules).
If optional `showparents` is `true`, also return arguments with a parent type of `typ`,
excluding type `Any`.
"""
function methodswith(t::Type, f::Function, showparents::Bool=false, meths = Method[])
for d in methods(f)
if any(function (x)
let x = rewrap_unionall(x, d.sig)
(type_close_enough(x, t) ||
(showparents ? (t <: x && (!isa(x,TypeVar) || x.ub != Any)) :
(isa(x,TypeVar) && x.ub != Any && t == x.ub)) &&
x != Any)
end
end,
unwrap_unionall(d.sig).parameters)
push!(meths, d)
end
end
return meths
end
function _methodswith(t::Type, m::Module, showparents::Bool)
meths = Method[]
for nm in names(m)
if isdefined(m, nm)
f = getfield(m, nm)
if isa(f, Function)
methodswith(t, f, showparents, meths)
end
end
end
return unique(meths)
end
methodswith(t::Type, m::Module, showparents::Bool=false) = _methodswith(t, m, showparents)
function methodswith(t::Type, showparents::Bool=false)
meths = Method[]
for mod in loaded_modules_array()
append!(meths, _methodswith(t, mod, showparents))
end
return unique(meths)
end
# file downloading
downloadcmd = nothing
if Sys.iswindows()
function download(url::AbstractString, filename::AbstractString)
res = ccall((:URLDownloadToFileW,:urlmon),stdcall,Cuint,
(Ptr{Void},Cwstring,Cwstring,Cuint,Ptr{Void}),C_NULL,url,filename,0,C_NULL)
if res != 0
error("automatic download failed (error: $res): $url")
end
filename
end
else
function download(url::AbstractString, filename::AbstractString)
global downloadcmd
if downloadcmd === nothing
for checkcmd in (:curl, :wget, :fetch)
if success(pipeline(`which $checkcmd`, DevNull))
downloadcmd = checkcmd
break
end
end
end
if downloadcmd == :wget
try
run(`wget -O $filename $url`)
catch
rm(filename) # wget always creates a file
rethrow()
end
elseif downloadcmd == :curl
run(`curl -g -L -f -o $filename $url`)
elseif downloadcmd == :fetch
run(`fetch -f $filename $url`)
else
error("no download agent available; install curl, wget, or fetch")
end
filename
end
end
function download(url::AbstractString)
filename = tempname()
download(url, filename)
end
"""
download(url::AbstractString, [localfile::AbstractString])
Download a file from the given url, optionally renaming it to the given local file name.
Note that this function relies on the availability of external tools such as `curl`, `wget`
or `fetch` to download the file and is provided for convenience. For production use or
situations in which more options are needed, please use a package that provides the desired
functionality instead.
"""
download(url, filename)
# workspace management
"""
workspace()
Replace the top-level module (`Main`) with a new one, providing a clean workspace. The
previous `Main` module is made available as `LastMain`.
If `Package` was previously loaded, `using Package` in the new `Main` will re-use the
loaded copy. Run `reload("Package")` first to load a fresh copy.
This function should only be used interactively.
"""
function workspace()
last = Core.Main # ensure to reference the current Main module
b = Base # this module
ccall(:jl_new_main_module, Any, ()) # make Core.Main a new baremodule
m = Core.Main # now grab a handle to the new Main module
ccall(:jl_add_standard_imports, Void, (Any,), m)
eval(m, Expr(:toplevel,
:(const Base = $b),
:(const LastMain = $last),
:(using Base.MainInclude)))
empty!(package_locks)
return m
end
# testing
"""
Base.runtests(tests=["all"], numcores=ceil(Int, Sys.CPU_CORES / 2);
exit_on_error=false, [seed])
Run the Julia unit tests listed in `tests`, which can be either a string or an array of
strings, using `numcores` processors. If `exit_on_error` is `false`, when one test
fails, all remaining tests in other files will still be run; they are otherwise discarded,
when `exit_on_error == true`.
If a seed is provided via the keyword argument, it is used to seed the
global RNG in the context where the tests are run; otherwise the seed is chosen randomly.
"""
function runtests(tests = ["all"], numcores = ceil(Int, Sys.CPU_CORES / 2);
exit_on_error=false,
seed::Union{BitInteger,Void}=nothing)
if isa(tests,AbstractString)
tests = split(tests)
end
exit_on_error && push!(tests, "--exit-on-error")
seed != nothing && push!(tests, "--seed=0x$(hex(seed % UInt128))") # cast to UInt128 to avoid a minus sign
ENV2 = copy(ENV)
ENV2["JULIA_CPU_CORES"] = "$numcores"
try
run(setenv(`$(julia_cmd()) $(joinpath(JULIA_HOME,
Base.DATAROOTDIR, "julia", "test", "runtests.jl")) $tests`, ENV2))
catch
buf = PipeBuffer()
versioninfo(buf)
error("A test has failed. Please submit a bug report (https://github.com/JuliaLang/julia/issues)\n" *
"including error messages above and the output of versioninfo():\n$(read(buf, String))")
end
end
# testing
"""
varinfo(m::Module=Main, pattern::Regex=r"")
Return a markdown table giving information about exported global variables in a module, optionally restricted
to those matching `pattern`.
The memory consumption estimate is an approximate lower bound on the size of the internal structure of the object.
"""
function varinfo(m::Module=Main, pattern::Regex=r"")
rows =
Any[ let value = getfield(m, v)
Any[string(v),
(value ∈ (Base, Main, Core) ? "" : format_bytes(summarysize(value))),
summary(value)]
end
for v in sort!(names(m)) if isdefined(m, v) && ismatch(pattern, string(v)) ]
unshift!(rows, Any["name", "size", "summary"])
return Markdown.MD(Any[Markdown.Table(rows, Symbol[:l, :r, :l])])
end
varinfo(pat::Regex) = varinfo(Main, pat)