Project

General

Profile

Actions

Bug #8767

closed

IO.copy_stream should write in binary mode.

Added by godfat (Lin Jen-Shin) over 10 years ago. Updated over 10 years ago.

Status:
Closed
Assignee:
-
Target version:
ruby -v:
ruby 2.1.0dev (2013-08-11 trunk 42495) [x86_64-darwin12.2.1]
[ruby-core:56518]

Description

This patch makes `IO.copy_stream' always copy in binary mode,
fixing the following scenario:

require 'tempfile'
require 'stringio'
Encoding.default_internal = 'UTF-8'
out = Tempfile.new('out')
out.binmode
# before this patch it raises:
# in `write': "\xDE" from ASCII-8BIT to UTF-8 (Encoding::UndefinedConversionError)
IO.copy_stream(StringIO.new("\xDE\xAD\xBE\xEF"), out)

The other way to fix this would be trying to preserve the file mode
from Tempfile instead of always writing in binary mode. However,
this won't be the case for other objects responding to `to_path'.

I think as we're treating destination as streams, we would always
want writing in binary. So I guess this is ok.

Thank you for reviewing.
This is discovered by using rubyzip, which is using Tempfile for
buffering input/output stream.

Commit on Github:
https://github.com/godfat/ruby/commit/94d9f3dd733fd19f5ade7b6e6f5bdf0c904e06c1


Files

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0