Skip to content

Commit 9ef8924

Browse files
fertapricJosé Valim
authored andcommitted
Handle non-printable args in StringIO gracefully (#8600)
Non-printable arguments were killing the StringIO process, which was killing the Logger handler associated to it. That was causing some errors when users were running their test suites with `capture_log: true`, because the proxy (Logger handler) was not there when ExUnit.CaptureLog was trying to remove it.
1 parent 4935188 commit 9ef8924

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

lib/elixir/lib/string_io.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,8 @@ defmodule StringIO do
279279
{_, _, _} ->
280280
{{:error, req}, state}
281281
end
282+
rescue
283+
ArgumentError -> {{:error, req}, state}
282284
end
283285

284286
## get_chars

lib/elixir/test/elixir/string_io_test.exs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,14 @@ defmodule StringIOTest do
137137
assert StringIO.contents(pid) == {"", "あいう"}
138138
end
139139

140+
test "IO.write with non-printable arguments" do
141+
{:ok, pid} = StringIO.open("")
142+
143+
assert_raise ArgumentError, fn ->
144+
IO.write(pid, [<<1::1>>])
145+
end
146+
end
147+
140148
test "IO.binwrite" do
141149
{:ok, pid} = StringIO.open("")
142150
assert IO.binwrite(pid, "foo") == :ok
@@ -160,6 +168,14 @@ defmodule StringIOTest do
160168
assert StringIO.contents(pid) == {"", "abc\n"}
161169
end
162170

171+
test "IO.puts with non-printable arguments" do
172+
{:ok, pid} = StringIO.open("")
173+
174+
assert_raise ArgumentError, fn ->
175+
IO.puts(pid, [<<1::1>>])
176+
end
177+
end
178+
163179
test "IO.inspect" do
164180
{:ok, pid} = StringIO.open("")
165181
assert IO.inspect(pid, {}, []) == {}

0 commit comments

Comments
 (0)