From 77861ff281b67c9c2a689bce2332c26f3736cd3f Mon Sep 17 00:00:00 2001 From: Charles Oliver Nutter Date: Fri, 13 Jan 2023 19:24:25 -0600 Subject: Remove deleted file from gemspec --- psych.gemspec | 1 - 1 file changed, 1 deletion(-) diff --git a/psych.gemspec b/psych.gemspec index a5dfc76..f826af0 100644 --- a/psych.gemspec +++ b/psych.gemspec @@ -52,7 +52,6 @@ DESCRIPTION "ext/java/org/jruby/ext/psych/PsychLibrary.java", "ext/java/org/jruby/ext/psych/PsychParser.java", "ext/java/org/jruby/ext/psych/PsychToRuby.java", - "ext/java/org/jruby/ext/psych/PsychYamlTree.java", "lib/psych_jars.rb", "lib/psych.jar" ] -- cgit v1.2.1 From ad1502202c72fe01aefe1035b392e0363c43cb6f Mon Sep 17 00:00:00 2001 From: Hiroshi SHIBATA Date: Tue, 17 Jan 2023 17:11:34 +0900 Subject: configure of libyaml couldn't detect "arm64-apple-darwin22" for build host. checking whether make sets $(MAKE)... (cached) yes checking build system type... arm-apple-darwin21.6.0 checking host system type... Invalid configuration `arm64-apple-darwin21': machine `arm64-apple' not recognized configure: error: /bin/sh /Users/hsbt/Downloads/yaml-0.2.5/config/config.sub arm64-apple-darwin21 failed *** extconf.rb failed *** --- ext/psych/extconf.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/psych/extconf.rb b/ext/psych/extconf.rb index 41daf8c..e7dd0bb 100644 --- a/ext/psych/extconf.rb +++ b/ext/psych/extconf.rb @@ -22,7 +22,7 @@ if yaml_source args = [ yaml_configure, "--enable-#{shared ? 'shared' : 'static'}", - "--host=#{RbConfig::CONFIG['host'].sub(/-unknown-/, '-')}", + "--host=#{RbConfig::CONFIG['host'].sub(/-unknown-/, '-').sub(/arm64/, 'arm')}", "CC=#{RbConfig::CONFIG['CC']}", *(["CFLAGS=-w"] if RbConfig::CONFIG["GCC"] == "yes"), ] -- cgit v1.2.1 From 67ec299c6806fe3def1c92664597b511d54f49b8 Mon Sep 17 00:00:00 2001 From: Hiroshi SHIBATA Date: Tue, 17 Jan 2023 17:27:20 +0900 Subject: Bump up 5.0.2.pre1 for testing --- lib/psych/versions.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/psych/versions.rb b/lib/psych/versions.rb index a592a69..9c563af 100644 --- a/lib/psych/versions.rb +++ b/lib/psych/versions.rb @@ -2,7 +2,7 @@ module Psych # The version of Psych you are using - VERSION = '5.0.1' + VERSION = '5.0.2.pre1' if RUBY_ENGINE == 'jruby' DEFAULT_SNAKEYAML_VERSION = '1.33'.freeze -- cgit v1.2.1 From a170b8eb466f25d16c4ceb1451a75de1d2c8c0cc Mon Sep 17 00:00:00 2001 From: Hiroshi SHIBATA Date: Wed, 18 Jan 2023 13:46:39 +0900 Subject: Bump up 5.0.2 --- lib/psych/versions.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/psych/versions.rb b/lib/psych/versions.rb index 9c563af..22b9094 100644 --- a/lib/psych/versions.rb +++ b/lib/psych/versions.rb @@ -2,7 +2,7 @@ module Psych # The version of Psych you are using - VERSION = '5.0.2.pre1' + VERSION = '5.0.2' if RUBY_ENGINE == 'jruby' DEFAULT_SNAKEYAML_VERSION = '1.33'.freeze -- cgit v1.2.1 From f44269fc9b25090b29be6c506505118797805aad Mon Sep 17 00:00:00 2001 From: Charles Oliver Nutter Date: Wed, 18 Jan 2023 11:32:47 -0600 Subject: Update for stricter 1.2 syntax This allows these tests to pass on SnakeYAML Engine -- which is a 1.2-only YAML library -- while still passing on libyaml 1.1. --- test/psych/test_yaml.rb | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/test/psych/test_yaml.rb b/test/psych/test_yaml.rb index 32dd43b..cedec46 100644 --- a/test/psych/test_yaml.rb +++ b/test/psych/test_yaml.rb @@ -223,8 +223,8 @@ EOY &C currency: GBP &D departure: LAX &A arrival: EDI - - { *F: MADF, *C: AUD, *D: SYD, *A: MEL } - - { *F: DFSF, *C: USD, *D: JFK, *A: MCO } + - { *F : MADF, *C : AUD, *D : SYD, *A : MEL } + - { *F : DFSF, *C : USD, *D : JFK, *A : MCO } EOY ) @@ -233,20 +233,20 @@ EOY --- ALIASES: [&f fareref, &c currency, &d departure, &a arrival] FARES: -- *f: DOGMA - *c: GBP - *d: LAX - *a: EDI - -- *f: MADF - *c: AUD - *d: SYD - *a: MEL - -- *f: DFSF - *c: USD - *d: JFK - *a: MCO +- *f : DOGMA + *c : GBP + *d : LAX + *a : EDI + +- *f : MADF + *c : AUD + *d : SYD + *a : MEL + +- *f : DFSF + *c : USD + *d : JFK + *a : MCO EOY ) -- cgit v1.2.1 From 69b109924a093575fd787ae46ae234c222a1d378 Mon Sep 17 00:00:00 2001 From: Charles Oliver Nutter Date: Wed, 18 Jan 2023 22:32:48 -0600 Subject: Reduce hops to call sites If we could embed indy call sites here they would cache as constants; this is the best we can do at the moment. --- ext/java/org/jruby/ext/psych/PsychParser.java | 38 ++++++++++++++++++--------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/ext/java/org/jruby/ext/psych/PsychParser.java b/ext/java/org/jruby/ext/psych/PsychParser.java index fd5e19c..85dfdec 100644 --- a/ext/java/org/jruby/ext/psych/PsychParser.java +++ b/ext/java/org/jruby/ext/psych/PsychParser.java @@ -116,7 +116,19 @@ public class PsychParser extends RubyObject { public PsychParser(Ruby runtime, RubyClass klass) { super(runtime, klass); - this.sites = (CachingCallSite[]) klass.getInternalVariable(JRUBY_CALL_SITES); + CachingCallSite[] sites = (CachingCallSite[]) klass.getInternalVariable(JRUBY_CALL_SITES); + this.path = sites[Call.path.ordinal()]; + this.event_location = sites[Call.event_location.ordinal()]; + this.start_stream = sites[Call.start_stream.ordinal()]; + this.start_document = sites[Call.start_document.ordinal()]; + this.end_document = sites[Call.end_document.ordinal()]; + this.alias = sites[Call.alias.ordinal()]; + this.scalar = sites[Call.scalar.ordinal()]; + this.start_sequence = sites[Call.start_sequence.ordinal()]; + this.end_sequence = sites[Call.end_sequence.ordinal()]; + this.start_mapping = sites[Call.start_mapping.ordinal()]; + this.end_mapping = sites[Call.end_mapping.ordinal()]; + this.end_stream = sites[Call.end_stream.ordinal()]; this.loadSettingsBuilder = LoadSettings.builder().setSchema(new CoreSchema()); } @@ -216,7 +228,7 @@ public class PsychParser extends RubyObject { parser = new ParserImpl(loadSettings, new ScannerImpl(loadSettings, readerFor(context, yaml, loadSettings))); if (path.isNil() && yaml.respondsTo("path")) { - path = sites[Call.path.ordinal()].call(context, this, yaml); + path = this.path.call(context, this, yaml); } while (parser.hasNext()) { @@ -230,11 +242,11 @@ public class PsychParser extends RubyObject { IRubyObject end_line = runtime.newFixnum(end.getLine()); IRubyObject end_column = runtime.newFixnum(end.getColumn()); - sites[Call.event_location.ordinal()].call(context, this, handler, start_line, start_column, end_line, end_column); + event_location.call(context, this, handler, start_line, start_column, end_line, end_column); switch (event.getEventId()) { case StreamStart: - sites[Call.start_stream.ordinal()].call(context, this, handler, runtime.newFixnum(YAML_ANY_ENCODING.ordinal())); + start_stream.call(context, this, handler, runtime.newFixnum(YAML_ANY_ENCODING.ordinal())); break; case DocumentStart: handleDocumentStart(context, (DocumentStartEvent) event, handler); @@ -242,12 +254,12 @@ public class PsychParser extends RubyObject { case DocumentEnd: IRubyObject notExplicit = runtime.newBoolean(!((DocumentEndEvent) event).isExplicit()); - sites[Call.end_document.ordinal()].call(context, this, handler, notExplicit); + end_document.call(context, this, handler, notExplicit); break; case Alias: IRubyObject alias = stringOrNilForAnchor(context, ((AliasEvent) event).getAnchor()); - sites[Call.alias.ordinal()].call(context, this, handler, alias); + this.alias.call(context, this, handler, alias); break; case Scalar: handleScalar(context, (ScalarEvent) event, handler); @@ -256,16 +268,16 @@ public class PsychParser extends RubyObject { handleSequenceStart(context, (SequenceStartEvent) event, handler); break; case SequenceEnd: - sites[Call.end_sequence.ordinal()].call(context, this, handler); + end_sequence.call(context, this, handler); break; case MappingStart: handleMappingStart(context, (MappingStartEvent) event, handler); break; case MappingEnd: - sites[Call.end_mapping.ordinal()].call(context, this, handler); + end_mapping.call(context, this, handler); break; case StreamEnd: - sites[Call.end_stream.ordinal()].call(context, this, handler); + end_stream.call(context, this, handler); break; } } @@ -333,7 +345,7 @@ public class PsychParser extends RubyObject { IRubyObject implicit = runtime.newBoolean(mse.isImplicit()); IRubyObject style = runtime.newFixnum(translateFlowStyle(mse.getFlowStyle())); - sites[Call.start_mapping.ordinal()].call(context, this, handler, anchor, tag, implicit, style); + start_mapping.call(context, this, handler, anchor, tag, implicit, style); } private void handleScalar(ThreadContext context, ScalarEvent se, IRubyObject handler) { @@ -346,7 +358,7 @@ public class PsychParser extends RubyObject { IRubyObject style = runtime.newFixnum(translateStyle(se.getScalarStyle())); IRubyObject val = stringFor(context, se.getValue()); - sites[Call.scalar.ordinal()].call(context, this, handler, val, anchor, tag, plain_implicit, + scalar.call(context, this, handler, val, anchor, tag, plain_implicit, quoted_implicit, style); } @@ -357,7 +369,7 @@ public class PsychParser extends RubyObject { IRubyObject implicit = runtime.newBoolean(sse.isImplicit()); IRubyObject style = runtime.newFixnum(translateFlowStyle(sse.getFlowStyle())); - sites[Call.start_sequence.ordinal()].call(context, this, handler, anchor, tag, implicit, style); + start_sequence.call(context, this, handler, anchor, tag, implicit, style); } private static void raiseParserException(ThreadContext context, ReaderException re, IRubyObject rbPath) { @@ -500,5 +512,5 @@ public class PsychParser extends RubyObject { path, event_location, start_stream, start_document, end_document, alias, scalar, start_sequence, end_sequence, start_mapping, end_mapping, end_stream } - final CachingCallSite[] sites; + private final CachingCallSite path, event_location, start_stream, start_document, end_document, alias, scalar, start_sequence, end_sequence, start_mapping, end_mapping, end_stream; } -- cgit v1.2.1 From b8a09c1cdf192061dbf6e256d91941ecf9e0ac99 Mon Sep 17 00:00:00 2001 From: Charles Oliver Nutter Date: Wed, 18 Jan 2023 23:04:52 -0600 Subject: Misc cleanup --- ext/java/org/jruby/ext/psych/PsychParser.java | 76 ++++++++++++++++----------- 1 file changed, 45 insertions(+), 31 deletions(-) diff --git a/ext/java/org/jruby/ext/psych/PsychParser.java b/ext/java/org/jruby/ext/psych/PsychParser.java index 85dfdec..0975f46 100644 --- a/ext/java/org/jruby/ext/psych/PsychParser.java +++ b/ext/java/org/jruby/ext/psych/PsychParser.java @@ -62,6 +62,7 @@ import org.snakeyaml.engine.v2.events.AliasEvent; import org.snakeyaml.engine.v2.events.DocumentEndEvent; import org.snakeyaml.engine.v2.events.DocumentStartEvent; import org.snakeyaml.engine.v2.events.Event; +import org.snakeyaml.engine.v2.events.ImplicitTuple; import org.snakeyaml.engine.v2.events.MappingStartEvent; import org.snakeyaml.engine.v2.events.ScalarEvent; import org.snakeyaml.engine.v2.events.SequenceStartEvent; @@ -147,17 +148,26 @@ public class PsychParser extends RubyObject { private IRubyObject stringFor(ThreadContext context, String value) { Ruby runtime = context.runtime; + boolean isUTF8 = true; + Charset charset = RubyEncoding.UTF8; + Encoding encoding = runtime.getDefaultInternalEncoding(); if (encoding == null) { encoding = UTF8Encoding.INSTANCE; + charset = RubyEncoding.UTF8; + } else { + Charset encodingCharset = encoding.getCharset(); + if (encodingCharset != null) { + isUTF8 = encodingCharset == RubyEncoding.UTF8; + charset = encodingCharset; + } } - Charset charset = RubyEncoding.UTF8; - if (encoding.getCharset() != null) { - charset = encoding.getCharset(); - } - - ByteList bytes = new ByteList(value.getBytes(charset), encoding); + ByteList bytes = new ByteList( + isUTF8 ? + RubyEncoding.encodeUTF8(value) : + RubyEncoding.encode(value, charset), + encoding); RubyString string = RubyString.newString(runtime, bytes); return string; @@ -324,18 +334,23 @@ public class PsychParser extends RubyObject { RubyArray.newEmptyArray(runtime); Map tagsMap = dse.getTags(); - RubyArray tags = RubyArray.newArray(runtime); - if (tagsMap != null && tagsMap.size() > 0) { + RubyArray tags; + int size; + if (tagsMap != null && (size = tagsMap.size()) > 0) { + tags = RubyArray.newArray(runtime, size); for (Map.Entry tag : tagsMap.entrySet()) { - IRubyObject key = stringFor(context, tag.getKey()); + IRubyObject key = stringFor(context, tag.getKey()); IRubyObject value = stringFor(context, tag.getValue()); tags.append(RubyArray.newArray(runtime, key, value)); } + } else { + tags = RubyArray.newEmptyArray(runtime); } + IRubyObject notExplicit = runtime.newBoolean(!dse.isExplicit()); - invoke(context, handler, "start_document", version, tags, notExplicit); + start_document.call(context, this, handler, version, tags, notExplicit); } private void handleMappingStart(ThreadContext context, MappingStartEvent mse, IRubyObject handler) { @@ -353,8 +368,9 @@ public class PsychParser extends RubyObject { IRubyObject anchor = stringOrNilForAnchor(context, se.getAnchor()); IRubyObject tag = stringOrNilFor(context, se.getTag()); - IRubyObject plain_implicit = runtime.newBoolean(se.getImplicit().canOmitTagInPlainScalar()); - IRubyObject quoted_implicit = runtime.newBoolean(se.getImplicit().canOmitTagInNonPlainScalar()); + ImplicitTuple implicit = se.getImplicit(); + IRubyObject plain_implicit = runtime.newBoolean(implicit.canOmitTagInPlainScalar()); + IRubyObject quoted_implicit = runtime.newBoolean(implicit.canOmitTagInNonPlainScalar()); IRubyObject style = runtime.newFixnum(translateStyle(se.getScalarStyle())); IRubyObject val = stringFor(context, se.getValue()); @@ -373,21 +389,20 @@ public class PsychParser extends RubyObject { } private static void raiseParserException(ThreadContext context, ReaderException re, IRubyObject rbPath) { - Ruby runtime; + Ruby runtime = context.runtime; RubyClass se; IRubyObject exception; - runtime = context.runtime; - se = (RubyClass)runtime.getModule("Psych").getConstant("SyntaxError"); + se = (RubyClass) runtime.getModule("Psych").getConstant("SyntaxError"); exception = se.newInstance(context, new IRubyObject[] { rbPath, - runtime.newFixnum(0), - runtime.newFixnum(0), + RubyFixnum.zero(runtime), + RubyFixnum.zero(runtime), runtime.newFixnum(re.getPosition()), - (null == re.getName() ? runtime.getNil() : runtime.newString(re.getName())), - (null == re.toString() ? runtime.getNil() : runtime.newString(re.toString())) + (null == re.getName() ? context.nil : runtime.newString(re.getName())), + (null == re.toString() ? context.nil : runtime.newString(re.toString())) }, Block.NULL_BLOCK); @@ -395,12 +410,11 @@ public class PsychParser extends RubyObject { } private static void raiseParserException(ThreadContext context, MarkedYamlEngineException mye, IRubyObject rbPath) { - Ruby runtime; + Ruby runtime = context.runtime; Mark mark; RubyClass se; IRubyObject exception; - runtime = context.runtime; se = (RubyClass)runtime.getModule("Psych").getConstant("SyntaxError"); mark = mye.getProblemMark().get(); @@ -411,8 +425,8 @@ public class PsychParser extends RubyObject { runtime.newFixnum(mark.getLine() + 1), runtime.newFixnum(mark.getColumn() + 1), runtime.newFixnum(mark.getIndex()), - (null == mye.getProblem() ? runtime.getNil() : runtime.newString(mye.getProblem())), - (null == mye.getContext() ? runtime.getNil() : runtime.newString(mye.getContext())) + (null == mye.getProblem() ? context.nil : runtime.newString(mye.getProblem())), + (null == mye.getContext() ? context.nil : runtime.newString(mye.getContext())) }, Block.NULL_BLOCK); @@ -420,11 +434,10 @@ public class PsychParser extends RubyObject { } private static void raiseParserException(ThreadContext context, MalformedInputException mie, IRubyObject rbPath) { - Ruby runtime;; + Ruby runtime = context.runtime; RubyClass se; IRubyObject exception; - runtime = context.runtime; se = (RubyClass)runtime.getModule("Psych").getConstant("SyntaxError"); mie.getInputLength(); @@ -432,11 +445,11 @@ public class PsychParser extends RubyObject { exception = se.newInstance(context, arrayOf( rbPath, - runtime.newFixnum(-1), - runtime.newFixnum(-1), + RubyFixnum.minus_one(runtime), + RubyFixnum.minus_one(runtime), runtime.newFixnum(mie.getInputLength()), - runtime.getNil(), - runtime.getNil() + context.nil, + context.nil ), Block.NULL_BLOCK); @@ -471,6 +484,7 @@ public class PsychParser extends RubyObject { Event event = null; + Parser parser = this.parser; if (parser != null) { if (parser.hasNext()) { event = parser.peekEvent(); @@ -480,7 +494,7 @@ public class PsychParser extends RubyObject { } if (event == null) { - return ((RubyClass)context.runtime.getClassFromPath("Psych::Parser::Mark")).newInstance( + return ((RubyClass) runtime.getClassFromPath("Psych::Parser::Mark")).newInstance( context, RubyFixnum.zero(runtime), RubyFixnum.zero(runtime), @@ -491,7 +505,7 @@ public class PsychParser extends RubyObject { Mark mark = event.getStartMark().orElseThrow(RuntimeException::new); - return ((RubyClass)context.runtime.getClassFromPath("Psych::Parser::Mark")).newInstance( + return ((RubyClass) runtime.getClassFromPath("Psych::Parser::Mark")).newInstance( context, RubyFixnum.zero(runtime), runtime.newFixnum(mark.getLine()), -- cgit v1.2.1 From c662354eecbae162b9287b8b566b432421414f96 Mon Sep 17 00:00:00 2001 From: Charles Oliver Nutter Date: Wed, 18 Jan 2023 23:14:37 -0600 Subject: Shorter path to encode strings in emitter --- ext/java/org/jruby/ext/psych/PsychEmitter.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ext/java/org/jruby/ext/psych/PsychEmitter.java b/ext/java/org/jruby/ext/psych/PsychEmitter.java index 9180dcc..baaca4a 100644 --- a/ext/java/org/jruby/ext/psych/PsychEmitter.java +++ b/ext/java/org/jruby/ext/psych/PsychEmitter.java @@ -33,12 +33,14 @@ import org.jruby.Ruby; import org.jruby.RubyArray; import org.jruby.RubyBoolean; import org.jruby.RubyClass; +import org.jruby.RubyEncoding; import org.jruby.RubyModule; import org.jruby.RubyObject; import org.jruby.RubyString; import org.jruby.anno.JRubyMethod; import org.jruby.runtime.ThreadContext; import org.jruby.runtime.builtin.IRubyObject; +import org.jruby.util.ByteList; import org.jruby.util.IOOutputStream; import org.jruby.util.TypeConverter; import org.jruby.util.io.EncodingUtils; @@ -375,8 +377,9 @@ public class PsychEmitter extends RubyObject { TypeConverter.checkType(context, tag, stringClass); tagStr = (RubyString) tag; + ByteList bytes = tagStr.getByteList(); - return EncodingUtils.strConvEnc(context, tagStr, tagStr.getEncoding(), UTF8Encoding.INSTANCE).asJavaString(); + return RubyEncoding.decodeUTF8(bytes.unsafeBytes(), bytes.begin(), bytes.realSize()); } Emitter emitter; -- cgit v1.2.1 From 4bf6dac18021e1928984fe1533a88d5a65860405 Mon Sep 17 00:00:00 2001 From: Charles Oliver Nutter Date: Wed, 18 Jan 2023 23:23:07 -0600 Subject: Minor tweaks and cleanup in emitter --- ext/java/org/jruby/ext/psych/PsychEmitter.java | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/ext/java/org/jruby/ext/psych/PsychEmitter.java b/ext/java/org/jruby/ext/psych/PsychEmitter.java index baaca4a..4df7989 100644 --- a/ext/java/org/jruby/ext/psych/PsychEmitter.java +++ b/ext/java/org/jruby/ext/psych/PsychEmitter.java @@ -118,17 +118,14 @@ public class PsychEmitter extends RubyObject { initEmitter(context, encoding); - StreamStartEvent event = new StreamStartEvent(NULL_MARK, NULL_MARK); - - emit(context, event); + emit(context, NULL_STREAM_START_EVENT); return this; } @JRubyMethod public IRubyObject end_stream(ThreadContext context) { - StreamEndEvent event = new StreamEndEvent(NULL_MARK, NULL_MARK); - emit(context, event); + emit(context, NULL_STREAM_START_EVENT); return this; } @@ -368,16 +365,16 @@ public class PsychEmitter extends RubyObject { return dumpSettingsBuilder.build(); } - private String exportToUTF8(ThreadContext context, IRubyObject tag, RubyClass stringClass) { - if (tag.isNil()) { + private String exportToUTF8(ThreadContext context, IRubyObject maybeString, RubyClass stringClass) { + if (maybeString.isNil()) { return null; } - RubyString tagStr; + RubyString string; - TypeConverter.checkType(context, tag, stringClass); - tagStr = (RubyString) tag; - ByteList bytes = tagStr.getByteList(); + TypeConverter.checkType(context, maybeString, stringClass); + string = (RubyString) maybeString; + ByteList bytes = string.getByteList(); return RubyEncoding.decodeUTF8(bytes.unsafeBytes(), bytes.begin(), bytes.realSize()); } @@ -388,6 +385,7 @@ public class PsychEmitter extends RubyObject { IRubyObject io; private static final Optional NULL_MARK = Optional.empty(); + private static final StreamStartEvent NULL_STREAM_START_EVENT = new StreamStartEvent(NULL_MARK, NULL_MARK); // Map style constants from Psych values (ANY = 0 ... FOLDED = 5) // to SnakeYaml values; see psych/nodes/scalar.rb. -- cgit v1.2.1 From 592a75a656a8575ae1791eb899f5bd5eb4f20352 Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Sun, 22 Jan 2023 09:04:11 -0500 Subject: Fix RestrictedYAMLTree allowing the Symbol class should allow all symbols Ref: https://github.com/ruby/psych/pull/495 That's how it works for `safe_load`: ```ruby >> YAML.safe_load(':foo', permitted_classes: [Symbol]) => :foo ``` So `safe_dump` should mirror that. --- lib/psych/visitors/yaml_tree.rb | 4 ++-- test/psych/test_psych.rb | 7 +++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/psych/visitors/yaml_tree.rb b/lib/psych/visitors/yaml_tree.rb index 3185879..5149178 100644 --- a/lib/psych/visitors/yaml_tree.rb +++ b/lib/psych/visitors/yaml_tree.rb @@ -568,7 +568,7 @@ module Psych raise BadAlias, "Tried to dump an aliased object" end - unless @permitted_classes[target.class] + unless Symbol === target || @permitted_classes[target.class] raise DisallowedClass.new('dump', target.class.name || target.class.inspect) end @@ -576,7 +576,7 @@ module Psych end def visit_Symbol sym - unless @permitted_symbols[sym] + unless @permitted_classes[Symbol] || @permitted_symbols[sym] raise DisallowedClass.new('dump', "Symbol(#{sym.inspect})") end diff --git a/test/psych/test_psych.rb b/test/psych/test_psych.rb index 1abd69c..c977e79 100644 --- a/test/psych/test_psych.rb +++ b/test/psych/test_psych.rb @@ -419,12 +419,15 @@ eoyml end def test_safe_dump_symbols + assert_equal Psych.dump(:foo), Psych.safe_dump(:foo, permitted_classes: [Symbol]) + assert_equal Psych.dump(:foo), Psych.safe_dump(:foo, permitted_symbols: [:foo]) + error = assert_raise Psych::DisallowedClass do - Psych.safe_dump(:foo, permitted_classes: [Symbol]) + Psych.safe_dump(:foo) end assert_equal "Tried to dump unspecified class: Symbol(:foo)", error.message - assert_match(/\A--- :foo\n(?:\.\.\.\n)?\z/, Psych.safe_dump(:foo, permitted_classes: [Symbol], permitted_symbols: [:foo])) + assert_match(/\A--- :foo\n(?:\.\.\.\n)?\z/, Psych.safe_dump(:foo, permitted_symbols: [:foo])) end def test_safe_dump_aliases -- cgit v1.2.1