ruby-changes:50313
From: naruse <ko1@a...>
Date: Fri, 16 Feb 2018 17:34:50 +0900 (JST)
Subject: [ruby-changes:50313] naruse:r62428 (ruby_2_5): merge revision(s) 61616: [Backport #14314]
naruse 2018-02-16 17:34:45 +0900 (Fri, 16 Feb 2018) New Revision: 62428 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=62428 Log: merge revision(s) 61616: [Backport #14314] marshal.c: allow marshalling keyword_init struct struct.c: define rb_struct_s_keyword_init to shared with marshal.c internal.h: add the declaration to be used by marshal.c test/ruby/test_marshal.rb: add test for Bug#14314 [Feature #14314] [ruby-core:84629] Modified directories: branches/ruby_2_5/ Modified files: branches/ruby_2_5/internal.h branches/ruby_2_5/marshal.c branches/ruby_2_5/struct.c branches/ruby_2_5/test/ruby/test_marshal.rb branches/ruby_2_5/version.h Index: ruby_2_5/test/ruby/test_marshal.rb =================================================================== --- ruby_2_5/test/ruby/test_marshal.rb (revision 62427) +++ ruby_2_5/test/ruby/test_marshal.rb (revision 62428) @@ -772,4 +772,11 @@ class TestMarshal < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/ruby_2_5/test/ruby/test_marshal.rb#L772 Marshal.dump(Bug12974.new) end end + + Bug14314 = Struct.new(:foo, keyword_init: true) + + def test_marshal_keyword_init_struct + obj = Bug14314.new(foo: 42) + assert_equal obj, Marshal.load(Marshal.dump(obj)) + end end Index: ruby_2_5/version.h =================================================================== --- ruby_2_5/version.h (revision 62427) +++ ruby_2_5/version.h (revision 62428) @@ -1,6 +1,6 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_5/version.h#L1 #define RUBY_VERSION "2.5.0" #define RUBY_RELEASE_DATE "2018-02-16" -#define RUBY_PATCHLEVEL 18 +#define RUBY_PATCHLEVEL 19 #define RUBY_RELEASE_YEAR 2018 #define RUBY_RELEASE_MONTH 2 Index: ruby_2_5/marshal.c =================================================================== --- ruby_2_5/marshal.c (revision 62427) +++ ruby_2_5/marshal.c (revision 62428) @@ -1811,17 +1811,31 @@ r_object0(struct load_arg *arg, int *ivp https://github.com/ruby/ruby/blob/trunk/ruby_2_5/marshal.c#L1811 arg->readable += (len - 1) * 2; v = r_entry0(v, idx, arg); values = rb_ary_new2(len); - for (i=0; i<len; i++) { - VALUE n = rb_sym2str(RARRAY_AREF(mem, i)); - slot = r_symbol(arg); + { + VALUE keywords; + int keyword_init = RTEST(rb_struct_s_keyword_init(klass)); + if (keyword_init) { + keywords = rb_hash_new(); + rb_ary_push(values, keywords); + } + + for (i=0; i<len; i++) { + VALUE n = rb_sym2str(RARRAY_AREF(mem, i)); + slot = r_symbol(arg); - if (!rb_str_equal(n, slot)) { - rb_raise(rb_eTypeError, "struct %"PRIsVALUE" not compatible (:%"PRIsVALUE" for :%"PRIsVALUE")", - rb_class_name(klass), - slot, n); + if (!rb_str_equal(n, slot)) { + rb_raise(rb_eTypeError, "struct %"PRIsVALUE" not compatible (:%"PRIsVALUE" for :%"PRIsVALUE")", + rb_class_name(klass), + slot, n); + } + if (keyword_init) { + rb_hash_aset(keywords, RARRAY_AREF(mem, i), r_object(arg)); + } + else { + rb_ary_push(values, r_object(arg)); + } + arg->readable -= 2; } - rb_ary_push(values, r_object(arg)); - arg->readable -= 2; } rb_struct_initialize(v, values); v = r_leave(v, arg); Index: ruby_2_5/struct.c =================================================================== --- ruby_2_5/struct.c (revision 62427) +++ ruby_2_5/struct.c (revision 62428) @@ -48,6 +48,12 @@ struct_ivar_get(VALUE c, ID id) https://github.com/ruby/ruby/blob/trunk/ruby_2_5/struct.c#L48 } VALUE +rb_struct_s_keyword_init(VALUE klass) +{ + return struct_ivar_get(klass, id_keyword_init); +} + +VALUE rb_struct_s_members(VALUE klass) { VALUE members = struct_ivar_get(klass, id_members); @@ -298,7 +304,7 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/ruby_2_5/struct.c#L304 rb_struct_s_inspect(VALUE klass) { VALUE inspect = rb_class_name(klass); - if (RTEST(struct_ivar_get(klass, id_keyword_init))) { + if (RTEST(rb_struct_s_keyword_init(klass))) { rb_str_cat_cstr(inspect, "(keyword_init: true)"); } return inspect; @@ -616,7 +622,7 @@ rb_struct_initialize_m(int argc, const V https://github.com/ruby/ruby/blob/trunk/ruby_2_5/struct.c#L622 rb_struct_modify(self); n = num_members(klass); - if (argc > 0 && RTEST(struct_ivar_get(klass, id_keyword_init))) { + if (argc > 0 && RTEST(rb_struct_s_keyword_init(klass))) { struct struct_hash_set_arg arg; if (argc > 2 || !RB_TYPE_P(argv[0], T_HASH)) { rb_raise(rb_eArgError, "wrong number of arguments (given %d, expected 0)", argc); Index: ruby_2_5/internal.h =================================================================== --- ruby_2_5/internal.h (revision 62427) +++ ruby_2_5/internal.h (revision 62428) @@ -1754,6 +1754,7 @@ VALUE rb_to_symbol_type(VALUE obj); https://github.com/ruby/ruby/blob/trunk/ruby_2_5/internal.h#L1754 /* struct.c */ VALUE rb_struct_init_copy(VALUE copy, VALUE s); VALUE rb_struct_lookup(VALUE s, VALUE idx); +VALUE rb_struct_s_keyword_init(VALUE klass); /* time.c */ struct timeval rb_time_timeval(VALUE); Index: ruby_2_5 =================================================================== --- ruby_2_5 (revision 62427) +++ ruby_2_5 (revision 62428) Property changes on: ruby_2_5 ___________________________________________________________________ Modified: svn:mergeinfo ## -0,0 +0,1 ## Merged /trunk:r61616 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/