From a629d14824ade5144dee213ff401e8f8a79d0665 Mon Sep 17 00:00:00 2001 From: Lars Kanis Date: Sun, 28 Jul 2024 12:34:24 +0200 Subject: [PATCH] Don't print a warning when bigdecimal is required on ruby-3.4+ Ruby-3.4+ prints a warning, if bigdecimal is required but not in the Gemfile. But it's a false positive, since we enable bigdecimal depending features only if it's available. And most people don't need these features. Fixes #569 --- ext/pg_text_decoder.c | 2 +- ext/pg_text_encoder.c | 2 +- lib/pg.rb | 10 ++++++++++ lib/pg/basic_type_map_for_queries.rb | 2 +- lib/pg/basic_type_registry.rb | 2 +- spec/helpers.rb | 2 +- 6 files changed, 15 insertions(+), 5 deletions(-) diff --git a/ext/pg_text_decoder.c b/ext/pg_text_decoder.c index f08ad37f4..e789238b5 100644 --- a/ext/pg_text_decoder.c +++ b/ext/pg_text_decoder.c @@ -174,7 +174,7 @@ pg_text_dec_numeric(t_pg_coder *conv, const char *val, int len, int tuple, int f static VALUE init_pg_text_decoder_numeric(VALUE rb_mPG_TextDecoder) { - rb_require("bigdecimal"); + rb_funcall(rb_mPG, rb_intern("require_bigdecimal_without_warning"), 0); s_id_BigDecimal = rb_intern("BigDecimal"); /* dummy = rb_define_class_under( rb_mPG_TextDecoder, "Numeric", rb_cPG_SimpleDecoder ); */ diff --git a/ext/pg_text_encoder.c b/ext/pg_text_encoder.c index 06a0c6382..e15c72e0b 100644 --- a/ext/pg_text_encoder.c +++ b/ext/pg_text_encoder.c @@ -377,7 +377,7 @@ init_pg_text_encoder_numeric(VALUE rb_mPG_TextDecoder) { s_str_F = rb_str_freeze(rb_str_new_cstr("F")); rb_global_variable(&s_str_F); - rb_require("bigdecimal"); + rb_funcall(rb_mPG, rb_intern("require_bigdecimal_without_warning"), 0); s_cBigDecimal = rb_const_get(rb_cObject, rb_intern("BigDecimal")); /* dummy = rb_define_class_under( rb_mPG_TextEncoder, "Numeric", rb_cPG_SimpleEncoder ); */ diff --git a/lib/pg.rb b/lib/pg.rb index 159ff20b6..b6217487f 100644 --- a/lib/pg.rb +++ b/lib/pg.rb @@ -126,4 +126,14 @@ def warn(str, category=nil) Warning.extend(TruffleFixWarn) end + # Ruby-3.4+ prints a warning, if bigdecimal is required but not in the Gemfile. + # But it's a false positive, since we enable bigdecimal depending features only if it's available. + # And most people don't need these features. + def self.require_bigdecimal_without_warning + oldverb, $VERBOSE = $VERBOSE, nil + require "bigdecimal" + ensure + $VERBOSE = oldverb + end + end # module PG diff --git a/lib/pg/basic_type_map_for_queries.rb b/lib/pg/basic_type_map_for_queries.rb index d571387cc..525d6cf2d 100644 --- a/lib/pg/basic_type_map_for_queries.rb +++ b/lib/pg/basic_type_map_for_queries.rb @@ -167,7 +167,7 @@ def get_array_type(value) end begin - require "bigdecimal" + PG.require_bigdecimal_without_warning has_bigdecimal = true rescue LoadError end diff --git a/lib/pg/basic_type_registry.rb b/lib/pg/basic_type_registry.rb index e92a79ce5..a3a164d30 100644 --- a/lib/pg/basic_type_registry.rb +++ b/lib/pg/basic_type_registry.rb @@ -233,7 +233,7 @@ def register_default_types alias_type 0, 'oid', 'int2' begin - require "bigdecimal" + PG.require_bigdecimal_without_warning register_type 0, 'numeric', PG::TextEncoder::Numeric, PG::TextDecoder::Numeric rescue LoadError end diff --git a/spec/helpers.rb b/spec/helpers.rb index c49baecae..2db112e04 100644 --- a/spec/helpers.rb +++ b/spec/helpers.rb @@ -711,7 +711,7 @@ def set_etc_hosts(hostaddr) config.filter_run_excluding( :ipv6 ) if Addrinfo.getaddrinfo("localhost", nil, nil, :STREAM).size < 2 config.filter_run_excluding( :ractor ) unless defined?(Ractor) begin - require "bigdecimal" + PG.require_bigdecimal_without_warning rescue LoadError config.filter_run_excluding( :bigdecimal ) end