Skip to content

Commit

Permalink
repr: allow context pairs when calling show
Browse files Browse the repository at this point in the history
use this instead of the special-case `Base.reprfull` in loading
  • Loading branch information
StefanKarpinski committed Jan 21, 2018
1 parent 808e828 commit c418a10
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
8 changes: 1 addition & 7 deletions base/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1047,12 +1047,6 @@ function evalfile(path::AbstractString, args::Vector{String}=String[])
end
evalfile(path::AbstractString, args::Vector) = evalfile(path, String[args...])

function reprfull(x)
s = IOBuffer()
show(IOContext(s, :module => nothing), x)
String(take!(s))
end

function create_expr_cache(input::String, output::String, concrete_deps::typeof(_concrete_dependencies), uuid::Union{Nothing,UUID})
rm(output, force=true) # Remove file if it exists
code_object = """
Expand All @@ -1072,7 +1066,7 @@ function create_expr_cache(input::String, output::String, concrete_deps::typeof(
write(in, """
begin
empty!(Base.LOAD_PATH)
append!(Base.LOAD_PATH, $(reprfull(LOAD_PATH)))
append!(Base.LOAD_PATH, $(repr(LOAD_PATH, :module => nothing)))
empty!(Base.DEPOT_PATH)
append!(Base.DEPOT_PATH, $(repr(DEPOT_PATH)))
empty!(Base.LOAD_CACHE_PATH)
Expand Down
9 changes: 9 additions & 0 deletions base/strings/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,11 @@ end

"""
repr(x)
repr(x, context::Pair{Symbol,<:Any}...)
Create a string from any value using the [`show`](@ref) function.
If context pairs are given, the IO buffer used to capture `show` output
is wrapped in an `IOContext` object with those context pairs.
# Examples
```jldoctest
Expand All @@ -170,6 +173,12 @@ function repr(x)
String(take!(s))
end

function repr(x, context::Pair{Symbol}...)
s = IOBuffer()
show(IOContext(s, context...), x)
String(take!(s))
end

# IOBuffer views of a (byte)string:

"""
Expand Down

0 comments on commit c418a10

Please sign in to comment.