1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* 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
This commit is contained in:
marcandre 2012-03-14 21:10:34 +00:00
parent 35c356ea72
commit 7316302483
20 changed files with 130 additions and 137 deletions

View file

@ -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

12
array.c
View file

@ -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 file

@ -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 file

@ -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());
}

13
eval.c
View file

@ -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--) {

6
file.c
View file

@ -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)) {

4
hash.c
View file

@ -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;

View file

@ -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 file

@ -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]);

6
proc.c
View file

@ -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 file

@ -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 file

@ -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)) {

4
re.c
View file

@ -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 file

@ -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)) {

View file

@ -2727,7 +2727,7 @@ rb_str_match_m(int argc, VALUE *argv, VALUE str)
{
VALUE re, result;
if (argc < 1)
rb_raise(rb_eArgError, "wrong number of arguments (%d for 1..2)", argc);
rb_check_arity(argc, 1, 2);
re = argv[0];
argv[0] = str;
result = rb_funcall2(get_pat(re, 0), rb_intern("match"), argc, argv);
@ -3237,9 +3237,7 @@ rb_str_aref_m(int argc, VALUE *argv, VALUE str)
}
return rb_str_substr(str, NUM2LONG(argv[0]), NUM2LONG(argv[1]));
}
if (argc != 1) {
rb_raise(rb_eArgError, "wrong number of arguments (%d for 1..2)", argc);
}
rb_check_arity(argc, 1, 2);
return rb_str_aref(str, argv[0]);
}
@ -3467,9 +3465,7 @@ rb_str_aset_m(int argc, VALUE *argv, VALUE str)
}
return argv[2];
}
if (argc != 2) {
rb_raise(rb_eArgError, "wrong number of arguments (%d for 2..3)", argc);
}
rb_check_arity(argc, 2, 3);
return rb_str_aset(str, argv[0], argv[1]);
}
@ -3532,9 +3528,7 @@ rb_str_slice_bang(int argc, VALUE *argv, VALUE str)
VALUE buf[3];
int i;
if (argc < 1 || 2 < argc) {
rb_raise(rb_eArgError, "wrong number of arguments (%d for 1..2)", argc);
}
rb_check_arity(argc, 1, 2);
for (i=0; i<argc; i++) {
buf[i] = argv[i];
}
@ -3593,11 +3587,13 @@ rb_str_sub_bang(int argc, VALUE *argv, VALUE str)
int tainted = 0;
int untrusted = 0;
long plen;
int min_arity = rb_block_given_p() ? 1 : 2;
if (argc == 1 && rb_block_given_p()) {
rb_check_arity(argc, min_arity, 2);
if (argc == 1) {
iter = 1;
}
else if (argc == 2) {
else {
repl = argv[1];
hash = rb_check_convert_type(argv[1], T_HASH, "Hash", "to_hash");
if (NIL_P(hash)) {
@ -3606,9 +3602,6 @@ rb_str_sub_bang(int argc, VALUE *argv, VALUE str)
if (OBJ_TAINTED(repl)) tainted = 1;
if (OBJ_UNTRUSTED(repl)) untrusted = 1;
}
else {
rb_raise(rb_eArgError, "wrong number of arguments (%d for 1..2)", argc);
}
pat = get_pat(argv[0], 1);
str_modifiable(str);
@ -3761,7 +3754,7 @@ str_gsub(int argc, VALUE *argv, VALUE str, int bang)
if (OBJ_TAINTED(repl)) tainted = 1;
break;
default:
rb_raise(rb_eArgError, "wrong number of arguments (%d for 1..2)", argc);
rb_check_arity(argc, 1, 2);
}
pat = get_pat(argv[0], 1);
@ -4120,9 +4113,7 @@ rb_str_byteslice(int argc, VALUE *argv, VALUE str)
if (argc == 2) {
return str_byte_substr(str, NUM2LONG(argv[0]), NUM2LONG(argv[1]));
}
if (argc != 1) {
rb_raise(rb_eArgError, "wrong number of arguments (%d for 1..2)", argc);
}
rb_check_arity(argc, 1, 2);
return str_byte_aref(str, argv[0]);
}
@ -5394,9 +5385,7 @@ rb_str_delete_bang(int argc, VALUE *argv, VALUE str)
int i, ascompat, cr;
if (RSTRING_LEN(str) == 0 || !RSTRING_PTR(str)) return Qnil;
if (argc < 1) {
rb_raise(rb_eArgError, "wrong number of arguments (at least 1)");
}
rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS);
for (i=0; i<argc; i++) {
VALUE s = argv[i];
@ -5640,9 +5629,7 @@ rb_str_count(int argc, VALUE *argv, VALUE str)
int i;
int ascompat;
if (argc < 1) {
rb_raise(rb_eArgError, "wrong number of arguments (at least 1)");
}
rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS);
for (i=0; i<argc; i++) {
VALUE tstr = argv[i];
unsigned char c;

View file

@ -776,9 +776,7 @@ rb_struct_select(int argc, VALUE *argv, VALUE s)
VALUE result;
long i;
if (argc > 0) {
rb_raise(rb_eArgError, "wrong number of arguments (%d for 0)", argc);
}
rb_check_arity(argc, 0, 0);
RETURN_ENUMERATOR(s, 0, 0);
result = rb_ary_new();
for (i = 0; i < RSTRUCT_LEN(s); i++) {

View file

@ -58,4 +58,14 @@ class TestArity < Test::Unit::TestCase
assert_equal :ok, p.call(1, 2, 3)
assert_equal :ok, p.call
end
def test_message_change_issue_6085
assert_equal "3 for 1..2", err_mess{ SignalException.new(1, "", nil) }
assert_equal "1 for 0", err_mess{ Hash.new(1){} }
assert_equal "3 for 1..2", err_mess{ Module.send :define_method, 1, 2, 3 }
assert_equal "1 for 2", err_mess{ "".sub!(//) }
assert_equal "0 for 1..2", err_mess{ "".sub!{} }
assert_equal "0 for 1+", err_mess{ exec }
assert_equal "0 for 1+", err_mess{ Struct.new }
end
end

View file

@ -2665,9 +2665,7 @@ str_transcode0(int argc, VALUE *argv, VALUE *self, int ecflags, VALUE ecopts)
const char *sname, *dname;
int dencidx;
if (argc <0 || argc > 2) {
rb_raise(rb_eArgError, "wrong number of arguments (%d for 0..2)", argc);
}
rb_check_arity(argc, 0, 2);
if (argc == 0) {
arg1 = rb_enc_default_internal();
@ -2984,8 +2982,7 @@ econv_args(int argc, VALUE *argv,
if (!NIL_P(flags_v)) {
if (!NIL_P(opt)) {
rb_raise(rb_eArgError, "wrong number of arguments (%d for 2..3)",
argc + 1);
rb_error_arity(argc + 1, 2, 3);
}
ecflags = NUM2INT(rb_to_int(flags_v));
ecopts = Qnil;
@ -3672,8 +3669,7 @@ econv_primitive_convert(int argc, VALUE *argv, VALUE self)
if (!NIL_P(flags_v)) {
if (!NIL_P(opt)) {
rb_raise(rb_eArgError, "wrong number of arguments (%d for 2..5)",
argc + 1);
rb_error_arity(argc + 1, 2, 5);
}
flags = NUM2INT(rb_to_int(flags_v));
}

View file

@ -87,16 +87,12 @@ vm_call0(rb_thread_t* th, VALUE recv, VALUE id, int argc, const VALUE *argv,
break;
}
case VM_METHOD_TYPE_ATTRSET: {
if (argc != 1) {
rb_raise(rb_eArgError, "wrong number of arguments (%d for 1)", argc);
}
rb_check_arity(argc, 1, 1);
val = rb_ivar_set(recv, def->body.attr.id, argv[0]);
break;
}
case VM_METHOD_TYPE_IVAR: {
if (argc != 0) {
rb_raise(rb_eArgError, "wrong number of arguments (%d for 0)", argc);
}
rb_check_arity(argc, 0, 0);
val = rb_attr_get(recv, def->body.attr.id);
break;
}
@ -1283,36 +1279,24 @@ static VALUE
specific_eval(int argc, VALUE *argv, VALUE klass, VALUE self)
{
if (rb_block_given_p()) {
if (argc > 0) {
rb_raise(rb_eArgError, "wrong number of arguments (%d for 0)", argc);
}
rb_check_arity(argc, 0, 0);
return yield_under(klass, self, Qundef);
}
else {
const char *file = "(eval)";
int line = 1;
if (argc == 0) {
rb_raise(rb_eArgError, "block not supplied");
rb_check_arity(argc, 1, 3);
if (rb_safe_level() >= 4) {
StringValue(argv[0]);
}
else {
if (rb_safe_level() >= 4) {
StringValue(argv[0]);
}
else {
SafeStringValue(argv[0]);
}
if (argc > 3) {
const char *name = rb_id2name(rb_frame_callee());
rb_raise(rb_eArgError,
"wrong number of arguments: %s(src) or %s{..}",
name, name);
}
if (argc > 2)
line = NUM2INT(argv[2]);
if (argc > 1) {
file = StringValuePtr(argv[1]);
}
SafeStringValue(argv[0]);
}
if (argc > 2)
line = NUM2INT(argv[2]);
if (argc > 1) {
file = StringValuePtr(argv[1]);
}
return eval_under(klass, self, argv[0], file, line);
}

View file

@ -163,6 +163,11 @@ unknown_keyword_error(const rb_iseq_t *iseq, VALUE hash)
rb_raise(rb_eArgError, msg, RSTRING_PTR(keys));
}
void
rb_error_arity(int argc, int min, int max) {
rb_exc_raise(rb_arg_error_new(argc, min, max));
}
#define VM_CALLEE_SETUP_ARG(ret, th, iseq, orig_argc, orig_argv, block) \
if (LIKELY((iseq)->arg_simple & 0x01)) { \
/* simple check */ \
@ -354,10 +359,7 @@ call_cfunc(VALUE (*func)(), VALUE recv,
{
/* printf("len: %d, argc: %d\n", len, argc); */
if (len >= 0 && argc != len) {
rb_raise(rb_eArgError, "wrong number of arguments(%d for %d)",
argc, len);
}
if (len >= 0) rb_check_arity(argc, len, len);
switch (len) {
case -2:
@ -581,17 +583,13 @@ vm_call_method(rb_thread_t *th, rb_control_frame_t *cfp,
break;
}
case VM_METHOD_TYPE_ATTRSET:{
if (num != 1) {
rb_raise(rb_eArgError, "wrong number of arguments (%d for 1)", num);
}
rb_check_arity(num, 1, 1);
val = rb_ivar_set(recv, me->def->body.attr.id, *(cfp->sp - 1));
cfp->sp -= 2;
break;
}
case VM_METHOD_TYPE_IVAR:{
if (num != 0) {
rb_raise(rb_eArgError, "wrong number of arguments (%d for 0)", num);
}
rb_check_arity(num, 0, 0);
val = rb_attr_get(recv, me->def->body.attr.id);
cfp->sp -= 1;
break;