summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorrm155 <86454369+rm155@users.noreply.github.com>2021-10-10 22:21:42 -0400
committerSutou Kouhei <kou@cozmixng.org>2021-10-24 05:57:33 +0900
commitee948fc1b4cb1ad382beee709008bb93b8f6ba75 (patch)
tree323e979f3c67259699d838228d28a0ea4fb3ac92 /lib
parent274882be62e5996d804e87103586feaeec381820 (diff)
downloadruby-ee948fc1b4cb1ad382beee709008bb93b8f6ba75.tar.gz
[ruby/csv] Add support for Ractor (https://github.com/ruby/csv/pull/218)
https://github.com/ruby/csv/commit/a802690e11
Diffstat (limited to 'lib')
-rw-r--r--lib/csv.rb21
-rw-r--r--lib/csv/csv.gemspec2
-rw-r--r--lib/csv/parser.rb11
3 files changed, 28 insertions, 6 deletions
diff --git a/lib/csv.rb b/lib/csv.rb
index 87c3a4be31..42e99435cb 100644
--- a/lib/csv.rb
+++ b/lib/csv.rb
@@ -536,6 +536,14 @@ using CSV::MatchP if CSV.const_defined?(:MatchP)
#
# There is no such storage structure for write headers.
#
+# In order for the parsing methods to access stored converters in non-main-Ractors, the
+# storage structure must be made shareable first.
+# Therefore, <tt>Ractor.make_shareable(CSV::Converters)</tt> and
+# <tt>Ractor.make_shareable(CSV::HeaderConverters)</tt> must be called before the creation
+# of Ractors that use the converters stored in these structures. (Since making the storage
+# structures shareable involves freezing them, any custom converters that are to be used
+# must be added first.)
+#
# ===== Converter Lists
#
# A _converter_ _list_ is an \Array that may include any assortment of:
@@ -908,6 +916,7 @@ class CSV
gsub(/\s+/, "_").to_sym
}
}
+
# Default values for method options.
DEFAULT_OPTIONS = {
# For both parsing and generating.
@@ -946,6 +955,8 @@ class CSV
# Creates or retrieves cached \CSV objects.
# For arguments and options, see CSV.new.
#
+ # This API is not Ractor-safe.
+ #
# ---
#
# With no block given, returns a \CSV object.
@@ -1873,6 +1884,10 @@ class CSV
# csv.converters # => [:integer]
# csv.convert(proc {|x| x.to_s })
# csv.converters
+ #
+ # Notes that you need to call
+ # +Ractor.make_shareable(CSV::Converters)+ on the main Ractor to use
+ # this method.
def converters
parser_fields_converter.map do |converter|
name = Converters.rassoc(converter)
@@ -1935,6 +1950,10 @@ class CSV
# Returns an \Array containing header converters; used for parsing;
# see {Header Converters}[#class-CSV-label-Header+Converters]:
# CSV.new('').header_converters # => []
+ #
+ # Notes that you need to call
+ # +Ractor.make_shareable(CSV::HeaderConverters)+ on the main Ractor
+ # to use this method.
def header_converters
header_fields_converter.map do |converter|
name = HeaderConverters.rassoc(converter)
@@ -2655,6 +2674,8 @@ end
# io = StringIO.new
# CSV(io, col_sep: ";") { |csv| csv << ["a", "b", "c"] }
#
+# This API is not Ractor-safe.
+#
def CSV(*args, **options, &block)
CSV.instance(*args, **options, &block)
end
diff --git a/lib/csv/csv.gemspec b/lib/csv/csv.gemspec
index 6948e9a72f..11c5b0f2a6 100644
--- a/lib/csv/csv.gemspec
+++ b/lib/csv/csv.gemspec
@@ -60,5 +60,5 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "bundler"
spec.add_development_dependency "rake"
spec.add_development_dependency "benchmark_driver"
- spec.add_development_dependency "test-unit", ">= 3.4.3"
+ spec.add_development_dependency "test-unit", ">= 3.4.8"
end
diff --git a/lib/csv/parser.rb b/lib/csv/parser.rb
index 0d8a157fd7..3334acfbdd 100644
--- a/lib/csv/parser.rb
+++ b/lib/csv/parser.rb
@@ -480,9 +480,9 @@ class CSV
begin
StringScanner.new("x").scan("x")
rescue TypeError
- @@string_scanner_scan_accept_string = false
+ STRING_SCANNER_SCAN_ACCEPT_STRING = false
else
- @@string_scanner_scan_accept_string = true
+ STRING_SCANNER_SCAN_ACCEPT_STRING = true
end
def prepare_separators
@@ -506,7 +506,7 @@ class CSV
@first_column_separators = Regexp.new(@escaped_first_column_separator +
"+".encode(@encoding))
else
- if @@string_scanner_scan_accept_string
+ if STRING_SCANNER_SCAN_ACCEPT_STRING
@column_end = @column_separator
else
@column_end = Regexp.new(@escaped_column_separator)
@@ -725,6 +725,8 @@ class CSV
end
end
+ SCANNER_TEST_CHUNK_SIZE =
+ Integer((ENV["CSV_PARSER_SCANNER_TEST_CHUNK_SIZE"] || "1"), 10)
def build_scanner
inputs = @samples.collect do |sample|
UnoptimizedStringIO.new(sample)
@@ -734,10 +736,9 @@ class CSV
else
inputs << @input
end
- chunk_size = ENV["CSV_PARSER_SCANNER_TEST_CHUNK_SIZE"] || "1"
InputsScanner.new(inputs,
@encoding,
- chunk_size: Integer(chunk_size, 10))
+ chunk_size: SCANNER_TEST_CHUNK_SIZE)
end
else
def build_scanner