From dfa7e6a5f2bca76060c7d7f5dd22ea738af201e9 Mon Sep 17 00:00:00 2001 From: akr Date: Sat, 3 May 2008 09:12:13 +0000 Subject: * time.c (time_timespec): raise TypeError for nil and other objects which has no divmod method. git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@16277 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- time.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) (limited to 'time.c') diff --git a/time.c b/time.c index 176033265..460a18450 100644 --- a/time.c +++ b/time.c @@ -217,18 +217,24 @@ time_timespec(VALUE num, int interval) break; default: - ary = rb_check_array_type(rb_funcall(num, id_divmod, 1, INT2FIX(1))); - if (NIL_P(ary)) { + if (rb_respond_to(num, id_divmod)) { + ary = rb_check_array_type(rb_funcall(num, id_divmod, 1, INT2FIX(1))); + if (NIL_P(ary)) { + goto typeerror; + } + i = rb_ary_entry(ary, 0); + f = rb_ary_entry(ary, 1); + t.tv_sec = NUM2LONG(i); + if (interval && t.tv_sec < 0) + rb_raise(rb_eArgError, "%s must be positive", tstr); + f = rb_funcall(f, id_mul, 1, INT2FIX(1000000000)); + t.tv_nsec = NUM2LONG(f); + } + else { +typeerror: rb_raise(rb_eTypeError, "can't convert %s into %s", rb_obj_classname(num), tstr); } - i = rb_ary_entry(ary, 0); - f = rb_ary_entry(ary, 1); - t.tv_sec = NUM2LONG(i); - if (interval && t.tv_sec < 0) - rb_raise(rb_eArgError, "%s must be positive", tstr); - f = rb_funcall(f, id_mul, 1, INT2FIX(1000000000)); - t.tv_nsec = NUM2LONG(f); break; } return t; -- cgit