[Feature #19590] Show the invalid clock argument

Include the failed clock argument in the error message from
`Process.clock_gettime` and `Process.clock_getres`.
This commit is contained in:
Nobuyoshi Nakada 2023-04-03 17:26:22 +09:00
parent 86db7a1cb8
commit 3612b1bed8
Notes: git 2023-04-13 05:07:50 +00:00
2 changed files with 21 additions and 8 deletions

View File

@ -7960,6 +7960,11 @@ ruby_real_ms_time(void)
# define NUM2CLOCKID(x) 0 # define NUM2CLOCKID(x) 0
#endif #endif
#define clock_failed(name, err, arg) do { \
int clock_error = (err); \
rb_syserr_fail_str(clock_error, rb_sprintf("clock_" name "(%+"PRIsVALUE")", (arg))); \
} while (0)
/* /*
* call-seq: * call-seq:
* Process.clock_gettime(clock_id [, unit]) -> number * Process.clock_gettime(clock_id [, unit]) -> number
@ -8260,15 +8265,17 @@ rb_clock_gettime(int argc, VALUE *argv, VALUE _)
gettime: gettime:
ret = clock_gettime(c, &ts); ret = clock_gettime(c, &ts);
if (ret == -1) if (ret == -1)
rb_sys_fail("clock_gettime"); clock_failed("gettime", errno, clk_id);
tt.count = (int32_t)ts.tv_nsec; tt.count = (int32_t)ts.tv_nsec;
tt.giga_count = ts.tv_sec; tt.giga_count = ts.tv_sec;
denominators[num_denominators++] = 1000000000; denominators[num_denominators++] = 1000000000;
goto success; goto success;
#endif #endif
} }
/* EINVAL emulates clock_gettime behavior when clock_id is invalid. */ else {
rb_syserr_fail(EINVAL, 0); rb_unexpected_type(clk_id, T_SYMBOL);
}
clock_failed("gettime", EINVAL, clk_id);
success: success:
return make_clock_result(&tt, numerators, num_numerators, denominators, num_denominators, unit); return make_clock_result(&tt, numerators, num_numerators, denominators, num_denominators, unit);
@ -8433,15 +8440,17 @@ rb_clock_getres(int argc, VALUE *argv, VALUE _)
getres: getres:
ret = clock_getres(c, &ts); ret = clock_getres(c, &ts);
if (ret == -1) if (ret == -1)
rb_sys_fail("clock_getres"); clock_failed("getres", errno, clk_id);
tt.count = (int32_t)ts.tv_nsec; tt.count = (int32_t)ts.tv_nsec;
tt.giga_count = ts.tv_sec; tt.giga_count = ts.tv_sec;
denominators[num_denominators++] = 1000000000; denominators[num_denominators++] = 1000000000;
goto success; goto success;
#endif #endif
} }
/* EINVAL emulates clock_getres behavior when clock_id is invalid. */ else {
rb_syserr_fail(EINVAL, 0); rb_unexpected_type(clk_id, T_SYMBOL);
}
clock_failed("getres", EINVAL, clk_id);
success: success:
if (unit == ID2SYM(id_hertz)) { if (unit == ID2SYM(id_hertz)) {

View File

@ -2156,7 +2156,9 @@ EOS
t3 = Process.clock_gettime(Process::CLOCK_REALTIME, :nanosecond) t3 = Process.clock_gettime(Process::CLOCK_REALTIME, :nanosecond)
assert_operator(t1, :<=, t2) assert_operator(t1, :<=, t2)
assert_operator(t2, :<=, t3) assert_operator(t2, :<=, t3)
assert_raise(Errno::EINVAL) { Process.clock_gettime(:foo) } assert_raise_with_message(Errno::EINVAL, /:foo/) do
Process.clock_gettime(:foo)
end
end end
def test_clock_gettime_unit def test_clock_gettime_unit
@ -2261,7 +2263,9 @@ EOS
rescue Errno::EINVAL rescue Errno::EINVAL
else else
assert_kind_of(Integer, r) assert_kind_of(Integer, r)
assert_raise(Errno::EINVAL) { Process.clock_getres(:foo) } assert_raise_with_message(Errno::EINVAL, /:foo/) do
Process.clock_getres(:foo)
end
end end
def test_clock_getres_constants def test_clock_getres_constants