Skip to content

Commit

Permalink
Merge pull request #18890 from JuliaLang/jb/names
Browse files Browse the repository at this point in the history
improve behavior of `names`. fixes #18768
  • Loading branch information
JeffBezanson authored Oct 13, 2016
2 parents 0134b9c + fdea7fa commit 919a3f7
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
10 changes: 8 additions & 2 deletions base/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,14 @@ end
"""
names(x::Module, all::Bool=false, imported::Bool=false)
Get an array of the names exported by a `Module`, with optionally more `Module` globals
according to the additional parameters.
Get an array of the names exported by a `Module`, excluding deprecated names.
If `all` is true, then the list also includes non-exported names defined in the module,
deprecated names, and compiler-generated names.
If `imported` is true, then names explicitly imported from other modules
are also included.
As a special case, all names defined in `Main` are considered \"exported\",
since it is not idiomatic to explicitly export names from `Main`.
"""
names(m::Module, all::Bool=false, imported::Bool=false) = sort!(ccall(:jl_module_names, Array{Symbol,1}, (Any,Cint,Cint), m, all, imported))

Expand Down
4 changes: 3 additions & 1 deletion doc/stdlib/base.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1456,7 +1456,9 @@ Reflection

.. Docstring generated from Julia source
Get an array of the names exported by a ``Module``\ , with optionally more ``Module`` globals according to the additional parameters.
Get an array of the names exported by a ``Module``\ , excluding deprecated names. If ``all`` is true, then the list also includes non-exported names defined in the module, deprecated names, and compiler-generated names. If ``imported`` is true, then names explicitly imported from other modules are also included.

As a special case, all names defined in ``Main`` are considered "exported", since it is not idiomatic to explicitly export names from ``Main``\ .

.. function:: nfields(x::DataType) -> Int

Expand Down
4 changes: 3 additions & 1 deletion src/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,9 @@ JL_DLLEXPORT jl_value_t *jl_module_names(jl_module_t *m, int all, int imported)
if (table[i] != HT_NOTFOUND) {
jl_binding_t *b = (jl_binding_t*)table[i];
int hidden = jl_symbol_name(b->name)[0]=='#';
if ((b->exportp || ((imported || b->owner == m) && (all || m == jl_main_module))) &&
if ((b->exportp ||
(imported && b->imported) ||
((b->owner == m) && (all || m == jl_main_module))) &&
(all || (!b->deprecated && !hidden))) {
jl_array_grow_end(a, 1);
//XXX: change to jl_arrayset if array storage allocation for Array{Symbols,1} changes:
Expand Down
4 changes: 4 additions & 0 deletions test/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ not_const = 1

module TestMod7648
using Base.Test
import Base.convert
export a9475, foo9475, c7648, foo7648, foo7648_nomethods, Foo7648

const c7648 = 8
Expand Down Expand Up @@ -210,6 +211,9 @@ let
@test Set(names(TestMod7648, true)) == Set([:TestMod7648, :TestModSub9475, :a9475, :foo9475, :c7648, :d7648, :f7648,
:foo7648, Symbol("#foo7648"), :foo7648_nomethods, Symbol("#foo7648_nomethods"),
:Foo7648, :eval, Symbol("#eval")])
@test Set(names(TestMod7648, true, true)) == Set([:TestMod7648, :TestModSub9475, :a9475, :foo9475, :c7648, :d7648, :f7648,
:foo7648, Symbol("#foo7648"), :foo7648_nomethods, Symbol("#foo7648_nomethods"),
:Foo7648, :eval, Symbol("#eval"), :convert])
@test isconst(TestMod7648, :c7648)
@test !isconst(TestMod7648, :d7648)
end
Expand Down

0 comments on commit 919a3f7

Please sign in to comment.