Permalink
Browse files

* include/ruby/intern.h: Add rb_check_arity, rb_error_arity [#6085]

* array.c: Use rb_check_arity / rb_error_arity

* class.c: ditto

* enumerator.c: ditto

* eval.c: ditto

* file.c: ditto

* hash.c: ditto

* numeric.c: ditto

* proc.c: ditto

* process.c: ditto

* random.c: ditto

* re.c: ditto

* signal.c: ditto

* string.c: ditto

* struct.c: ditto

* transcode.c: ditto

* vm_eval.c: ditto

* vm_insnhelper.c: ditto & implementation of rb_error_arity

* test/ruby/test_arity.rb: tests for above

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@35024 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information...
1 parent 35c356e commit 7316302483182a73136b00ad368585e551cbd86f @marcandre marcandre committed Mar 14, 2012
Showing with 130 additions and 137 deletions.
  1. +49 −0 ChangeLog
  2. +3 −9 array.c
  3. +1 −6 class.c
  4. +1 −1 enumerator.c
  5. +4 −9 eval.c
  6. +3 −3 file.c
  7. +1 −3 hash.c
  8. +6 −0 include/ruby/intern.h
  9. +4 −8 numeric.c
  10. +2 −4 proc.c
  11. +3 −7 process.c
  12. +2 −2 random.c
  13. +1 −3 re.c
  14. +4 −9 signal.c
  15. +12 −25 string.c
  16. +1 −3 struct.c
  17. +10 −0 test/ruby/test_arity.rb
  18. +3 −7 transcode.c
  19. +12 −28 vm_eval.c
  20. +8 −10 vm_insnhelper.c
View
@@ -1,3 +1,52 @@
+Thu Mar 15 06:08:06 2012 Marc-Andre Lafortune <ruby-core@marc-andre.ca>
+
+ * include/ruby/intern.h: Add rb_check_arity, rb_error_arity [#6085]
+
+ * array.c: Use rb_check_arity / rb_error_arity
+
+ * class.c: ditto
+
+ * enumerator.c: ditto
+
+ * eval.c: ditto
+
+ * file.c: ditto
+
+ * hash.c: ditto
+
+ * numeric.c: ditto
+
+ * proc.c: ditto
+
+ * process.c: ditto
+
+ * random.c: ditto
+
+ * re.c: ditto
+
+ * signal.c: ditto
+
+ * string.c: ditto
+
+ * struct.c: ditto
+
+ * transcode.c: ditto
+
+ * vm_eval.c: ditto
+
+ * vm_insnhelper.c: ditto & implementation of rb_error_arity
+
+ * test/ruby/test_arity.rb: tests for above
+
+Thu Mar 15 06:08:05 2012 Marc-Andre Lafortune <ruby-core@marc-andre.ca>
+
+ * vm_insnhelper.c: improve number of arguments error in case of
+ optional parameters (issue #6085)
+
+ * include/ruby/intern.h: define UNLIMITED_ARGUMENTS
+
+ * test/ruby/test_arity.rb: test for above
+
Thu Mar 15 00:58:04 2012 Shugo Maeda <shugo@ruby-lang.org>
* enumerator.c (enumerable_lazy): fix the documentation of
View
@@ -1443,9 +1443,7 @@ rb_ary_aset(int argc, VALUE *argv, VALUE ary)
rb_ary_splice(ary, beg, len, argv[2]);
return argv[2];
}
- if (argc != 2) {
- rb_raise(rb_eArgError, "wrong number of arguments (%d for 2)", argc);
- }
+ rb_check_arity(argc, 2, 2);
rb_ary_modify_check(ary);
if (FIXNUM_P(argv[0])) {
offset = FIX2LONG(argv[0]);
@@ -1480,9 +1478,7 @@ rb_ary_insert(int argc, VALUE *argv, VALUE ary)
{
long pos;
- if (argc < 1) {
- rb_raise(rb_eArgError, "wrong number of arguments (at least 1)");
- }
+ rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS);
rb_ary_modify_check(ary);
if (argc == 1) return ary;
pos = NUM2LONG(argv[0]);
@@ -3902,9 +3898,7 @@ rb_ary_shuffle_bang(int argc, VALUE *argv, VALUE ary)
if (OPTHASH_GIVEN_P(opts)) {
randgen = rb_hash_lookup2(opts, sym_random, randgen);
}
- if (argc > 0) {
- rb_raise(rb_eArgError, "wrong number of arguments (%d for 0)", argc);
- }
+ rb_check_arity(argc, 0, 0);
rb_ary_modify(ary);
i = RARRAY_LEN(ary);
ptr = RARRAY_PTR(ary);
View
@@ -1655,12 +1655,7 @@ rb_scan_args(int argc, const VALUE *argv, const char *fmt, ...)
return argc;
argc_error:
- if (0 < n_opt)
- rb_raise(rb_eArgError, "wrong number of arguments (%d for %d..%d%s)",
- argc, n_mand, n_mand + n_opt, f_var ? "+" : "");
- else
- rb_raise(rb_eArgError, "wrong number of arguments (%d for %d%s)",
- argc, n_mand, f_var ? "+" : "");
+ rb_error_arity(argc, n_mand, f_var ? UNLIMITED_ARGUMENTS : n_mand + n_opt);
}
/*!
View
@@ -352,7 +352,7 @@ enumerator_initialize(int argc, VALUE *argv, VALUE obj)
if (argc == 0) {
if (!rb_block_given_p())
- rb_raise(rb_eArgError, "wrong number of argument (0 for 1+)");
+ rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS);
recv = generator_init(generator_allocate(rb_cGenerator), rb_block_proc());
}
View
@@ -555,7 +555,7 @@ make_exception(int argc, VALUE *argv, int isstr)
}
break;
default:
- rb_raise(rb_eArgError, "wrong number of arguments (%d for 0..3)", argc);
+ rb_check_arity(argc, 0, 3);
break;
}
if (argc > 0) {
@@ -889,10 +889,7 @@ rb_mod_mix_into(int argc, VALUE *argv, VALUE klass)
st_table *const_tbl = 0, *method_tbl = 0;
int i = 0;
- if (argc < 1 || argc > 3) {
- wrong_args:
- rb_raise(rb_eArgError, "wrong number of arguments (%d for 1)", argc);
- }
+ rb_check_arity(argc, 1, 3);
module = argv[i++];
switch (TYPE(module)) {
@@ -922,7 +919,7 @@ rb_mod_mix_into(int argc, VALUE *argv, VALUE klass)
}
methods = tmp;
}
- if (i < argc) goto wrong_args;
+ if (i < argc) rb_raise(rb_eArgError, "wrong arguments");
if (!NIL_P(constants)) {
VALUE hash = rb_hash_new();
for (i = 0; i < RARRAY_LEN(constants); ++i) {
@@ -1015,9 +1012,7 @@ rb_obj_extend(int argc, VALUE *argv, VALUE obj)
{
int i;
- if (argc == 0) {
- rb_raise(rb_eArgError, "wrong number of arguments (at least 1)");
- }
+ rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS);
for (i = 0; i < argc; i++)
Check_Type(argv[i], T_MODULE);
while (argc--) {
View
@@ -2581,7 +2581,7 @@ rb_file_s_umask(int argc, VALUE *argv)
omask = umask(NUM2INT(argv[0]));
}
else {
- rb_raise(rb_eArgError, "wrong number of arguments (%d for 0..1)", argc);
+ rb_check_arity(argc, 0, 1);
}
return INT2FIX(omask);
}
@@ -4180,7 +4180,7 @@ test_check(int n, int argc, VALUE *argv)
rb_secure(2);
n+=1;
- if (n != argc) rb_raise(rb_eArgError, "wrong number of arguments (%d for %d)", argc, n);
+ rb_check_arity(argc, n, n);
for (i=1; i<n; i++) {
switch (TYPE(argv[i])) {
case T_STRING:
@@ -4258,7 +4258,7 @@ rb_f_test(int argc, VALUE *argv)
{
int cmd;
- if (argc == 0) rb_raise(rb_eArgError, "wrong number of arguments (0 for 2..3)");
+ if (argc == 0) rb_check_arity(argc, 2, 3);
cmd = NUM2CHR(argv[0]);
if (cmd == 0) goto unknown;
if (strchr("bcdefgGkloOprRsSuwWxXz", cmd)) {
View
@@ -323,9 +323,7 @@ rb_hash_initialize(int argc, VALUE *argv, VALUE hash)
rb_hash_modify(hash);
if (rb_block_given_p()) {
- if (argc > 0) {
- rb_raise(rb_eArgError, "wrong number of arguments");
- }
+ rb_check_arity(argc, 0, 0);
ifnone = rb_block_proc();
default_proc_arity_check(ifnone);
RHASH_IFNONE(hash) = ifnone;
@@ -256,6 +256,12 @@ int rb_sourceline(void);
const char *rb_sourcefile(void);
VALUE rb_check_funcall(VALUE, ID, int, VALUE*);
+NORETURN(void rb_error_arity(int, int, int));
+#define rb_check_arity(argc, min, max) do { \
+ if (((argc) < (min)) || ((argc) > (max) && (max) != UNLIMITED_ARGUMENTS)) \
+ rb_error_arity(argc, min, max); \
+ } while(0)
+
#if defined(NFDBITS) && defined(HAVE_RB_FD_INIT)
typedef struct {
int maxfd;
View
@@ -1765,13 +1765,9 @@ num_step(int argc, VALUE *argv, VALUE from)
step = INT2FIX(1);
}
else {
- if (argc == 2) {
- to = argv[0];
- step = argv[1];
- }
- else {
- rb_raise(rb_eArgError, "wrong number of arguments (%d for 1..2)", argc);
- }
+ rb_check_arity(argc, 1, 2);
+ to = argv[0];
+ step = argv[1];
if (rb_equal(step, INT2FIX(0))) {
rb_raise(rb_eArgError, "step can't be 0");
}
@@ -2357,7 +2353,7 @@ int_chr(int argc, VALUE *argv, VALUE num)
case 1:
break;
default:
- rb_raise(rb_eArgError, "wrong number of arguments (%d for 0..1)", argc);
+ rb_check_arity(argc, 0, 1);
break;
}
enc = rb_to_encoding(argv[0]);
View
@@ -1350,7 +1350,8 @@ rb_mod_define_method(int argc, VALUE *argv, VALUE mod)
id = rb_to_id(argv[0]);
body = rb_block_lambda();
}
- else if (argc == 2) {
+ else {
+ rb_check_arity(argc, 1, 2);
id = rb_to_id(argv[0]);
body = argv[1];
if (!rb_obj_is_method(body) && !rb_obj_is_proc(body)) {
@@ -1359,9 +1360,6 @@ rb_mod_define_method(int argc, VALUE *argv, VALUE mod)
rb_obj_classname(body));
}
}
- else {
- rb_raise(rb_eArgError, "wrong number of arguments (%d for 1)", argc);
- }
if (rb_obj_is_method(body)) {
struct METHOD *method = (struct METHOD *)DATA_PTR(body);
View
@@ -1721,9 +1721,7 @@ rb_check_argv(int argc, VALUE *argv)
int i;
const char *name = 0;
- if (argc == 0) {
- rb_raise(rb_eArgError, "wrong number of arguments");
- }
+ rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS);
prog = 0;
tmp = rb_check_array_type(argv[0]);
@@ -3385,11 +3383,9 @@ rb_f_sleep(int argc, VALUE *argv)
if (argc == 0) {
rb_thread_sleep_forever();
}
- else if (argc == 1) {
- rb_thread_wait_for(rb_time_interval(argv[0]));
- }
else {
- rb_raise(rb_eArgError, "wrong number of arguments (%d for 0..1)", argc);
+ rb_check_arity(argc, 0, 1);
+ rb_thread_wait_for(rb_time_interval(argv[0]));
}
end = time(0) - beg;
View
@@ -1168,8 +1168,8 @@ rand_random(int argc, VALUE *argv, rb_random_t *rnd)
if (argc == 0) {
return rb_float_new(genrand_real(&rnd->mt));
}
- else if (argc != 1) {
- rb_raise(rb_eArgError, "wrong number of arguments (%d for 0..1)", argc);
+ else {
+ rb_check_arity(argc, 0, 1);
}
vmax = argv[0];
if (NIL_P(vmax)) {
View
@@ -2904,9 +2904,7 @@ rb_reg_initialize_m(int argc, VALUE *argv, VALUE self)
const char *ptr;
long len;
- if (argc == 0 || argc > 3) {
- rb_raise(rb_eArgError, "wrong number of arguments (%d for 1..3)", argc);
- }
+ rb_check_arity(argc, 1, 3);
if (TYPE(argv[0]) == T_REGEXP) {
VALUE re = argv[0];
View
@@ -231,10 +231,7 @@ esignal_init(int argc, VALUE *argv, VALUE self)
if (!NIL_P(sig)) argnum = 2;
else sig = argv[0];
}
- if (argc < 1 || argnum < argc) {
- rb_raise(rb_eArgError, "wrong number of arguments (%d for %d)",
- argc, argnum);
- }
+ rb_check_arity(argc, 1, argnum);
if (argnum == 2) {
signo = NUM2INT(sig);
if (signo < 0 || signo > NSIG) {
@@ -346,8 +343,8 @@ rb_f_kill(int argc, VALUE *argv)
const char *s;
rb_secure(2);
- if (argc < 2)
- rb_raise(rb_eArgError, "wrong number of arguments (%d for at least 2)", argc);
+ rb_check_arity(argc, 2, UNLIMITED_ARGUMENTS);
+
switch (TYPE(argv[0])) {
case T_FIXNUM:
sig = FIX2INT(argv[0]);
@@ -931,9 +928,7 @@ sig_trap(int argc, VALUE *argv)
struct trap_arg arg;
rb_secure(2);
- if (argc < 1 || argc > 2) {
- rb_raise(rb_eArgError, "wrong number of arguments (%d for 1..2)", argc);
- }
+ rb_check_arity(argc, 1, 2);
arg.sig = trap_signm(argv[0]);
if (reserved_signal_p(arg.sig)) {
Oops, something went wrong.

0 comments on commit 7316302

Please sign in to comment.