Skip to content

Commit 1f5e849

Browse files
committed
Workaround rubygems $LOAD_PATH bug
Ref: #647 Ref: rubygems/rubygems#6490 Older rubygems are executing `extconf.rb` with a broken `$LOAD_PATH` causing the `json` gem native extension to be loaded with the stdlib version of the `.rb` files. This fails with ``` json/common.rb:82:in `initialize': wrong number of arguments (given 1, expected 0) (ArgumentError) ``` Since this is just for `extconf.rb` we can probably just accept that extra argument and ignore it. The bug was fixed in rubygems 3.4.9 / 2023-03-20
1 parent 49de571 commit 1f5e849

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

ext/json/ext/generator/generator.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -977,6 +977,12 @@ static VALUE cState_generate(VALUE self, VALUE obj)
977977
return result;
978978
}
979979

980+
static VALUE cState_initialize(int argc, VALUE *argv, VALUE self)
981+
{
982+
rb_warn("The json gem extension was loaded with the stdlib ruby code. You should upgrade rubygems with `gem update --system`");
983+
return self;
984+
}
985+
980986
/*
981987
* call-seq: initialize_copy(orig)
982988
*
@@ -1422,6 +1428,9 @@ void Init_generator(void)
14221428
cState = rb_define_class_under(mGenerator, "State", rb_cObject);
14231429
rb_define_alloc_func(cState, cState_s_allocate);
14241430
rb_define_singleton_method(cState, "from_state", cState_from_state_s, 1);
1431+
rb_define_method(cState, "initialize", cState_initialize, -1);
1432+
rb_define_alias(cState, "initialize", "initialize"); // avoid method redefinition warnings
1433+
14251434
rb_define_method(cState, "initialize_copy", cState_init_copy, 1);
14261435
rb_define_method(cState, "indent", cState_indent, 0);
14271436
rb_define_method(cState, "indent=", cState_indent_set, 1);

test/json/json_generator_test.rb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -268,19 +268,19 @@ def test_buffer_initial_length
268268
end
269269

270270
def test_gc
271-
if respond_to?(:assert_in_out_err) && !(RUBY_PLATFORM =~ /java/)
272-
assert_in_out_err(%w[-rjson -Ilib -Iext], <<-EOS, [], [])
273-
bignum_too_long_to_embed_as_string = 1234567890123456789012345
274-
expect = bignum_too_long_to_embed_as_string.to_s
275-
GC.stress = true
276-
277-
10.times do |i|
278-
tmp = bignum_too_long_to_embed_as_string.to_json
279-
raise "'\#{expect}' is expected, but '\#{tmp}'" unless tmp == expect
280-
end
281-
EOS
271+
pid = fork do
272+
bignum_too_long_to_embed_as_string = 1234567890123456789012345
273+
expect = bignum_too_long_to_embed_as_string.to_s
274+
GC.stress = true
275+
276+
10.times do |i|
277+
tmp = bignum_too_long_to_embed_as_string.to_json
278+
raise "#{expect}' is expected, but '#{tmp}'" unless tmp == expect
279+
end
282280
end
283-
end if GC.respond_to?(:stress=)
281+
_, status = Process.waitpid2(pid)
282+
assert_predicate status, :success?
283+
end if GC.respond_to?(:stress=) && Process.respond_to?(:fork)
284284

285285
def test_configure_using_configure_and_merge
286286
numbered_state = {

0 commit comments

Comments
 (0)