Skip to content

Commit ad156f7

Browse files
jeremyevansnobu
authored andcommitted
[ruby/date] Fix cannot load complex into simple error when loading marshal dump (Fixes #20)
This problem exists because Marshal.load calls Date.allocate, which uses a SimpleDateData. There doesn't seem to be any support for taking an existing Date instance and converting it from SimpleDateData to ComplexDateData. Work around this issue by making Date.allocate use a ComplexDateData. This causes problems in Date#initialize, so remove the Date#initialize method (keeping the date_initialize function, used internally for Date.civil). Alias Date.new to Date.civil, since they do the same thing. ruby/date@6bb8d8fa0f
1 parent d1af234 commit ad156f7

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

ext/date/date_core.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9318,7 +9318,7 @@ Init_date_core(void)
93189318
*/
93199319
rb_define_const(cDate, "GREGORIAN", DBL2NUM(GREGORIAN));
93209320

9321-
rb_define_alloc_func(cDate, d_lite_s_alloc_simple);
9321+
rb_define_alloc_func(cDate, d_lite_s_alloc_complex);
93229322

93239323
#ifndef NDEBUG
93249324
rb_define_private_method(CLASS_OF(cDate), "_valid_jd?",
@@ -9368,6 +9368,7 @@ Init_date_core(void)
93689368
rb_define_singleton_method(cDate, "jd", date_s_jd, -1);
93699369
rb_define_singleton_method(cDate, "ordinal", date_s_ordinal, -1);
93709370
rb_define_singleton_method(cDate, "civil", date_s_civil, -1);
9371+
rb_define_singleton_method(cDate, "new", date_s_civil, -1);
93719372
rb_define_singleton_method(cDate, "commercial", date_s_commercial, -1);
93729373

93739374
#ifndef NDEBUG
@@ -9395,7 +9396,6 @@ Init_date_core(void)
93959396
rb_define_singleton_method(cDate, "_jisx0301", date_s__jisx0301, 1);
93969397
rb_define_singleton_method(cDate, "jisx0301", date_s_jisx0301, -1);
93979398

9398-
rb_define_method(cDate, "initialize", date_initialize, -1);
93999399
rb_define_method(cDate, "initialize_copy", d_lite_initialize_copy, 1);
94009400

94019401
#ifndef NDEBUG

test/date/test_date_marshal.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ def test_marshal
3939
assert(d.frozen?)
4040
expected_error = defined?(FrozenError) ? FrozenError : RuntimeError
4141
assert_raise(expected_error){d.marshal_load(a)}
42+
43+
d = Date.new + 1/2r + 2304/65437r/86400
44+
m = Marshal.dump(d)
45+
d2 = Marshal.load(m)
46+
assert_equal(d, d2)
47+
assert_equal(d.start, d2.start)
48+
assert_instance_of(String, d2.to_s)
4249
end
4350

4451
def test_memsize

0 commit comments

Comments
 (0)