Skip to content

Commit 10a795f

Browse files
author
José Valim
committed
Only support iolists on IO
1 parent ba20926 commit 10a795f

File tree

3 files changed

+16
-13
lines changed

3 files changed

+16
-13
lines changed

lib/elixir/lib/io.ex

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ defmodule IO do
2525

2626
import :erlang, only: [group_leader: 0]
2727

28+
defmacrop is_iolist(data) do
29+
quote do
30+
is_list(unquote(data)) or is_binary(unquote(data))
31+
end
32+
end
33+
2834
@doc """
2935
Reads `count` characters from the IO device or until
3036
the end of the line if `:line` is given. It returns:
@@ -92,8 +98,8 @@ defmodule IO do
9298
#=> "error"
9399
94100
"""
95-
def write(device // group_leader(), item) do
96-
:io.put_chars map_dev(device), to_iodata(item)
101+
def write(device // group_leader(), item) when is_iolist(item) do
102+
:io.put_chars map_dev(device), item
97103
end
98104

99105
@doc """
@@ -102,18 +108,18 @@ defmodule IO do
102108
103109
Check `write/2` for more information.
104110
"""
105-
def binwrite(device // group_leader(), item) do
106-
:file.write map_dev(device), to_iodata(item)
111+
def binwrite(device // group_leader(), item) when is_iolist(item) do
112+
:file.write map_dev(device), item
107113
end
108114

109115
@doc """
110116
Writes the argument to the device, similar to `write/2`,
111117
but adds a newline at the end. The argument is expected
112118
to be a chardata.
113119
"""
114-
def puts(device // group_leader(), item) do
120+
def puts(device // group_leader(), item) when is_iolist(item) do
115121
erl_dev = map_dev(device)
116-
:io.put_chars erl_dev, [to_iodata(item), ?\n]
122+
:io.put_chars erl_dev, [item, ?\n]
117123
end
118124

119125
@doc """
@@ -182,7 +188,7 @@ defmodule IO do
182188
Otherwise, `count` is the number of raw bytes to be retrieved.
183189
"""
184190
def getn(device, prompt, count) do
185-
:io.get_chars(map_dev(device), to_iodata(prompt), count)
191+
:io.get_chars(map_dev(device), prompt, count)
186192
end
187193

188194
@doc """
@@ -198,7 +204,7 @@ defmodule IO do
198204
NFS file system.
199205
"""
200206
def gets(device // group_leader(), prompt) do
201-
:io.get_line(map_dev(device), to_iodata(prompt))
207+
:io.get_line(map_dev(device), prompt)
202208
end
203209

204210
@doc """
@@ -271,7 +277,4 @@ defmodule IO do
271277
defp map_dev(:stdio), do: :standard_io
272278
defp map_dev(:stderr), do: :standard_error
273279
defp map_dev(other), do: other
274-
275-
defp to_iodata(io) when is_list(io) or is_binary(io), do: io
276-
defp to_iodata(other), do: to_string(other)
277280
end

lib/elixir/test/elixir/enum_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,7 @@ defmodule EnumTest.Others do
751751
test :take_with_side_effects do
752752
reducible = fn(acc, fun) ->
753753
Enum.reduce([1, 2, 3], acc, fn(x, acc) ->
754-
IO.puts x
754+
IO.puts to_string(x)
755755
fun.(x, acc)
756756
end)
757757
end

lib/elixir/test/elixir/kernel/cli_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ defmodule Kernel.CLI.InitTest do
66
use ExUnit.Case, async: true
77

88
test :code_init do
9-
assert elixir('-e "IO.puts 3"') == '3\n'
9+
assert elixir('-e "IO.puts [?3]"') == '3\n'
1010

1111
result = elixir('-e "IO.puts inspect(System.argv)" #{fixture_path("init_sample.exs")} -o 1 2 3')
1212
assert result == '#{inspect ["-o", "1", "2", "3"]}\n3\n'

0 commit comments

Comments
 (0)