Skip to content

Commit 1cbde5b

Browse files
committed
merge revision(s) r46391,r46395: [Backport ruby#9766]
* lib/csv.rb (CSV#<<): honor explicity given encoding. based on the patch by DAISUKE TANIWAKI <daisuketaniwaki AT gmail.com> at [ruby-core:62113]. [Bug ruby#9766] * lib/csv.rb (CSV#<<): honor explicitly given encoding. based on git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_1@47586 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent 9755952 commit 1cbde5b

File tree

4 files changed

+27
-10
lines changed

4 files changed

+27
-10
lines changed

ChangeLog

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
Mon Sep 15 00:02:20 2014 Nobuyoshi Nakada <[email protected]>
2+
3+
* lib/csv.rb (CSV#<<): honor explicitly given encoding. based on
4+
the patch by DAISUKE TANIWAKI <daisuketaniwaki AT gmail.com> at
5+
[ruby-core:62113]. [Bug #9766]
6+
17
Wed Sep 10 23:36:38 2014 Koichi Sasada <[email protected]>
28

39
* test/ruby/test_object.rb: extend timeout.

lib/csv.rb

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,9 +1148,9 @@ def self.generate(*args)
11481148
io.seek(0, IO::SEEK_END)
11491149
args.unshift(io)
11501150
else
1151-
encoding = (args[-1] = args[-1].dup).delete(:encoding) if args.last.is_a?(Hash)
1151+
encoding = args[-1][:encoding] if args.last.is_a?(Hash)
11521152
str = ""
1153-
str.encode!(encoding) if encoding
1153+
str.force_encoding(encoding) if encoding
11541154
args.unshift(str)
11551155
end
11561156
csv = new(*args) # wrap
@@ -1515,7 +1515,7 @@ def initialize(data, options = Hash.new)
15151515
init_headers(options)
15161516
init_comments(options)
15171517

1518-
options.delete(:encoding)
1518+
@force_encoding = !!(encoding || options.delete(:encoding))
15191519
options.delete(:internal_encoding)
15201520
options.delete(:external_encoding)
15211521
unless options.empty?
@@ -1655,10 +1655,13 @@ def <<(row)
16551655

16561656
output = row.map(&@quote).join(@col_sep) + @row_sep # quote and separate
16571657
if @io.is_a?(StringIO) and
1658-
output.encoding != raw_encoding and
1659-
(compatible_encoding = Encoding.compatible?(@io.string, output))
1660-
@io.set_encoding(compatible_encoding)
1661-
@io.seek(0, IO::SEEK_END)
1658+
output.encoding != (encoding = raw_encoding)
1659+
if @force_encoding
1660+
output = output.encode(encoding)
1661+
elsif (compatible_encoding = Encoding.compatible?(@io.string, output))
1662+
@io.set_encoding(compatible_encoding)
1663+
@io.seek(0, IO::SEEK_END)
1664+
end
16621665
end
16631666
@io << output
16641667

test/csv/test_encodings.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,14 @@ def test_encoding_is_upgraded_for_ascii_content_during_writing_as_needed
257257
assert_equal("UTF-8", data.to_csv.encoding.name)
258258
end
259259

260+
def test_explicit_encoding
261+
bug9766 = '[ruby-core:62113] [Bug #9766]'
262+
s = CSV.generate(encoding: "Windows-31J") do |csv|
263+
csv << ["foo".force_encoding("ISO-8859-1"), "\u3042"]
264+
end
265+
assert_equal(["foo,\u3042\n".encode(Encoding::Windows_31J), Encoding::Windows_31J], [s, s.encoding], bug9766)
266+
end
267+
260268
private
261269

262270
def assert_parses(fields, encoding, options = { })

version.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#define RUBY_VERSION "2.1.2"
2-
#define RUBY_RELEASE_DATE "2014-09-10"
3-
#define RUBY_PATCHLEVEL 235
2+
#define RUBY_RELEASE_DATE "2014-09-15"
3+
#define RUBY_PATCHLEVEL 236
44

55
#define RUBY_RELEASE_YEAR 2014
66
#define RUBY_RELEASE_MONTH 9
7-
#define RUBY_RELEASE_DAY 10
7+
#define RUBY_RELEASE_DAY 15
88

99
#include "ruby/version.h"
1010

0 commit comments

Comments
 (0)