From a58611dfb1bfc74fb1a51a9cd0ca8ac690c2f1f4 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Tue, 24 May 2022 16:48:34 +0900 Subject: [PATCH] Allow to just warn as bool expected, without an exception --- dir.c | 2 +- error.c | 2 +- internal/object.h | 2 +- io.c | 4 ++-- object.c | 24 +++++++++++++++--------- process.c | 2 +- 6 files changed, 21 insertions(+), 15 deletions(-) diff --git a/dir.c b/dir.c index a090b9f38c..ee58d9c656 100644 --- a/dir.c +++ b/dir.c @@ -2948,7 +2948,7 @@ dir_glob_option_base(VALUE base) static int dir_glob_option_sort(VALUE sort) { - return (rb_bool_expected(sort, "sort") ? 0 : FNM_GLOB_NOSORT); + return (rb_bool_expected(sort, "sort", TRUE) ? 0 : FNM_GLOB_NOSORT); } static VALUE diff --git a/error.c b/error.c index c420aaca3c..35e797f5b9 100644 --- a/error.c +++ b/error.c @@ -1273,7 +1273,7 @@ check_highlight_keyword(VALUE opt, int auto_tty_detect) switch (highlight) { default: - rb_bool_expected(highlight, "highlight"); + rb_bool_expected(highlight, "highlight", TRUE); UNREACHABLE; case Qtrue: case Qfalse: case Qnil: break; } diff --git a/internal/object.h b/internal/object.h index f560fb7f9f..88f3a44bc6 100644 --- a/internal/object.h +++ b/internal/object.h @@ -22,7 +22,7 @@ double rb_num_to_dbl(VALUE val); VALUE rb_obj_dig(int argc, VALUE *argv, VALUE self, VALUE notfound); VALUE rb_immutable_obj_clone(int, VALUE *, VALUE); VALUE rb_check_convert_type_with_id(VALUE,int,const char*,ID); -int rb_bool_expected(VALUE, const char *); +int rb_bool_expected(VALUE, const char *, int raise); static inline void RBASIC_CLEAR_CLASS(VALUE obj); static inline void RBASIC_SET_CLASS_RAW(VALUE obj, VALUE klass); static inline void RBASIC_SET_CLASS(VALUE obj, VALUE klass); diff --git a/io.c b/io.c index fc1a5d8110..a8326fcbcf 100644 --- a/io.c +++ b/io.c @@ -3385,7 +3385,7 @@ io_read_nonblock(rb_execution_context_t *ec, VALUE io, VALUE length, VALUE str, } shrinkable = io_setstrbuf(&str, len); - rb_bool_expected(ex, "exception"); + rb_bool_expected(ex, "exception", TRUE); GetOpenFile(io, fptr); rb_io_check_byte_readable(fptr); @@ -3433,7 +3433,7 @@ io_write_nonblock(rb_execution_context_t *ec, VALUE io, VALUE str, VALUE ex) if (!RB_TYPE_P(str, T_STRING)) str = rb_obj_as_string(str); - rb_bool_expected(ex, "exception"); + rb_bool_expected(ex, "exception", TRUE); io = GetWriteIO(io); GetOpenFile(io, fptr); diff --git a/object.c b/object.c index c7375f0c56..6a7b2cbf23 100644 --- a/object.c +++ b/object.c @@ -3158,16 +3158,22 @@ rb_check_integer_type(VALUE val) } int -rb_bool_expected(VALUE obj, const char *flagname) +rb_bool_expected(VALUE obj, const char *flagname, int raise) { switch (obj) { - case Qtrue: case Qfalse: - break; - default: - rb_raise(rb_eArgError, "expected true or false as %s: %+"PRIsVALUE, - flagname, obj); + case Qtrue: + return TRUE; + case Qfalse: + return FALSE; + default: { + static const char message[] = "expected true or false as %s: %+"PRIsVALUE; + if (raise) { + rb_raise(rb_eArgError, message, flagname, obj); + } + rb_warning(message, flagname, obj); + return !NIL_P(obj); + } } - return obj != Qfalse; } int @@ -3176,7 +3182,7 @@ rb_opts_exception_p(VALUE opts, int default_value) static const ID kwds[1] = {idException}; VALUE exception; if (rb_get_kwargs(opts, kwds, 0, 1, &exception)) - return rb_bool_expected(exception, "exception"); + return rb_bool_expected(exception, "exception", TRUE); return default_value; } @@ -3562,7 +3568,7 @@ rb_f_float1(rb_execution_context_t *ec, VALUE obj, VALUE arg) static VALUE rb_f_float(rb_execution_context_t *ec, VALUE obj, VALUE arg, VALUE opts) { - int exception = rb_bool_expected(opts, "exception"); + int exception = rb_bool_expected(opts, "exception", TRUE); return rb_convert_to_float(arg, exception); } diff --git a/process.c b/process.c index 9568399f93..53727ab116 100644 --- a/process.c +++ b/process.c @@ -2210,7 +2210,7 @@ rb_execarg_addopt_rlimit(struct rb_execarg *eargp, int rtype, VALUE val) } #endif -#define TO_BOOL(val, name) NIL_P(val) ? 0 : rb_bool_expected((val), name) +#define TO_BOOL(val, name) (NIL_P(val) ? 0 : rb_bool_expected((val), name, TRUE)) int rb_execarg_addopt(VALUE execarg_obj, VALUE key, VALUE val) {