object.c: check const names
* object.c (rb_mod_const_get, rb_mod_const_defined): check constant names more strictly. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@44197 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
1e4a955435
commit
a370556cd2
13
object.c
13
object.c
@ -2134,16 +2134,16 @@ rb_mod_const_get(int argc, VALUE *argv, VALUE mod)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!id) {
|
if (!id) {
|
||||||
if (!ISUPPER(*pbeg) || !rb_enc_symname2_p(pbeg, len, enc)) {
|
part = rb_str_subseq(name, beglen, len);
|
||||||
part = rb_str_subseq(name, beglen, len);
|
OBJ_FREEZE(part);
|
||||||
|
if (!ISUPPER(*pbeg) || !rb_is_const_name(part)) {
|
||||||
rb_name_error_str(part, "wrong constant name %"PRIsVALUE,
|
rb_name_error_str(part, "wrong constant name %"PRIsVALUE,
|
||||||
QUOTE(part));
|
QUOTE(part));
|
||||||
}
|
}
|
||||||
else if (!rb_method_basic_definition_p(CLASS_OF(mod), id_const_missing)) {
|
else if (!rb_method_basic_definition_p(CLASS_OF(mod), id_const_missing)) {
|
||||||
id = rb_intern3(pbeg, len, enc);
|
id = rb_intern_str(part);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
part = rb_str_subseq(name, beglen, len);
|
|
||||||
rb_name_error_str(part, "uninitialized constant %"PRIsVALUE"%"PRIsVALUE,
|
rb_name_error_str(part, "uninitialized constant %"PRIsVALUE"%"PRIsVALUE,
|
||||||
rb_str_subseq(name, 0, beglen),
|
rb_str_subseq(name, 0, beglen),
|
||||||
QUOTE(part));
|
QUOTE(part));
|
||||||
@ -2271,8 +2271,9 @@ rb_mod_const_defined(int argc, VALUE *argv, VALUE mod)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!id) {
|
if (!id) {
|
||||||
if (!ISUPPER(*pbeg) || !rb_enc_symname2_p(pbeg, len, enc)) {
|
part = rb_str_subseq(name, beglen, len);
|
||||||
part = rb_str_subseq(name, beglen, len);
|
OBJ_FREEZE(part);
|
||||||
|
if (!ISUPPER(*pbeg) || !rb_is_const_name(part)) {
|
||||||
rb_name_error_str(part, "wrong constant name %"PRIsVALUE,
|
rb_name_error_str(part, "wrong constant name %"PRIsVALUE,
|
||||||
QUOTE(part));
|
QUOTE(part));
|
||||||
}
|
}
|
||||||
|
@ -240,7 +240,7 @@ class TestModule < Test::Unit::TestCase
|
|||||||
assert_not_operator(Math, :const_defined?, "IP")
|
assert_not_operator(Math, :const_defined?, "IP")
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_bad_constants
|
def each_bad_constants(m, &b)
|
||||||
[
|
[
|
||||||
"#<Class:0x7b8b718b>",
|
"#<Class:0x7b8b718b>",
|
||||||
":Object",
|
":Object",
|
||||||
@ -248,15 +248,28 @@ class TestModule < Test::Unit::TestCase
|
|||||||
":",
|
":",
|
||||||
["String::", "[Bug #7573]"],
|
["String::", "[Bug #7573]"],
|
||||||
"\u3042",
|
"\u3042",
|
||||||
|
"Name?",
|
||||||
].each do |name, msg|
|
].each do |name, msg|
|
||||||
expected = "wrong constant name %s" % quote(name)
|
expected = "wrong constant name %s" % quote(name)
|
||||||
msg = "#{msg}#{': ' if msg}wrong constant name #{name.dump}"
|
msg = "#{msg}#{': ' if msg}wrong constant name #{name.dump}"
|
||||||
assert_raise_with_message(NameError, expected, msg) {
|
assert_raise_with_message(NameError, expected, "#{msg} to #{m}") do
|
||||||
Object.const_get name
|
yield name
|
||||||
}
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_bad_constants_get
|
||||||
|
each_bad_constants("get") {|name|
|
||||||
|
Object.const_get name
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_bad_constants_defined
|
||||||
|
each_bad_constants("defined?") {|name|
|
||||||
|
Object.const_defined? name
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
def test_leading_colons
|
def test_leading_colons
|
||||||
assert_equal Object, AClass.const_get('::Object')
|
assert_equal Object, AClass.const_get('::Object')
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user