Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add options for varinfo() to show all and imported, sort by each column, search modules recursively #38042

Merged
merged 5 commits into from
Oct 29, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions stdlib/InteractiveUtils/src/InteractiveUtils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,36 @@ include("macros.jl")
include("clipboard.jl")

"""
varinfo(m::Module=Main, pattern::Regex=r"")
varinfo(m::Module=Main, pattern::Regex=r""; all::Bool = false, imported::Bool = false, sort_size::Bool = false)

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.

- `all` : also list non-exported objects defined in the module, deprecated objects, and compiler-generated objects.
- `imported` : also list objects explicitly imported from other modules.
- `sort_size` : sort results by their size, in descending order
"""
function varinfo(m::Module=Main, pattern::Regex=r"")
function varinfo(m::Module=Main, pattern::Regex=r""; all::Bool = false, imported::Bool = false, sort_size::Bool = false)
rows =
Any[ let value = getfield(m, v)
Any[string(v),
(value===Base || value===Main || value===Core ? "" : format_bytes(summarysize(value))),
summary(value)]
summary(value),
summarysize(value)]
end
for v in sort!(names(m)) if isdefined(m, v) && occursin(pattern, string(v)) ]

for v in sort!(names(m, all = all, imported = imported)) if isdefined(m, v) && occursin(pattern, string(v)) ]
if sort_size
sizes = map(r->r[4], rows)
p = sortperm(sizes, rev=true)
rows = rows[p]
IanButterworth marked this conversation as resolved.
Show resolved Hide resolved
end
pushfirst!(rows, Any["name", "size", "summary"])

return Markdown.MD(Any[Markdown.Table(rows, Symbol[:l, :r, :l])])
return Markdown.MD(Any[Markdown.Table(map(r->r[1:3], rows), Symbol[:l, :r, :l])])
end
varinfo(pat::Regex) = varinfo(Main, pat)
varinfo(pat::Regex; kwargs...) = varinfo(Main, pat, kwargs...)

"""
versioninfo(io::IO=stdout; verbose::Bool=false)
Expand Down
26 changes: 23 additions & 3 deletions stdlib/InteractiveUtils/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,35 @@ mktemp() do f, io
end

module _test_varinfo_
export x
x = 1.0
import Test: @test
export exported
exported = 1.0
not_exp = 1.0
z_larger = Vector{Float64}(undef, 3)
a_smaller = Vector{Float64}(undef, 2)
end
@test repr(varinfo(Main, r"^$")) == """
| name | size | summary |
|:---- | ----:|:------- |
"""
let v = repr(varinfo(_test_varinfo_))
@test occursin("| x | 8 bytes | Float64 |", v)
@test occursin("| exported | 8 bytes | Float64 |", v)
@test !occursin("not_exp", v)
@test !occursin("@test", v)
end
let v = repr(varinfo(_test_varinfo_, all = true))
@test occursin("exported", v)
@test occursin("not_exp", v)
@test !occursin("@test", v)
@test findfirst("a_smaller", v)[1] < findfirst("z_larger", v)[1] # check for alphabetical
end
let v = repr(varinfo(_test_varinfo_, imported = true))
@test occursin("exported", v)
@test !occursin("not_exp", v)
@test occursin("@test", v)
end
let v = repr(varinfo(_test_varinfo_, all=true, sort_size = true))
@test findfirst("z_larger", v)[1] < findfirst("a_smaller", v)[1] # check for size order
end

# Issue 14173
Expand Down