diff --git a/ext/psych/lib/psych/visitors/to_ruby.rb b/ext/psych/lib/psych/visitors/to_ruby.rb index 74a52df866..c265acb819 100644 --- a/ext/psych/lib/psych/visitors/to_ruby.rb +++ b/ext/psych/lib/psych/visitors/to_ruby.rb @@ -252,6 +252,8 @@ module Psych e = build_exception((resolve_class($1) || class_loader.exception), h.delete('message')) + + e.set_backtrace h.delete('backtrace') if h.key? 'backtrace' init_with(e, h, o) when '!set', 'tag:yaml.org,2002:set' diff --git a/ext/psych/lib/psych/visitors/yaml_tree.rb b/ext/psych/lib/psych/visitors/yaml_tree.rb index bc7d2352e4..62d1d5c3f4 100644 --- a/ext/psych/lib/psych/visitors/yaml_tree.rb +++ b/ext/psych/lib/psych/visitors/yaml_tree.rb @@ -472,6 +472,9 @@ module Psych accept msg end + @emitter.scalar 'backtrace', nil, nil, true, false, Nodes::Scalar::ANY + accept o.backtrace + dump_ivars o @emitter.end_mapping diff --git a/test/psych/test_exception.rb b/test/psych/test_exception.rb index df7fd73740..e7fc88c706 100644 --- a/test/psych/test_exception.rb +++ b/test/psych/test_exception.rb @@ -23,6 +23,20 @@ module Psych $VERBOSE = @orig_verbose end + def make_ex msg = 'oh no!' + begin + raise msg + rescue ::Exception => e + e + end + end + + def test_backtrace + err = make_ex + new_err = Psych.load(Psych.dump(err)) + assert_equal err.backtrace, new_err.backtrace + end + def test_naming_exception err = String.xxx rescue $! new_err = Psych.load(Psych.dump(err))