From fdea7fa75f7973404fc0064f6cc9833cfab0db42 Mon Sep 17 00:00:00 2001 From: Jeff Bezanson Date: Wed, 12 Oct 2016 13:30:08 -0400 Subject: [PATCH] improve behavior of `names`. fixes #18768 - never include names only visible via `using` - make `imported` orthogonal to the other flags - improve docs --- base/reflection.jl | 10 ++++++++-- doc/stdlib/base.rst | 4 +++- src/module.c | 4 +++- test/reflection.jl | 4 ++++ 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/base/reflection.jl b/base/reflection.jl index d4b3792f0ea7a..2304e058e01da 100644 --- a/base/reflection.jl +++ b/base/reflection.jl @@ -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)) diff --git a/doc/stdlib/base.rst b/doc/stdlib/base.rst index 435c662775994..8cd13c7a3082c 100644 --- a/doc/stdlib/base.rst +++ b/doc/stdlib/base.rst @@ -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 diff --git a/src/module.c b/src/module.c index cd16426c6c137..651ba648c36b8 100644 --- a/src/module.c +++ b/src/module.c @@ -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: diff --git a/test/reflection.jl b/test/reflection.jl index d1a1825dcf18e..74713b4cbc7d9 100644 --- a/test/reflection.jl +++ b/test/reflection.jl @@ -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 @@ -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