[前][次][番号順一覧][スレッド一覧]

ruby-changes:68204

From: Jeremy <ko1@a...>
Date: Sat, 2 Oct 2021 23:51:41 +0900 (JST)
Subject: [ruby-changes:68204] 3f7da458a7 (master): Make encoding loading not issue warning

https://git.ruby-lang.org/ruby.git/commit/?id=3f7da458a7

From 3f7da458a77f270d96e6a9f82177d6c90476c34d Mon Sep 17 00:00:00 2001
From: Jeremy Evans <code@j...>
Date: Thu, 23 Sep 2021 12:31:32 -0700
Subject: Make encoding loading not issue warning

Instead of relying on setting an unsetting ruby_verbose, which is
not thread-safe, restructure require_internal and load_lock to
accept a warn argument for whether to warn, and add
rb_require_internal_silent to require without warnings.  Use
rb_require_internal_silent when loading encoding.

Note this does not modify ruby_debug and errinfo handling, those
remain thread-unsafe.

Also silent requires when loading transcoders.
---
 encoding.c  |  7 +++----
 load.c      | 21 ++++++++++++++-------
 transcode.c |  4 +++-
 3 files changed, 20 insertions(+), 12 deletions(-)

diff --git a/encoding.c b/encoding.c
index bc3d2f78f2..a162821e64 100644
--- a/encoding.c
+++ b/encoding.c
@@ -797,11 +797,12 @@ rb_enc_get_from_index(int index) https://github.com/ruby/ruby/blob/trunk/encoding.c#L797
     return must_encindex(index);
 }
 
+int rb_require_internal_silent(VALUE fname);
+
 static int
 load_encoding(const char *name)
 {
     VALUE enclib = rb_sprintf("enc/%s.so", name);
-    VALUE verbose = ruby_verbose;
     VALUE debug = ruby_debug;
     VALUE errinfo;
     char *s = RSTRING_PTR(enclib) + 4, *e = RSTRING_END(enclib) - 3;
@@ -814,11 +815,9 @@ load_encoding(const char *name) https://github.com/ruby/ruby/blob/trunk/encoding.c#L815
 	++s;
     }
     enclib = rb_fstring(enclib);
-    ruby_verbose = Qfalse;
     ruby_debug = Qfalse;
     errinfo = rb_errinfo();
-    loaded = rb_require_internal(enclib);
-    ruby_verbose = verbose;
+    loaded = rb_require_internal_silent(enclib);
     ruby_debug = debug;
     rb_set_errinfo(errinfo);
 
diff --git a/load.c b/load.c
index 28c17e8b9c..ebff4d9da1 100644
--- a/load.c
+++ b/load.c
@@ -767,7 +767,7 @@ rb_f_load(int argc, VALUE *argv, VALUE _) https://github.com/ruby/ruby/blob/trunk/load.c#L767
 }
 
 static char *
-load_lock(const char *ftptr)
+load_lock(const char *ftptr, bool warn)
 {
     st_data_t data;
     st_table *loading_tbl = get_loading_table();
@@ -787,7 +787,7 @@ load_lock(const char *ftptr) https://github.com/ruby/ruby/blob/trunk/load.c#L787
 	(*init)();
 	return (char *)"";
     }
-    if (RTEST(ruby_verbose)) {
+    if (warn) {
 	VALUE warning = rb_warning_string("loading in progress, circular require considered harmful - %s", ftptr);
 	rb_backtrace_each(rb_str_append, warning);
 	rb_warning("%"PRIsVALUE, warning);
@@ -1050,7 +1050,7 @@ rb_ext_ractor_safe(bool flag) https://github.com/ruby/ruby/blob/trunk/load.c#L1050
  * >1: exception
  */
 static int
-require_internal(rb_execution_context_t *ec, VALUE fname, int exception)
+require_internal(rb_execution_context_t *ec, VALUE fname, int exception, bool warn)
 {
     volatile int result = -1;
     rb_thread_t *th = rb_ec_thread_ptr(ec);
@@ -1084,7 +1084,7 @@ require_internal(rb_execution_context_t *ec, VALUE fname, int exception) https://github.com/ruby/ruby/blob/trunk/load.c#L1084
         path = saved_path;
 
 	if (found) {
-            if (!path || !(ftptr = load_lock(RSTRING_PTR(path)))) {
+            if (!path || !(ftptr = load_lock(RSTRING_PTR(path), warn))) {
 		result = 0;
 	    }
 	    else if (!*ftptr) {
@@ -1149,11 +1149,18 @@ require_internal(rb_execution_context_t *ec, VALUE fname, int exception) https://github.com/ruby/ruby/blob/trunk/load.c#L1149
     return result;
 }
 
+int
+rb_require_internal_silent(VALUE fname)
+{
+    rb_execution_context_t *ec = GET_EC();
+    return require_internal(ec, fname, 1, false);
+}
+
 int
 rb_require_internal(VALUE fname)
 {
     rb_execution_context_t *ec = GET_EC();
-    return require_internal(ec, fname, 1);
+    return require_internal(ec, fname, 1, RTEST(ruby_verbose));
 }
 
 int
@@ -1162,7 +1169,7 @@ ruby_require_internal(const char *fname, unsigned int len) https://github.com/ruby/ruby/blob/trunk/load.c#L1169
     struct RString fake;
     VALUE str = rb_setup_fake_str(&fake, fname, len, 0);
     rb_execution_context_t *ec = GET_EC();
-    int result = require_internal(ec, str, 0);
+    int result = require_internal(ec, str, 0, RTEST(ruby_verbose));
     rb_set_errinfo(Qnil);
     return result == TAG_RETURN ? 1 : result ? -1 : 0;
 }
@@ -1171,7 +1178,7 @@ VALUE https://github.com/ruby/ruby/blob/trunk/load.c#L1178
 rb_require_string(VALUE fname)
 {
     rb_execution_context_t *ec = GET_EC();
-    int result = require_internal(ec, fname, 1);
+    int result = require_internal(ec, fname, 1, RTEST(ruby_verbose));
 
     if (result > TAG_RETURN) {
         EC_JUMP_TAG(ec, result);
diff --git a/transcode.c b/transcode.c
index bc985992a1..ec0507ca80 100644
--- a/transcode.c
+++ b/transcode.c
@@ -376,6 +376,8 @@ transcode_search_path(const char *sname, const char *dname, https://github.com/ruby/ruby/blob/trunk/transcode.c#L376
     return pathlen; /* is -1 if not found */
 }
 
+int rb_require_internal_silent(VALUE fname);
+
 static const rb_transcoder *
 load_transcoder_entry(transcoder_entry_t *entry)
 {
@@ -393,7 +395,7 @@ load_transcoder_entry(transcoder_entry_t *entry) https://github.com/ruby/ruby/blob/trunk/transcode.c#L395
         memcpy(path + sizeof(transcoder_lib_prefix) - 1, lib, len);
         rb_str_set_len(fn, total_len);
         OBJ_FREEZE(fn);
-        rb_require_string(fn);
+        rb_require_internal_silent(fn);
     }
 
     if (entry->transcoder)
-- 
cgit v1.2.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]