From 94d9f3dd733fd19f5ade7b6e6f5bdf0c904e06c1 Mon Sep 17 00:00:00 2001 From: Lin Jen-Shin Date: Sun, 11 Aug 2013 01:30:52 +0800 Subject: [PATCH] io.c: IO.copy_stream should write in binary mode. 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. --- io.c | 1 + 1 file changed, 1 insertion(+) diff --git a/io.c b/io.c index 8678af7..97da92f 100644 --- a/io.c +++ b/io.c @@ -10172,6 +10172,7 @@ copy_stream_body(VALUE arg) args[1] = INT2NUM(oflags); args[2] = INT2FIX(0666); dst_io = rb_class_new_instance(3, args, rb_cFile); + rb_io_binmode_m(dst_io); stp->dst = dst_io; stp->close_dst = 1; } -- 1.8.3.4