ruby-changes:61573
From: Jean <ko1@a...>
Date: Fri, 5 Jun 2020 11:50:17 +0900 (JST)
Subject: [ruby-changes:61573] 4085c51a51 (master): [ruby/psych] Implement a freeze load option
https://git.ruby-lang.org/ruby.git/commit/?id=4085c51a51 From 4085c51a5175aeff2aeb8b849214c2899a79eb19 Mon Sep 17 00:00:00 2001 From: Jean Boussier <jean.boussier@g...> Date: Wed, 24 Jul 2019 15:35:55 -0400 Subject: [ruby/psych] Implement a freeze load option https://github.com/ruby/psych/commit/04f97f7a56 diff --git a/ext/psych/lib/psych.rb b/ext/psych/lib/psych.rb index 67024fb..3fc98db 100644 --- a/ext/psych/lib/psych.rb +++ b/ext/psych/lib/psych.rb @@ -268,7 +268,7 @@ module Psych https://github.com/ruby/ruby/blob/trunk/ext/psych/lib/psych.rb#L268 # YAML documents that are supplied via user input. Instead, please use the # safe_load method. # - def self.load yaml, legacy_filename = NOT_GIVEN, filename: nil, fallback: false, symbolize_names: false + def self.load yaml, legacy_filename = NOT_GIVEN, filename: nil, fallback: false, symbolize_names: false, freeze: false if legacy_filename != NOT_GIVEN warn_with_uplevel 'Passing filename with the 2nd argument of Psych.load is deprecated. Use keyword argument like Psych.load(yaml, filename: ...) instead.', uplevel: 1 if $VERBOSE filename = legacy_filename @@ -276,7 +276,7 @@ module Psych https://github.com/ruby/ruby/blob/trunk/ext/psych/lib/psych.rb#L276 result = parse(yaml, filename: filename) return fallback unless result - result = result.to_ruby(symbolize_names: symbolize_names) if result + result = result.to_ruby(symbolize_names: symbolize_names, freeze: freeze) if result result end @@ -324,7 +324,7 @@ module Psych https://github.com/ruby/ruby/blob/trunk/ext/psych/lib/psych.rb#L324 # Psych.safe_load("---\n foo: bar") # => {"foo"=>"bar"} # Psych.safe_load("---\n foo: bar", symbolize_names: true) # => {:foo=>"bar"} # - def self.safe_load yaml, legacy_permitted_classes = NOT_GIVEN, legacy_permitted_symbols = NOT_GIVEN, legacy_aliases = NOT_GIVEN, legacy_filename = NOT_GIVEN, permitted_classes: [], permitted_symbols: [], aliases: false, filename: nil, fallback: nil, symbolize_names: false + def self.safe_load yaml, legacy_permitted_classes = NOT_GIVEN, legacy_permitted_symbols = NOT_GIVEN, legacy_aliases = NOT_GIVEN, legacy_filename = NOT_GIVEN, permitted_classes: [], permitted_symbols: [], aliases: false, filename: nil, fallback: nil, symbolize_names: false, freeze: false if legacy_permitted_classes != NOT_GIVEN warn_with_uplevel 'Passing permitted_classes with the 2nd argument of Psych.safe_load is deprecated. Use keyword argument like Psych.safe_load(yaml, permitted_classes: ...) instead.', uplevel: 1 if $VERBOSE permitted_classes = legacy_permitted_classes @@ -352,9 +352,9 @@ module Psych https://github.com/ruby/ruby/blob/trunk/ext/psych/lib/psych.rb#L352 permitted_symbols.map(&:to_s)) scanner = ScalarScanner.new class_loader visitor = if aliases - Visitors::ToRuby.new scanner, class_loader, symbolize_names: symbolize_names + Visitors::ToRuby.new scanner, class_loader, symbolize_names: symbolize_names, freeze: freeze else - Visitors::NoAliasRuby.new scanner, class_loader, symbolize_names: symbolize_names + Visitors::NoAliasRuby.new scanner, class_loader, symbolize_names: symbolize_names, freeze: freeze end result = visitor.accept result result diff --git a/ext/psych/lib/psych/nodes/node.rb b/ext/psych/lib/psych/nodes/node.rb index 7e414e7..05cb08d 100644 --- a/ext/psych/lib/psych/nodes/node.rb +++ b/ext/psych/lib/psych/nodes/node.rb @@ -46,8 +46,8 @@ module Psych https://github.com/ruby/ruby/blob/trunk/ext/psych/lib/psych/nodes/node.rb#L46 # Convert this node to Ruby. # # See also Psych::Visitors::ToRuby - def to_ruby(symbolize_names: false) - Visitors::ToRuby.create(symbolize_names: symbolize_names).accept(self) + def to_ruby(symbolize_names: false, freeze: false) + Visitors::ToRuby.create(symbolize_names: symbolize_names, freeze: freeze).accept(self) end alias :transform :to_ruby diff --git a/ext/psych/lib/psych/visitors/to_ruby.rb b/ext/psych/lib/psych/visitors/to_ruby.rb index 3021aa7..68452e5 100644 --- a/ext/psych/lib/psych/visitors/to_ruby.rb +++ b/ext/psych/lib/psych/visitors/to_ruby.rb @@ -12,21 +12,22 @@ module Psych https://github.com/ruby/ruby/blob/trunk/ext/psych/lib/psych/visitors/to_ruby.rb#L12 ### # This class walks a YAML AST, converting each node to Ruby class ToRuby < Psych::Visitors::Visitor - def self.create(symbolize_names: false) + def self.create(symbolize_names: false, freeze: false) class_loader = ClassLoader.new scanner = ScalarScanner.new class_loader - new(scanner, class_loader, symbolize_names: symbolize_names) + new(scanner, class_loader, symbolize_names: symbolize_names, freeze: freeze) end attr_reader :class_loader - def initialize ss, class_loader, symbolize_names: false + def initialize ss, class_loader, symbolize_names: false, freeze: false super() @st = {} @ss = ss @domain_types = Psych.domain_types @class_loader = class_loader @symbolize_names = symbolize_names + @freeze = freeze end def accept target -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/