Skip to content

Commit

Permalink
Workaround rubygems $LOAD_PATH bug
Browse files Browse the repository at this point in the history
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
  • Loading branch information
byroot committed Oct 25, 2024
1 parent c2cd953 commit a0cd1de
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
9 changes: 9 additions & 0 deletions ext/json/ext/generator/generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,12 @@ static VALUE cState_generate(VALUE self, VALUE obj)
return result;
}

static VALUE cState_initialize(int argc, VALUE *argv, VALUE self)
{
rb_warn("The json gem extension was loaded with the stdlib ruby code. You should upgrade rubygems with `gem update --system`");
return self;
}

/*
* call-seq: initialize_copy(orig)
*
Expand Down Expand Up @@ -1408,6 +1414,9 @@ void Init_generator(void)
cState = rb_define_class_under(mGenerator, "State", rb_cObject);
rb_define_alloc_func(cState, cState_s_allocate);
rb_define_singleton_method(cState, "from_state", cState_from_state_s, 1);
rb_define_method(cState, "initialize", cState_initialize, -1);
rb_define_alias(cState, "initialize", "initialize"); // avoid method redefinition warnings

rb_define_method(cState, "initialize_copy", cState_init_copy, 1);
rb_define_method(cState, "indent", cState_indent, 0);
rb_define_method(cState, "indent=", cState_indent_set, 1);
Expand Down
24 changes: 12 additions & 12 deletions test/json/json_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -261,19 +261,19 @@ def test_buffer_initial_length
end

def test_gc
if respond_to?(:assert_in_out_err) && !(RUBY_PLATFORM =~ /java/)
assert_in_out_err(%w[-rjson -Ilib -Iext], <<-EOS, [], [])
bignum_too_long_to_embed_as_string = 1234567890123456789012345
expect = bignum_too_long_to_embed_as_string.to_s
GC.stress = true
10.times do |i|
tmp = bignum_too_long_to_embed_as_string.to_json
raise "'\#{expect}' is expected, but '\#{tmp}'" unless tmp == expect
end
EOS
pid = fork do
bignum_too_long_to_embed_as_string = 1234567890123456789012345
expect = bignum_too_long_to_embed_as_string.to_s
GC.stress = true

10.times do |i|
tmp = bignum_too_long_to_embed_as_string.to_json
raise "#{expect}' is expected, but '#{tmp}'" unless tmp == expect
end
end
end if GC.respond_to?(:stress=)
_, status = Process.waitpid2(pid)
assert_predicate status, :success?
end if GC.respond_to?(:stress=) && Process.respond_to?(:fork)

def test_configure_using_configure_and_merge
numbered_state = {
Expand Down

0 comments on commit a0cd1de

Please sign in to comment.