Support setopts request with binary option in IO proxy (#2983)

This commit is contained in:
Jonatan Kłosko 2025-04-11 08:20:50 +02:00 committed by GitHub
parent 710957b265
commit 331efa04f7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 22 additions and 6 deletions

View file

@ -228,12 +228,11 @@ defmodule Livebook.Runtime.Evaluator.IOProxy do
{{:error, :enotsup}, state}
end
defp io_request({:setopts, [encoding: encoding]}, state) when encoding in [:latin1, :unicode] do
{:ok, %{state | encoding: encoding}}
end
defp io_request({:setopts, _opts}, state) do
{{:error, :enotsup}, state}
defp io_request({:setopts, opts}, state) do
case setopts(opts, state) do
{:ok, state} -> {:ok, state}
:error -> {{:error, :enotsup}, state}
end
end
defp io_request(:getopts, state) do
@ -465,6 +464,17 @@ defmodule Livebook.Runtime.Evaluator.IOProxy do
ArgumentError -> {{:error, req}, state}
end
defp setopts([{:encoding, encoding} | opts], state) when encoding in [:latin1, :unicode] do
setopts(opts, %{state | encoding: encoding})
end
defp setopts([opt | opts], state) when opt in [:binary, {:binary, true}] do
setopts(opts, state)
end
defp setopts([], state), do: {:ok, state}
defp setopts(_opts, _state), do: :error
defp request_input_value(input_id, state) do
request = {:runtime_evaluation_input_request, state.ref, self(), input_id}
reply_tag = :runtime_evaluation_input_reply

View file

@ -44,6 +44,12 @@ defmodule Livebook.Runtime.Evaluator.IOProxyTest do
test "IO.gets", %{io: io} do
assert IO.gets(io, "name: ") == {:error, :enotsup}
end
test ":io.setopts", %{io: io} do
assert :ok = :io.setopts(io, encoding: :unicode)
assert :ok = :io.setopts(io, [:binary])
assert {:error, :enotsup} = :io.setopts(io, [:unknown_option])
end
end
describe "input" do