Skip to content

Commit

Permalink
[GR-18163] Process#spawn should call #to_io on non-IO file descriptor…
Browse files Browse the repository at this point in the history
… objects (#2809)

PullRequest: truffleruby/3589
  • Loading branch information
eregon committed Dec 19, 2022
2 parents d4c94d8 + 9387900 commit 4a1c573
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Compatibility:
* Warn about unknown directive passed to `Array#pack` in verbose mode (#2791, @andrykonchin).
* Added constants `IO::SEEK_DATE` and `IO::SEEK_HOLE` (#2792, @andrykonchin).
* Fix `StringIO.new` to accept keyword arguments (#2793, @andrykonchin).
* `Process#spawn` should call `#to_io` on non-IO file descriptor objects (#2809, @jcouball).

Performance:

Expand Down
10 changes: 10 additions & 0 deletions spec/ruby/core/process/spawn_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,16 @@ def child_pids(pid)

# redirection

it 'redirects to the wrapped IO using wrapped_io.to_io if out: wrapped_io' do
File.open(@name, 'w') do |file|
-> do
wrapped_io = mock('wrapped IO')
wrapped_io.should_receive(:to_io).and_return(file)
Process.wait Process.spawn('echo "Hello World"', out: wrapped_io)
end.should output_to_fd("Hello World\n", file)
end
end

it "redirects STDOUT to the given file descriptor if out: Integer" do
File.open(@name, 'w') do |file|
-> do
Expand Down
6 changes: 5 additions & 1 deletion src/main/ruby/truffleruby/core/truffle/process_operations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,11 @@ def convert_to_fd(obj, target)
open_file_for_child(obj[0], convert_file_mode(obj[1]), obj[2])
end
else
raise ArgumentError, "wrong exec redirect: #{obj.inspect}"
if obj.respond_to?(:to_io)
obj.to_io.fileno
else
raise ArgumentError, "wrong exec redirect: #{obj.inspect}"
end
end
end

Expand Down

0 comments on commit 4a1c573

Please sign in to comment.