Skip to content

Commit a288779

Browse files
oxinaboxStefanKarpinski
authored andcommitted
Allow nothing to be printed (#32148)
1 parent 53beb34 commit a288779

File tree

4 files changed

+21
-13
lines changed

4 files changed

+21
-13
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Standard library changes
5151
* `empty` now accepts a `NamedTuple` ([#32534]).
5252
* `mod` now accepts a unit range as the second argument to easily perform offset modular arithmetic to ensure the result is inside the range ([#32628]).
5353
* `Sockets.recvfrom` now returns both host and port as an InetAddr ([#32729]).
54+
* `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])
5455

5556
#### Libdl
5657

base/cmd.jl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,10 @@ pipeline(a, b, c, d...) = pipeline(pipeline(a, b), c, d...)
314314

315315
## implementation of `cmd` syntax ##
316316

317+
cmd_interpolate(xs...) = cstr(string(map(cmd_interpolate1, xs)...))
318+
cmd_interpolate1(x) = x
319+
cmd_interpolate1(::Nothing) = throw(ArgumentError("`nothing` can not be interpolated into commands (`Cmd`)"))
320+
317321
arg_gen() = String[]
318322
arg_gen(x::AbstractString) = String[cstr(x)]
319323
function arg_gen(cmd::Cmd)
@@ -327,11 +331,11 @@ function arg_gen(head)
327331
if isiterable(typeof(head))
328332
vals = String[]
329333
for x in head
330-
push!(vals, cstr(string(x)))
334+
push!(vals, cmd_interpolate(x))
331335
end
332336
return vals
333337
else
334-
return String[cstr(string(head))]
338+
return String[cmd_interpolate(head)]
335339
end
336340
end
337341

@@ -340,7 +344,7 @@ function arg_gen(head, tail...)
340344
tail = arg_gen(tail...)
341345
vals = String[]
342346
for h = head, t = tail
343-
push!(vals, cstr(string(h,t)))
347+
push!(vals, cmd_interpolate(h,t))
344348
end
345349
return vals
346350
end

base/show.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,6 @@ function show(io::IO, tn::Core.TypeName)
590590
end
591591

592592
show(io::IO, ::Nothing) = print(io, "nothing")
593-
print(io::IO, ::Nothing) = throw(ArgumentError("`nothing` should not be printed; use `show`, `repr`, or custom output instead."))
594593
show(io::IO, b::Bool) = print(io, get(io, :typeinfo, Any) === Bool ? (b ? "1" : "0") : (b ? "true" : "false"))
595594
show(io::IO, n::Signed) = (write(io, string(n)); nothing)
596595
show(io::IO, n::Unsigned) = print(io, "0x", string(n, pad = sizeof(n)<<1, base = 16))

test/show.jl

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,15 +1467,19 @@ let src = code_typed(gcd, (Int, Int), debuginfo=:source)[1][1]
14671467
@test pop!(lines) == " ! ── unreachable::#UNDEF"
14681468
end
14691469

1470-
# issue #27352
1471-
@test_throws ArgumentError print(nothing)
1472-
@test_throws ArgumentError print(stdout, nothing)
1473-
@test_throws ArgumentError string(nothing)
1474-
@test_throws ArgumentError string(1, "", nothing)
1475-
@test_throws ArgumentError let x = nothing; "x = $x" end
1476-
@test let x = nothing; "x = $(repr(x))" end == "x = nothing"
1477-
@test_throws ArgumentError `/bin/foo $nothing`
1478-
@test_throws ArgumentError `$nothing`
1470+
@testset "printing and interpolating nothing" begin
1471+
@test sprint(print, nothing) == "nothing"
1472+
@test string(nothing) == "nothing"
1473+
@test repr(nothing) == "nothing"
1474+
@test string(1, "", nothing) == "1nothing"
1475+
@test let x = nothing; "x = $x" end == "x = nothing"
1476+
@test let x = nothing; "x = $(repr(x))" end == "x = nothing"
1477+
1478+
# issue #27352 : No interpolating nothing into commands
1479+
@test_throws ArgumentError `/bin/foo $nothing`
1480+
@test_throws ArgumentError `$nothing`
1481+
@test_throws ArgumentError let x = nothing; `/bin/foo $x` end
1482+
end
14791483

14801484
struct X28004
14811485
value::Any

0 commit comments

Comments
 (0)