ruby-changes:19690
From: jeg2 <ko1@a...>
Date: Wed, 25 May 2011 23:46:16 +0900 (JST)
Subject: [ruby-changes:19690] jeg2:r31735 (trunk): * lib/csv.rb: Improved stray quoting error message (patch by Edvard Majakari).
jeg2 2011-05-25 23:46:08 +0900 (Wed, 25 May 2011) New Revision: 31735 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=31735 Log: * lib/csv.rb: Improved stray quoting error message (patch by Edvard Majakari). * lib/csv.rb: Remove debugging prints. Modified files: trunk/lib/csv.rb trunk/test/csv/test_csv_parsing.rb trunk/test/csv/test_encodings.rb Index: lib/csv.rb =================================================================== --- lib/csv.rb (revision 31734) +++ lib/csv.rb (revision 31735) @@ -1567,23 +1567,24 @@ @io = data.is_a?(String) ? StringIO.new(data) : data # honor the IO encoding if we can, otherwise default to ASCII-8BIT @encoding = raw_encoding(nil) || - (if encoding = options.delete(:internal_encoding) - case encoding - when Encoding; encoding - else Encoding.find(encoding) - end - end) || - (case encoding = options.delete(:encoding) - when Encoding; encoding - when /\A[^:]+/; Encoding.find($&) - end) || + ( if encoding = options.delete(:internal_encoding) + case encoding + when Encoding; encoding + else Encoding.find(encoding) + end + end ) || + ( case encoding = options.delete(:encoding) + when Encoding; encoding + when /\A[^:]+/; Encoding.find($&) + end ) || Encoding.default_internal || Encoding.default_external # # prepare for building safe regular expressions in the target encoding, # if we can transcode the needed characters # @re_esc = "\\".encode(@encoding) rescue "" - @re_chars = /#{%"[-][\\.^$?*+{}()|# \r\n\t\f\v]".encode(@encoding, fallback: proc{""})}/ + @re_chars = /#{%"[-][\\.^$?*+{}()|# \r\n\t\f\v]".encode(@encoding)}/ + # @re_chars = /#{%"[-][\\.^$?*+{}()|# \r\n\t\f\v]".encode(@encoding, fallback: proc{""})}/ init_separators(options) init_parsers(options) @@ -1884,7 +1885,10 @@ if part[-1] == @quote_char && part.count(@quote_char) % 2 != 0 # extended column ends csv.last << part[0..-2] - raise MalformedCSVError if csv.last =~ @parsers[:stray_quote] + if csv.last =~ @parsers[:stray_quote] + raise MalformedCSVError, + "Missing or stray quote in line #{lineno + 1}" + end csv.last.gsub!(@quote_char * 2, @quote_char) in_extended_col = false else @@ -1901,7 +1905,10 @@ else # regular quoted column csv << part[1..-2] - raise MalformedCSVError if csv.last =~ @parsers[:stray_quote] + if csv.last =~ @parsers[:stray_quote] + raise MalformedCSVError, + "Missing or stray quote in line #{lineno + 1}" + end csv.last.gsub!(@quote_char * 2, @quote_char) end elsif part =~ @parsers[:quote_or_nl] @@ -1910,7 +1917,7 @@ raise MalformedCSVError, "Unquoted fields do not allow " + "\\r or \\n (line #{lineno + 1})." else - raise MalformedCSVError, "Illegal quoting on line #{lineno + 1}." + raise MalformedCSVError, "Illegal quoting in line #{lineno + 1}." end else # Regular ole unquoted field. Index: test/csv/test_csv_parsing.rb =================================================================== --- test/csv/test_csv_parsing.rb (revision 31734) +++ test/csv/test_csv_parsing.rb (revision 31735) @@ -191,7 +191,7 @@ assert_send([csv.lineno, :<, 4]) end rescue CSV::MalformedCSVError - assert_equal("Illegal quoting on line 4.", $!.message) + assert_equal("Illegal quoting in line 4.", $!.message) end end Index: test/csv/test_encodings.rb =================================================================== --- test/csv/test_encodings.rb (revision 31734) +++ test/csv/test_encodings.rb (revision 31735) @@ -80,15 +80,19 @@ end def test_read_with_default_encoding - data = "abc" + data = "abc" default_external = Encoding.default_external each_encoding do |encoding| File.open(@temp_csv_path, "wb", encoding: encoding) {|f| f << data} begin - Encoding.default_external = encoding + no_warnings do + Encoding.default_external = encoding + end result = CSV.read(@temp_csv_path)[0][0] ensure - Encoding.default_external = default_external + no_warnings do + Encoding.default_external = default_external + end end assert_equal(encoding, result.encoding) end @@ -325,4 +329,11 @@ yield encoding end end + + def no_warnings + old_verbose, $VERBOSE = $VERBOSE, nil + yield + ensure + $VERBOSE = old_verbose + end end -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/