Skip to content

Commit

Permalink
Allow nothin to be printed
Browse files Browse the repository at this point in the history
fix missing bracket

fix up whitespace

Keep defining custom show for nothing

add printing of nothing

Let print of nothing fallback to show
  • Loading branch information
oxinabox committed Aug 3, 2019
1 parent 2e649b0 commit c6c9c1a
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Standard library changes
* Cmd interpolation (`` `$(x::Cmd) a b c` `` where) now propagates `x`'s process flags
(environment, flags, working directory, etc) if `x` is the first interpolant and errors
otherwise ([#24353]).
* `nothing` can now be `print`ed, and interplated into strings etc. as the string `"nothing"`. It is still not permitted to be interplated into Cmds (i.e. ``echo `$(nothing)` `` will still error without running anything.) ([#32148])

#### LinearAlgebra

Expand Down
2 changes: 1 addition & 1 deletion base/process.jl
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,7 @@ end

cmd_interpolate(xs...) = cstr(string(map(cmd_interpolate1, xs)...))
cmd_interpolate1(x) = x
cmd_interpolate1(::Nothing) = throw(ArgumentError("`nothing` can not be interpolated into commands (`Cmd`)")
cmd_interpolate1(::Nothing) = throw(ArgumentError("`nothing` can not be interpolated into commands (`Cmd`)"))

arg_gen() = String[]
arg_gen(x::AbstractString) = String[cstr(x)]
Expand Down
1 change: 0 additions & 1 deletion base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,6 @@ function show(io::IO, tn::Core.TypeName)
end

show(io::IO, ::Nothing) = print(io, "nothing")
print(io::IO, ::Nothing) = throw(ArgumentError("`nothing` should not be printed; use `show`, `repr`, or custom output instead."))
show(io::IO, b::Bool) = print(io, get(io, :typeinfo, Any) === Bool ? (b ? "1" : "0") : (b ? "true" : "false"))
show(io::IO, n::Signed) = (write(io, string(n)); nothing)
show(io::IO, n::Unsigned) = print(io, "0x", string(n, pad = sizeof(n)<<1, base = 16))
Expand Down
22 changes: 13 additions & 9 deletions test/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1459,15 +1459,19 @@ let src = code_typed(gcd, (Int, Int), debuginfo=:source)[1][1]
@test pop!(lines) == " ! ── unreachable::#UNDEF"
end

# issue #27352
@test_throws ArgumentError print(nothing)
@test_throws ArgumentError print(stdout, nothing)
@test_throws ArgumentError string(nothing)
@test_throws ArgumentError string(1, "", nothing)
@test_throws ArgumentError let x = nothing; "x = $x" end
@test let x = nothing; "x = $(repr(x))" end == "x = nothing"
@test_throws ArgumentError `/bin/foo $nothing`
@test_throws ArgumentError `$nothing`
@testset "printing and interpolating nothing" begin
@test sprint(print, nothing) == "nothing"
@test string(nothing) == "nothing"
@test repr(nothing) == "nothing"
@test string(1, "", nothing) == "1nothing"
@test let x = nothing; "x = $x" end == "x = nothing"
@test let x = nothing; "x = $(repr(x))" end == "x = nothing"

# issue #27352 : No interpolating nothing into commands
@test_throws ArgumentError `/bin/foo $nothing`
@test_throws ArgumentError `$nothing`
@test_throws ArgumentError let x = nothing; `/bin/foo $x` end
end

struct X28004
value::Any
Expand Down

0 comments on commit c6c9c1a

Please sign in to comment.