ruby-changes:72869
From: Alexander <ko1@a...>
Date: Tue, 9 Aug 2022 01:33:41 +0900 (JST)
Subject: [ruby-changes:72869] 9c13a6ce5f (master): [ruby/psych] Raise specific error when an anchor isn't defined
https://git.ruby-lang.org/ruby.git/commit/?id=9c13a6ce5f From 9c13a6ce5ff1f83489d9df65a5c7745efe16c8a4 Mon Sep 17 00:00:00 2001 From: Alexander Momchilov <alexander.momchilov@s...> Date: Fri, 22 Jul 2022 16:16:04 -0400 Subject: [ruby/psych] Raise specific error when an anchor isn't defined https://github.com/ruby/psych/commit/98fbd5247a --- ext/psych/lib/psych/exception.rb | 7 +++++++ ext/psych/lib/psych/visitors/to_ruby.rb | 2 +- test/psych/test_hash.rb | 2 +- test/psych/test_merge_keys.rb | 2 +- 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/ext/psych/lib/psych/exception.rb b/ext/psych/lib/psych/exception.rb index 04a9a906a4..d7469a4b30 100644 --- a/ext/psych/lib/psych/exception.rb +++ b/ext/psych/lib/psych/exception.rb @@ -13,6 +13,13 @@ module Psych https://github.com/ruby/ruby/blob/trunk/ext/psych/lib/psych/exception.rb#L13 end end + # Subclasses `BadAlias` for backwards compatibility + class AnchorNotDefined < BadAlias + def initialize anchor_name + super "An alias referenced an unknown anchor: #{anchor_name}" + end + end + class DisallowedClass < Exception def initialize action, klass_name super "Tried to #{action} unspecified class: #{klass_name}" diff --git a/ext/psych/lib/psych/visitors/to_ruby.rb b/ext/psych/lib/psych/visitors/to_ruby.rb index 0bf5198ccc..cce5daf3bb 100644 --- a/ext/psych/lib/psych/visitors/to_ruby.rb +++ b/ext/psych/lib/psych/visitors/to_ruby.rb @@ -323,7 +323,7 @@ module Psych https://github.com/ruby/ruby/blob/trunk/ext/psych/lib/psych/visitors/to_ruby.rb#L323 end def visit_Psych_Nodes_Alias o - @st.fetch(o.anchor) { raise BadAlias, "Unknown alias: #{o.anchor}" } + @st.fetch(o.anchor) { raise AnchorNotDefined, o.anchor } end private diff --git a/test/psych/test_hash.rb b/test/psych/test_hash.rb index 88b520ebfc..31eba8580b 100644 --- a/test/psych/test_hash.rb +++ b/test/psych/test_hash.rb @@ -124,7 +124,7 @@ module Psych https://github.com/ruby/ruby/blob/trunk/test/psych/test_hash.rb#L124 end def test_raises_if_anchor_not_defined - assert_raise(Psych::BadAlias) do + assert_raise(Psych::AnchorNotDefined) do Psych.unsafe_load(<<~eoyml) --- foo: &foo diff --git a/test/psych/test_merge_keys.rb b/test/psych/test_merge_keys.rb index 8d2fceabf5..2f55a1ed8e 100644 --- a/test/psych/test_merge_keys.rb +++ b/test/psych/test_merge_keys.rb @@ -117,7 +117,7 @@ development: https://github.com/ruby/ruby/blob/trunk/test/psych/test_merge_keys.rb#L117 bar: << : *foo eoyml - exp = assert_raise(Psych::BadAlias) { Psych.load(yaml, aliases: true) } + exp = assert_raise(Psych::AnchorNotDefined) { Psych.load(yaml, aliases: true) } assert_match 'foo', exp.message end -- cgit v1.2.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/