зеркало из https://github.com/github/ruby.git
* object.c (rb_mod_const_get): Fix constant missing exception class
and message to maintain backwards compatibility. Constant search should start at Object when constant starts with '::' * test/ruby/test_module.rb: test for fixes git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37494 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
Родитель
e5e5d0c55e
Коммит
ac7f5157ac
|
@ -1,3 +1,11 @@
|
||||||
|
Tue Nov 6 20:40:28 2012 Aaron Patterson <aaron@tenderlovemaking.com>
|
||||||
|
|
||||||
|
* object.c (rb_mod_const_get): Fix constant missing exception class
|
||||||
|
and message to maintain backwards compatibility. Constant search
|
||||||
|
should start at Object when constant starts with '::'
|
||||||
|
|
||||||
|
* test/ruby/test_module.rb: test for fixes
|
||||||
|
|
||||||
Tue Nov 6 16:50:00 2012 Masaki Matsushita <glass.saga@gmail.com>
|
Tue Nov 6 16:50:00 2012 Masaki Matsushita <glass.saga@gmail.com>
|
||||||
|
|
||||||
* lib/tempfile.rb (Tempfile#inspect): fix confusing #inspect.
|
* lib/tempfile.rb (Tempfile#inspect): fix confusing #inspect.
|
||||||
|
|
18
object.c
18
object.c
|
@ -1935,12 +1935,28 @@ rb_mod_const_get(int argc, VALUE *argv, VALUE mod)
|
||||||
}
|
}
|
||||||
|
|
||||||
pbeg = p = path;
|
pbeg = p = path;
|
||||||
|
|
||||||
|
if (!*p) {
|
||||||
|
rb_raise(rb_eNameError, "wrong constant name %s", path);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (p[0] == ':' && p[1] == ':') {
|
||||||
|
mod = rb_cObject;
|
||||||
|
p += 2;
|
||||||
|
pbeg = p;
|
||||||
|
}
|
||||||
|
|
||||||
while (*p) {
|
while (*p) {
|
||||||
while (*p && *p != ':') p++;
|
while (*p && *p != ':') p++;
|
||||||
|
|
||||||
|
if (pbeg == p) {
|
||||||
|
rb_raise(rb_eNameError, "wrong constant name %s", path);
|
||||||
|
}
|
||||||
|
|
||||||
id = rb_intern3(pbeg, p-pbeg, enc);
|
id = rb_intern3(pbeg, p-pbeg, enc);
|
||||||
if (p[0] == ':') {
|
if (p[0] == ':') {
|
||||||
if (p[1] != ':') {
|
if (p[1] != ':') {
|
||||||
rb_raise(rb_eArgError, "undefined class/module %.*s", (int)(p-path), path);
|
rb_raise(rb_eNameError, "wrong constant name %s", path);
|
||||||
}
|
}
|
||||||
p += 2;
|
p += 2;
|
||||||
pbeg = p;
|
pbeg = p;
|
||||||
|
|
|
@ -240,6 +240,24 @@ class TestModule < Test::Unit::TestCase
|
||||||
assert(!Math.const_defined?("IP"))
|
assert(!Math.const_defined?("IP"))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_bad_constants
|
||||||
|
[
|
||||||
|
"#<Class:0x7b8b718b>",
|
||||||
|
":Object",
|
||||||
|
"",
|
||||||
|
":",
|
||||||
|
].each do |name|
|
||||||
|
e = assert_raises(NameError) {
|
||||||
|
Object.const_get name
|
||||||
|
}
|
||||||
|
assert_equal("wrong constant name %s" % name, e.message)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_leading_colons
|
||||||
|
assert_equal Object, AClass.const_get('::Object')
|
||||||
|
end
|
||||||
|
|
||||||
def test_const_get
|
def test_const_get
|
||||||
assert_equal(Math::PI, Math.const_get("PI"))
|
assert_equal(Math::PI, Math.const_get("PI"))
|
||||||
assert_equal(Math::PI, Math.const_get(:PI))
|
assert_equal(Math::PI, Math.const_get(:PI))
|
||||||
|
|
Загрузка…
Ссылка в новой задаче