* lib/csv.rb: Improved stray quoting error message (patch by Edvard Majakari).
* lib/csv.rb: Remove debugging prints. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@31735 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
ce9d84a9f0
commit
6e7544f132
15
lib/csv.rb
15
lib/csv.rb
@ -1583,7 +1583,8 @@ class CSV
|
|||||||
# if we can transcode the needed characters
|
# if we can transcode the needed characters
|
||||||
#
|
#
|
||||||
@re_esc = "\\".encode(@encoding) rescue ""
|
@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_separators(options)
|
||||||
init_parsers(options)
|
init_parsers(options)
|
||||||
@ -1884,7 +1885,10 @@ class CSV
|
|||||||
if part[-1] == @quote_char && part.count(@quote_char) % 2 != 0
|
if part[-1] == @quote_char && part.count(@quote_char) % 2 != 0
|
||||||
# extended column ends
|
# extended column ends
|
||||||
csv.last << part[0..-2]
|
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)
|
csv.last.gsub!(@quote_char * 2, @quote_char)
|
||||||
in_extended_col = false
|
in_extended_col = false
|
||||||
else
|
else
|
||||||
@ -1901,7 +1905,10 @@ class CSV
|
|||||||
else
|
else
|
||||||
# regular quoted column
|
# regular quoted column
|
||||||
csv << part[1..-2]
|
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)
|
csv.last.gsub!(@quote_char * 2, @quote_char)
|
||||||
end
|
end
|
||||||
elsif part =~ @parsers[:quote_or_nl]
|
elsif part =~ @parsers[:quote_or_nl]
|
||||||
@ -1910,7 +1917,7 @@ class CSV
|
|||||||
raise MalformedCSVError, "Unquoted fields do not allow " +
|
raise MalformedCSVError, "Unquoted fields do not allow " +
|
||||||
"\\r or \\n (line #{lineno + 1})."
|
"\\r or \\n (line #{lineno + 1})."
|
||||||
else
|
else
|
||||||
raise MalformedCSVError, "Illegal quoting on line #{lineno + 1}."
|
raise MalformedCSVError, "Illegal quoting in line #{lineno + 1}."
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
# Regular ole unquoted field.
|
# Regular ole unquoted field.
|
||||||
|
@ -191,7 +191,7 @@ class TestCSV::Parsing < TestCSV
|
|||||||
assert_send([csv.lineno, :<, 4])
|
assert_send([csv.lineno, :<, 4])
|
||||||
end
|
end
|
||||||
rescue CSV::MalformedCSVError
|
rescue CSV::MalformedCSVError
|
||||||
assert_equal("Illegal quoting on line 4.", $!.message)
|
assert_equal("Illegal quoting in line 4.", $!.message)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -85,11 +85,15 @@ class TestCSV::Encodings < TestCSV
|
|||||||
each_encoding do |encoding|
|
each_encoding do |encoding|
|
||||||
File.open(@temp_csv_path, "wb", encoding: encoding) {|f| f << data}
|
File.open(@temp_csv_path, "wb", encoding: encoding) {|f| f << data}
|
||||||
begin
|
begin
|
||||||
|
no_warnings do
|
||||||
Encoding.default_external = encoding
|
Encoding.default_external = encoding
|
||||||
|
end
|
||||||
result = CSV.read(@temp_csv_path)[0][0]
|
result = CSV.read(@temp_csv_path)[0][0]
|
||||||
ensure
|
ensure
|
||||||
|
no_warnings do
|
||||||
Encoding.default_external = default_external
|
Encoding.default_external = default_external
|
||||||
end
|
end
|
||||||
|
end
|
||||||
assert_equal(encoding, result.encoding)
|
assert_equal(encoding, result.encoding)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -325,4 +329,11 @@ class TestCSV::Encodings < TestCSV
|
|||||||
yield encoding
|
yield encoding
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def no_warnings
|
||||||
|
old_verbose, $VERBOSE = $VERBOSE, nil
|
||||||
|
yield
|
||||||
|
ensure
|
||||||
|
$VERBOSE = old_verbose
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user