mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
[ruby/psych] Raise specific error when aliases are not enabled
https://github.com/ruby/psych/commit/0c11ddcf46
This commit is contained in:
parent
71f89c2874
commit
54219ae8c4
9 changed files with 17 additions and 10 deletions
|
@ -307,7 +307,7 @@ module Psych
|
||||||
# A Psych::DisallowedClass exception will be raised if the yaml contains a
|
# A Psych::DisallowedClass exception will be raised if the yaml contains a
|
||||||
# class that isn't in the +permitted_classes+ list.
|
# class that isn't in the +permitted_classes+ list.
|
||||||
#
|
#
|
||||||
# A Psych::BadAlias exception will be raised if the yaml contains aliases
|
# A Psych::AliasesNotEnabled exception will be raised if the yaml contains aliases
|
||||||
# but the +aliases+ keyword argument is set to false.
|
# but the +aliases+ keyword argument is set to false.
|
||||||
#
|
#
|
||||||
# +filename+ will be used in the exception message if any exception is raised
|
# +filename+ will be used in the exception message if any exception is raised
|
||||||
|
|
|
@ -6,6 +6,13 @@ module Psych
|
||||||
class BadAlias < Exception
|
class BadAlias < Exception
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Subclasses `BadAlias` for backwards compatibility
|
||||||
|
class AliasesNotEnabled < BadAlias
|
||||||
|
def initialize
|
||||||
|
super "Alias parsing was not enabled. To enable it, pass `aliases: true` to `Psych::load` or `Psych::safe_load`."
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
class DisallowedClass < Exception
|
class DisallowedClass < Exception
|
||||||
def initialize action, klass_name
|
def initialize action, klass_name
|
||||||
super "Tried to #{action} unspecified class: #{klass_name}"
|
super "Tried to #{action} unspecified class: #{klass_name}"
|
||||||
|
|
|
@ -427,7 +427,7 @@ module Psych
|
||||||
|
|
||||||
class NoAliasRuby < ToRuby
|
class NoAliasRuby < ToRuby
|
||||||
def visit_Psych_Nodes_Alias o
|
def visit_Psych_Nodes_Alias o
|
||||||
raise BadAlias, "Unknown alias: #{o.anchor}"
|
raise AliasesNotEnabled
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -51,7 +51,7 @@ module Psych
|
||||||
:UseVersion => true, :UseHeader => true, :SortKeys => true
|
:UseVersion => true, :UseHeader => true, :SortKeys => true
|
||||||
)
|
)
|
||||||
))
|
))
|
||||||
rescue Psych::DisallowedClass, Psych::BadAlias
|
rescue Psych::DisallowedClass, Psych::BadAlias, Psych::AliasesNotEnabled
|
||||||
assert_to_yaml obj, yaml, :unsafe_load
|
assert_to_yaml obj, yaml, :unsafe_load
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -61,7 +61,7 @@ module Psych
|
||||||
def assert_parse_only( obj, yaml )
|
def assert_parse_only( obj, yaml )
|
||||||
begin
|
begin
|
||||||
assert_equal obj, Psych::load( yaml )
|
assert_equal obj, Psych::load( yaml )
|
||||||
rescue Psych::DisallowedClass, Psych::BadAlias
|
rescue Psych::DisallowedClass, Psych::BadAlias, Psych::AliasesNotEnabled
|
||||||
assert_equal obj, Psych::unsafe_load( yaml )
|
assert_equal obj, Psych::unsafe_load( yaml )
|
||||||
end
|
end
|
||||||
assert_equal obj, Psych::parse( yaml ).transform
|
assert_equal obj, Psych::parse( yaml ).transform
|
||||||
|
@ -79,7 +79,7 @@ module Psych
|
||||||
assert_equal(obj, Psych.load(v.tree.yaml))
|
assert_equal(obj, Psych.load(v.tree.yaml))
|
||||||
assert_equal(obj, Psych::load(Psych.dump(obj)))
|
assert_equal(obj, Psych::load(Psych.dump(obj)))
|
||||||
assert_equal(obj, Psych::load(obj.to_yaml))
|
assert_equal(obj, Psych::load(obj.to_yaml))
|
||||||
rescue Psych::DisallowedClass, Psych::BadAlias
|
rescue Psych::DisallowedClass, Psych::BadAlias, Psych::AliasesNotEnabled
|
||||||
assert_equal(obj, Psych.unsafe_load(v.tree.yaml))
|
assert_equal(obj, Psych.unsafe_load(v.tree.yaml))
|
||||||
assert_equal(obj, Psych::unsafe_load(Psych.dump(obj)))
|
assert_equal(obj, Psych::unsafe_load(Psych.dump(obj)))
|
||||||
assert_equal(obj, Psych::unsafe_load(obj.to_yaml))
|
assert_equal(obj, Psych::unsafe_load(obj.to_yaml))
|
||||||
|
|
|
@ -68,7 +68,7 @@ module Psych
|
||||||
def test_recursive_array_uses_alias
|
def test_recursive_array_uses_alias
|
||||||
@list << @list
|
@list << @list
|
||||||
|
|
||||||
assert_raise(BadAlias) do
|
assert_raise(AliasesNotEnabled) do
|
||||||
Psych.load(Psych.dump(@list), aliases: false)
|
Psych.load(Psych.dump(@list), aliases: false)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -125,7 +125,7 @@ eoyml
|
||||||
h = { }
|
h = { }
|
||||||
h["recursive_reference"] = h
|
h["recursive_reference"] = h
|
||||||
|
|
||||||
assert_raise(BadAlias) do
|
assert_raise(AliasesNotEnabled) do
|
||||||
Psych.load(Psych.dump(h), aliases: false)
|
Psych.load(Psych.dump(h), aliases: false)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -117,7 +117,7 @@ development:
|
||||||
bar:
|
bar:
|
||||||
<< : *foo
|
<< : *foo
|
||||||
eoyml
|
eoyml
|
||||||
exp = assert_raise(Psych::BadAlias) { Psych.load yaml }
|
exp = assert_raise(Psych::BadAlias) { Psych.load(yaml, aliases: true) }
|
||||||
assert_match 'foo', exp.message
|
assert_match 'foo', exp.message
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -46,7 +46,7 @@ module Psych
|
||||||
foo = Foo.new(nil)
|
foo = Foo.new(nil)
|
||||||
foo.parent = foo
|
foo.parent = foo
|
||||||
|
|
||||||
assert_raise(BadAlias) do
|
assert_raise(AliasesNotEnabled) do
|
||||||
Psych.load(Psych.dump(foo), permitted_classes: [Foo], aliases: false)
|
Psych.load(Psych.dump(foo), permitted_classes: [Foo], aliases: false)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -28,7 +28,7 @@ module Psych
|
||||||
b: *ABC
|
b: *ABC
|
||||||
YAML
|
YAML
|
||||||
|
|
||||||
assert_raise(Psych::BadAlias) do
|
assert_raise(Psych::AliasesNotEnabled) do
|
||||||
Psych.safe_load(yaml_with_aliases)
|
Psych.safe_load(yaml_with_aliases)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue