ruby-changes:52336
From: hsbt <ko1@a...>
Date: Mon, 27 Aug 2018 09:44:10 +0900 (JST)
Subject: [ruby-changes:52336] hsbt:r64544 (trunk): Merge psych-3.1.0.pre1.
hsbt 2018-08-27 09:44:04 +0900 (Mon, 27 Aug 2018) New Revision: 64544 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=64544 Log: Merge psych-3.1.0.pre1. * Update bundled libyaml-0.2.1 from 0.1.7. https://github.com/ruby/psych/pull/368 * Unify Psych's API: To use keyword arguments with method call. https://github.com/ruby/psych/pull/358 Added files: trunk/test/psych/test_yaml_special_cases.rb Modified files: trunk/ext/psych/lib/psych/versions.rb trunk/ext/psych/lib/psych.rb trunk/ext/psych/psych.gemspec trunk/ext/psych/yaml/api.c trunk/ext/psych/yaml/config.h trunk/ext/psych/yaml/dumper.c trunk/ext/psych/yaml/emitter.c trunk/ext/psych/yaml/loader.c trunk/ext/psych/yaml/parser.c trunk/ext/psych/yaml/reader.c trunk/ext/psych/yaml/scanner.c trunk/ext/psych/yaml/yaml_private.h trunk/test/psych/test_exception.rb trunk/test/psych/test_psych.rb trunk/test/psych/test_safe_load.rb Index: test/psych/test_exception.rb =================================================================== --- test/psych/test_exception.rb (revision 64543) +++ test/psych/test_exception.rb (revision 64544) @@ -30,9 +30,15 @@ module Psych https://github.com/ruby/ruby/blob/trunk/test/psych/test_exception.rb#L30 assert_nil ex.file ex = assert_raises(Psych::SyntaxError) do - Psych.load '--- `', 'meow' + Psych.load '--- `', filename: 'meow' end assert_equal 'meow', ex.file + + # deprecated interface + ex = assert_raises(Psych::SyntaxError) do + Psych.load '--- `', 'deprecated' + end + assert_equal 'deprecated', ex.file end def test_psych_parse_stream_takes_file @@ -43,7 +49,7 @@ module Psych https://github.com/ruby/ruby/blob/trunk/test/psych/test_exception.rb#L49 assert_match '(<unknown>)', ex.message ex = assert_raises(Psych::SyntaxError) do - Psych.parse_stream '--- `', 'omg!' + Psych.parse_stream '--- `', filename: 'omg!' end assert_equal 'omg!', ex.file assert_match 'omg!', ex.message @@ -57,9 +63,15 @@ module Psych https://github.com/ruby/ruby/blob/trunk/test/psych/test_exception.rb#L63 assert_match '(<unknown>)', ex.message ex = assert_raises(Psych::SyntaxError) do - Psych.load_stream '--- `', 'omg!' + Psych.load_stream '--- `', filename: 'omg!' end assert_equal 'omg!', ex.file + + # deprecated interface + ex = assert_raises(Psych::SyntaxError) do + Psych.load_stream '--- `', 'deprecated' + end + assert_equal 'deprecated', ex.file end def test_parse_file_exception @@ -94,9 +106,15 @@ module Psych https://github.com/ruby/ruby/blob/trunk/test/psych/test_exception.rb#L106 assert_nil ex.file ex = assert_raises(Psych::SyntaxError) do - Psych.parse '--- `', 'omg!' + Psych.parse '--- `', filename: 'omg!' end assert_match 'omg!', ex.message + + # deprecated interface + ex = assert_raises(Psych::SyntaxError) do + Psych.parse '--- `', 'deprecated' + end + assert_match 'deprecated', ex.message end def test_attributes Index: test/psych/test_safe_load.rb =================================================================== --- test/psych/test_safe_load.rb (revision 64543) +++ test/psych/test_safe_load.rb (revision 64544) @@ -22,6 +22,8 @@ module Psych https://github.com/ruby/ruby/blob/trunk/test/psych/test_safe_load.rb#L22 def test_explicit_recursion x = [] x << x + assert_equal(x, Psych.safe_load(Psych.dump(x), whitelist_classes: [], whitelist_symbols: [], aliases: true)) + # deprecated interface assert_equal(x, Psych.safe_load(Psych.dump(x), [], [], true)) end @@ -30,6 +32,16 @@ module Psych https://github.com/ruby/ruby/blob/trunk/test/psych/test_safe_load.rb#L32 assert_raises(Psych::DisallowedClass) do Psych.safe_load yml end + assert_equal( + :foo, + Psych.safe_load( + yml, + whitelist_classes: [Symbol], + whitelist_symbols: [:foo] + ) + ) + + # deprecated interface assert_equal(:foo, Psych.safe_load(yml, [Symbol], [:foo])) end @@ -38,32 +50,71 @@ module Psych https://github.com/ruby/ruby/blob/trunk/test/psych/test_safe_load.rb#L50 assert_safe_cycle :foo end assert_raises(Psych::DisallowedClass) do + Psych.safe_load '--- !ruby/symbol foo', whitelist_classes: [] + end + + # deprecated interface + assert_raises(Psych::DisallowedClass) do Psych.safe_load '--- !ruby/symbol foo', [] end - assert_safe_cycle :foo, [Symbol] - assert_safe_cycle :foo, %w{ Symbol } + + assert_safe_cycle :foo, whitelist_classes: [Symbol] + assert_safe_cycle :foo, whitelist_classes: %w{ Symbol } + assert_equal :foo, Psych.safe_load('--- !ruby/symbol foo', whitelist_classes: [Symbol]) + + # deprecated interface assert_equal :foo, Psych.safe_load('--- !ruby/symbol foo', [Symbol]) end def test_foo assert_raises(Psych::DisallowedClass) do + Psych.safe_load '--- !ruby/object:Foo {}', whitelist_classes: [Foo] + end + + # deprecated interface + assert_raises(Psych::DisallowedClass) do Psych.safe_load '--- !ruby/object:Foo {}', [Foo] end + assert_raises(Psych::DisallowedClass) do assert_safe_cycle Foo.new end + assert_kind_of(Foo, Psych.safe_load(Psych.dump(Foo.new), whitelist_classes: [Foo])) + + # deprecated interface assert_kind_of(Foo, Psych.safe_load(Psych.dump(Foo.new), [Foo])) end X = Struct.new(:x) def test_struct_depends_on_sym - assert_safe_cycle(X.new, [X, Symbol]) + assert_safe_cycle(X.new, whitelist_classes: [X, Symbol]) assert_raises(Psych::DisallowedClass) do - cycle X.new, [X] + cycle X.new, whitelist_classes: [X] end end def test_anon_struct + assert Psych.safe_load(<<-eoyml, whitelist_classes: [Struct, Symbol]) +--- !ruby/struct + foo: bar + eoyml + + assert_raises(Psych::DisallowedClass) do + Psych.safe_load(<<-eoyml, whitelist_classes: [Struct]) +--- !ruby/struct + foo: bar + eoyml + end + + assert_raises(Psych::DisallowedClass) do + Psych.safe_load(<<-eoyml, whitelist_classes: [Symbol]) +--- !ruby/struct + foo: bar + eoyml + end + end + + def test_deprecated_anon_struct assert Psych.safe_load(<<-eoyml, [Struct, Symbol]) --- !ruby/struct foo: bar @@ -84,14 +135,28 @@ module Psych https://github.com/ruby/ruby/blob/trunk/test/psych/test_safe_load.rb#L135 end end + def test_safe_load_default_fallback + assert_nil Psych.safe_load("") + end + + def test_safe_load + assert_equal %w[a b], Psych.safe_load("- a\n- b") + end + + def test_safe_load_raises_on_bad_input + assert_raises(Psych::SyntaxError) { Psych.safe_load("--- `") } + end + private - def cycle object, whitelist = [] - Psych.safe_load(Psych.dump(object), whitelist) + def cycle object, whitelist_classes: [] + Psych.safe_load(Psych.dump(object), whitelist_classes: whitelist_classes) + # deprecated interface test + Psych.safe_load(Psych.dump(object), whitelist_classes) end - def assert_safe_cycle object, whitelist = [] - other = cycle object, whitelist + def assert_safe_cycle object, whitelist_classes: [] + other = cycle object, whitelist_classes: whitelist_classes assert_equal object, other end end Index: test/psych/test_yaml_special_cases.rb =================================================================== --- test/psych/test_yaml_special_cases.rb (nonexistent) +++ test/psych/test_yaml_special_cases.rb (revision 64544) @@ -0,0 +1,130 @@ https://github.com/ruby/ruby/blob/trunk/test/psych/test_yaml_special_cases.rb#L1 +# frozen_string_literal: true + +require_relative 'helper' + +require 'stringio' +require 'tempfile' + +module Psych + class TestYamlSpecialCases < TestCase + def setup + super + end + + def test_empty_string + s = "" + assert_equal false, Psych.load(s) + assert_equal [], Psych.load_stream(s) + assert_equal false, Psych.parse(s) + assert_equal [], Psych.parse_stream(s).transform + assert_equal nil, Psych.safe_load(s) + end + + def test_false + s = "false" + assert_equal false, Psych.load(s) + assert_equal [false], Psych.load_stream(s) + assert_equal false, Psych.parse(s).transform + assert_equal [false], Psych.parse_stream(s).transform + assert_equal false, Psych.safe_load(s) + end + + def test_n + s = "n" + assert_equal "n", Psych.load(s) + assert_equal ["n"], Psych.load_stream(s) + assert_equal "n", Psych.parse(s).transform + assert_equal ["n"], Psych.parse_stream(s).transform + assert_equal "n", Psych.safe_load(s) + end + + def test_off + s = "off" + assert_equal false, Psych.load(s) + assert_equal [false], Psych.load_stream(s) + assert_equal false, Psych.parse(s).transform + assert_equal [false], Psych.parse_stream(s).transform + assert_equal false, Psych.safe_load(s) + end + + def test_inf + s = "-.inf" + assert_equal -Float::INFINITY, Psych.load(s) + assert_equal [-Float::INFINITY], Psych.load_stream(s) + assert_equal -Float::INFINITY, Psych.parse(s).transform + assert_equal [-Float::INFINITY], Psych.parse_stream(s).transform + assert_equal -Float::INFINITY, Psych.safe_load(s) + end + + def test_NaN + s = ".NaN" + assert Float::NAN, Psych.load(s).nan? + assert [Float::NAN], Psych.load_stream(s).first.nan? + assert Psych.parse(s).transform.nan? + assert Psych.parse_stream(s).transform.first.nan? + assert Psych.safe_load(s).nan? + end + + def test_0xC + s = "0xC" + assert_equal 12, Psych.load(s) + assert_equal [12], Psych.load_stream(s) + assert_equal 12, Psych.parse(s).transform + assert_equal [12], Psych.parse_stream(s).transform + assert_equal 12, Psych.safe_load(s) + end + + def test_arrows + s = "<<" + assert_equal "<<", Psych.load(s) + assert_equal ["<<"], Psych.load_stream(s) + assert_equal "<<", Psych.parse(s).transform + assert_equal ["<<"], Psych.parse_stream(s).transform + assert_equal "<<", Psych.safe_load(s) + end + + def test_arrows_hash + s = "<<: {}" + assert_equal({}, Psych.load(s)) + assert_equal [{}], Psych.load_stream(s) + assert_equal({}, Psych.parse(s).transform) + assert_equal [{}], Psych.parse_stream(s).transform + assert_equal({}, Psych.safe_load(s)) + end + + def test_thousand + s = "- 1000\n- +1000\n- 1_000" + assert_equal [1000, 1000, 1000], Psych.load(s) + assert_equal [[1000, 1000, 1000]], Psych.load_stream(s) + assert_equal [1000, 1000, 1000], Psych.parse(s).transform + assert_equal [[1000, 1000, 1000]], Psych.parse_stream(s).transform + assert_equal [1000, 1000, 1000], Psych.safe_load(s) + end + + def test_8 + s = "[8, 08, 0o10, 010]" + assert_equal [8, "08", "0o10", 8], Psych.load(s) + assert_equal [[8, "08", "0o10", 8]], Psych.load_stream(s) + assert_equal [8, "08", "0o10", 8], Psych.parse(s).transform + assert_equal [[8, "08", "0o10", 8]], Psych.parse_stream(s).transform + assert_equal [8, "08", "0o10", 8], Psych.safe_load(s) + end + + def test_null + s = "null" + assert_equal nil, Psych.load(s) + assert_equal [nil], Psych.load_stream(s) + assert_equal nil, Psych.parse(s).transform + assert_equal [nil], Psych.parse_stream(s).transform + assert_equal nil, Psych.safe_load(s) + end + + private + + def special_case_cycle(object) + %w[load load_stream parse parse_stream safe_load].map do |m| + Psych.public_send(m, object) + end + end + end +end Index: test/psych/test_psych.rb =================================================================== --- test/psych/test_psych.rb (revision 64543) +++ test/psych/test_psych.rb (revision 64544) @@ -60,6 +60,22 @@ class TestPsych < Psych::TestCase https://github.com/ruby/ruby/blob/trunk/test/psych/test_psych.rb#L60 end end + def test_parse + assert_equal %w[a b], Psych.parse("- a\n- b").to_ruby + end + + def test_parse_default_fallback + assert_equal false, Psych.parse("") + end + + def test_parse_raises_on_bad_input + assert_raises(Psych::SyntaxError) { Psych.parse("--- `") } + end + + def test_parse_with_fallback + assert_equal 42, Psych.parse("", fallback: 42) + end + def test_non_existing_class_on_deserialize e = assert_raises(ArgumentError) do Psych.load("--- !ruby/object:NonExistent\nfoo: 1") @@ -103,9 +119,44 @@ class TestPsych < Psych::TestCase https://github.com/ruby/ruby/blob/trunk/test/psych/test_psych.rb#L119 assert_equal %w{ foo bar }, docs end + def test_load_stream_default_fallback + assert_equal [], Psych.load_stream("") + end + + def test_load_stream_raises_on_bad_input + assert_raises(Psych::SyntaxError) { Psych.load_stream("--- `") } + end + def test_parse_stream docs = Psych.parse_stream("--- foo\n...\n--- bar\n...") - assert_equal %w{ foo bar }, docs.children.map { |x| x.transform } + assert_equal(%w[foo bar], docs.children.map(&:transform)) + end + + def test_parse_stream_with_block + docs = [] + Psych.parse_stream("--- foo\n...\n--- bar\n...") do |node| + docs << node + end + + assert_equal %w[foo bar], docs.map(&:to_ruby) + end + + def test_parse_stream_default_fallback + docs = Psych.parse_stream("") + assert_equal [], docs.children.map(&:to_ruby) + end + + def test_parse_stream_with_block_default_fallback + docs = [] + Psych.parse_stream("") do |node| + docs << node + end + + assert_equal [], docs.map(&:to_ruby) + end + + def test_parse_stream_raises_on_bad_input + assert_raises(Psych::SyntaxError) { Psych.parse_stream("--- `") } end def test_add_builtin_type @@ -135,6 +186,31 @@ class TestPsych < Psych::TestCase https://github.com/ruby/ruby/blob/trunk/test/psych/test_psych.rb#L186 assert_equal({ 'hello' => 'world' }, got) end + def test_load_default_fallback + assert_equal false, Psych.load("") + end + + def test_load_with_fallback + assert_equal 42, Psych.load("", "file", fallback: 42) + end + + def test_load_with_fallback_nil_or_false + assert_nil Psych.load("", "file", fallback: nil) + assert_equal false, Psych.load("", "file", fallback: false) + end + + def test_load_with_fallback_hash + assert_equal Hash.new, Psych.load("", "file", fallback: Hash.new) + end + + def test_load_with_fallback_for_nil + assert_nil Psych.load("--- null", "file", fallback: 42) + end + + def test_load_with_fallback_for_false + assert_equal false, Psych.load("--- false", "file", fallback: 42) + end + def test_load_file Tempfile.create(['yikes', 'yml']) {|t| t.binmode @@ -144,7 +220,7 @@ class TestPsych < Psych::TestCase https://github.com/ruby/ruby/blob/trunk/test/psych/test_psych.rb#L220 } end - def test_load_file_default_return_value + def test_load_file_default_fallback Tempfile.create(['empty', 'yml']) {|t| assert_equal false, Psych.load_file(t.path) } @@ -196,6 +272,12 @@ class TestPsych < Psych::TestCase https://github.com/ruby/ruby/blob/trunk/test/psych/test_psych.rb#L272 } end + def test_parse_file_default_fallback + Tempfile.create(['empty', 'yml']) do |t| + assert_equal false, Psych.parse_file(t.path) + end + end + def test_degenerate_strings assert_equal false, Psych.load(' ') assert_equal false, Psych.parse(' ') Index: ext/psych/psych.gemspec =================================================================== --- ext/psych/psych.gemspec (revision 64543) +++ ext/psych/psych.gemspec (revision 64544) @@ -1,9 +1,15 @@ https://github.com/ruby/ruby/blob/trunk/ext/psych/psych.gemspec#L1 # -*- encoding: utf-8 -*- # frozen_string_literal: true +begin + require_relative 'lib/psych/versions' +rescue LoadError + require_relative 'versions' +end + Gem::Specification.new do |s| s.name = "psych" - s.version = "3.0.3.pre1" + s.version = Psych::VERSION s.authors = ["Aaron Patterson", "SHIBATA Hiroshi", "Charles Oliver Nutter"] s.email = ["aaron@t...", "hsbt@r...", "headius@h..."] s.summary = "Psych is a YAML parser and emitter" @@ -53,7 +59,7 @@ DESCRIPTION https://github.com/ruby/ruby/blob/trunk/ext/psych/psych.gemspec#L59 "ext/java/PsychEmitter.java", "ext/java/PsychLibrary.java", "ext/java/PsychParser.java", "ext/java/PsychToRuby.java", "ext/java/PsychYamlTree.java", "lib/psych_jars.rb", "lib/psych.jar" ] - s.requirements = "jar org.yaml:snakeyaml, 1.21" + s.requirements = "jar org.yaml:snakeyaml, #{Psych::DEFAULT_SNAKEYAML_VERSION}" s.add_dependency 'jar-dependencies', '>= 0.1.7' s.add_development_dependency 'ruby-maven' else Index: ext/psych/yaml/dumper.c =================================================================== --- ext/psych/yaml/dumper.c (revision 64543) +++ ext/psych/yaml/dumper.c (revision 64544) @@ -245,9 +245,9 @@ yaml_emitter_anchor_node(yaml_emitter_t https://github.com/ruby/ruby/blob/trunk/ext/psych/yaml/dumper.c#L245 #define ANCHOR_TEMPLATE_LENGTH 16 static yaml_char_t * -yaml_emitter_generate_anchor(yaml_emitter_t *emitter, int anchor_id) +yaml_emitter_generate_anchor(SHIM(yaml_emitter_t *emitter), int anchor_id) { - yaml_char_t *anchor = yaml_malloc(ANCHOR_TEMPLATE_LENGTH); + yaml_char_t *anchor = YAML_MALLOC(ANCHOR_TEMPLATE_LENGTH); if (!anchor) return NULL; Index: ext/psych/yaml/parser.c =================================================================== --- ext/psych/yaml/parser.c (revision 64543) +++ ext/psych/yaml/parser.c (revision 64544) @@ -605,7 +605,7 @@ yaml_parser_parse_node(yaml_parser_t *pa https://github.com/ruby/ruby/blob/trunk/ext/psych/yaml/parser.c#L605 if (strcmp((char *)tag_directive->handle, (char *)tag_handle) == 0) { size_t prefix_len = strlen((char *)tag_directive->prefix); size_t suffix_len = strlen((char *)tag_suffix); - tag = yaml_malloc(prefix_len+suffix_len+1); + tag = YAML_MALLOC(prefix_len+suffix_len+1); if (!tag) { parser->error = YAML_MEMORY_ERROR; goto error; @@ -685,7 +685,7 @@ yaml_parser_parse_node(yaml_parser_t *pa https://github.com/ruby/ruby/blob/trunk/ext/psych/yaml/parser.c#L685 return 1; } else if (anchor || tag) { - yaml_char_t *value = yaml_malloc(1); + yaml_char_t *value = YAML_MALLOC(1); if (!value) { parser->error = YAML_MEMORY_ERROR; goto error; @@ -1208,7 +1208,7 @@ yaml_parser_process_empty_scalar(yaml_pa https://github.com/ruby/ruby/blob/trunk/ext/psych/yaml/parser.c#L1208 { yaml_char_t *value; - value = yaml_malloc(1); + value = YAML_MALLOC(1); if (!value) { parser->error = YAML_MEMORY_ERROR; return 0; @@ -1245,7 +1245,7 @@ yaml_parser_process_directives(yaml_pars https://github.com/ruby/ruby/blob/trunk/ext/psych/yaml/parser.c#L1245 } tag_directives = { NULL, NULL, NULL }; yaml_token_t *token; - if (!STACK_INIT(parser, tag_directives, INITIAL_STACK_SIZE)) + if (!STACK_INIT(parser, tag_directives, yaml_tag_directive_t*)) goto error; token = PEEK_TOKEN(parser); @@ -1266,7 +1266,7 @@ yaml_parser_process_directives(yaml_pars https://github.com/ruby/ruby/blob/trunk/ext/psych/yaml/parser.c#L1266 "found incompatible YAML document", token->start_mark); goto error; } - version_directive = yaml_malloc(sizeof(yaml_version_directive_t)); + version_directive = YAML_MALLOC_STATIC(yaml_version_directive_t); if (!version_directive) { parser->error = YAML_MEMORY_ERROR; goto error; Index: ext/psych/yaml/yaml_private.h =================================================================== --- ext/psych/yaml/yaml_private.h (revision 64543) +++ ext/psych/yaml/yaml_private.h (revision 64544) @@ -12,16 +12,6 @@ https://github.com/ruby/ruby/blob/trunk/ext/psych/yaml/yaml_private.h#L12 #include <limits.h> #include <stddef.h> -#ifndef _MSC_VER -#include <stdint.h> -#else - (... truncated) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/