Project

General

Profile

Feature #13224 » 0001-Add-FrozenError-as-a-subclass-of-RuntimeError.patch

Updated to apply to r60182 - jeremyevans0 (Jeremy Evans), 10/15/2017 02:49 AM

View differences:

error.c
VALUE rb_eFatal;
VALUE rb_eStandardError;
VALUE rb_eRuntimeError;
VALUE rb_eFrozenError;
VALUE rb_eTypeError;
VALUE rb_eArgError;
VALUE rb_eIndexError;
......
* Document-class: RuntimeError
*
* A generic error class raised when an invalid operation is attempted.
* Kernel.raise will raise a RuntimeError if no Exception class is
* specified.
*
* [1, 2, 3].freeze << 4
* raise "ouch"
*
* <em>raises the exception:</em>
*
* RuntimeError: can't modify frozen Array
* RuntimeError: ouch
*/
/*
* Document-class: FrozenError
*
* Kernel.raise will raise a RuntimeError if no Exception class is
* specified.
* Raised when there is an attempt to modify a frozen object.
*
* raise "ouch"
* [1, 2, 3].freeze << 4
*
* <em>raises the exception:</em>
*
* RuntimeError: ouch
* FrozenError: can't modify frozen Array
*/
/*
......
rb_define_method(rb_eNoMethodError, "private_call?", nometh_err_private_call_p, 0);
rb_eRuntimeError = rb_define_class("RuntimeError", rb_eStandardError);
rb_eFrozenError = rb_define_class("FrozenError", rb_eRuntimeError);
rb_eSecurityError = rb_define_class("SecurityError", rb_eException);
rb_eNoMemError = rb_define_class("NoMemoryError", rb_eException);
rb_eEncodingError = rb_define_class("EncodingError", rb_eStandardError);
......
void
rb_error_frozen(const char *what)
{
rb_raise(rb_eRuntimeError, "can't modify frozen %s", what);
rb_raise(rb_eFrozenError, "can't modify frozen %s", what);
}
void
......
VALUE path = rb_ary_entry(debug_info, 0);
VALUE line = rb_ary_entry(debug_info, 1);
rb_raise(rb_eRuntimeError, "can't modify frozen %"PRIsVALUE", created at %"PRIsVALUE":%"PRIsVALUE,
rb_raise(rb_eFrozenError, "can't modify frozen %"PRIsVALUE", created at %"PRIsVALUE":%"PRIsVALUE,
CLASS_OF(frozen_obj), path, line);
}
else {
rb_raise(rb_eRuntimeError, "can't modify frozen %"PRIsVALUE,
rb_raise(rb_eFrozenError, "can't modify frozen %"PRIsVALUE,
CLASS_OF(frozen_obj));
}
}
include/ruby/ruby.h
RUBY_EXTERN VALUE rb_eRangeError;
RUBY_EXTERN VALUE rb_eIOError;
RUBY_EXTERN VALUE rb_eRuntimeError;
RUBY_EXTERN VALUE rb_eFrozenError;
RUBY_EXTERN VALUE rb_eSecurityError;
RUBY_EXTERN VALUE rb_eSystemCallError;
RUBY_EXTERN VALUE rb_eThreadError;
object.c
*
* <em>produces:</em>
*
* prog.rb:3:in `<<': can't modify frozen Array (RuntimeError)
* prog.rb:3:in `<<': can't modify frozen Array (FrozenError)
* from prog.rb:3
*
* Objects of the following classes are always frozen: Integer,
test/-ext-/string/test_enc_associate.rb
s = Bug::String.new("abc")
s.force_encoding(Encoding::US_ASCII)
s.freeze
assert_raise(RuntimeError) {s.associate_encoding!(Encoding::US_ASCII)}
assert_raise(RuntimeError) {s.associate_encoding!(Encoding::UTF_8)}
assert_raise(FrozenError) {s.associate_encoding!(Encoding::US_ASCII)}
assert_raise(FrozenError) {s.associate_encoding!(Encoding::UTF_8)}
end
Encoding.list.select(&:dummy?).each do |enc|
test/date/test_date_marshal.rb
a = d.marshal_dump
d.freeze
assert(d.frozen?)
assert_raise(RuntimeError){d.marshal_load(a)}
assert_raise(FrozenError){d.marshal_load(a)}
d = DateTime.now
a = d.marshal_dump
d.freeze
assert(d.frozen?)
assert_raise(RuntimeError){d.marshal_load(a)}
assert_raise(FrozenError){d.marshal_load(a)}
end
end
test/dbm/test_dbm.rb
def test_freeze
DBM.open("#{@tmproot}/a") {|d|
d.freeze
assert_raise(RuntimeError) { d["k"] = "v" }
assert_raise(FrozenError) { d["k"] = "v" }
}
end
end
test/gdbm/test_gdbm.rb
def test_freeze
GDBM.open("#{@tmproot}/a.dbm") {|d|
d.freeze
assert_raise(RuntimeError) { d["k"] = "v" }
assert_raise(FrozenError) { d["k"] = "v" }
}
end
end
test/pathname/test_pathname.rb
obj = Pathname.new("a")
obj.freeze
assert_equal(false, obj.tainted?)
assert_raise(RuntimeError) { obj.taint }
assert_raise(FrozenError) { obj.taint }
obj = Pathname.new("a")
obj.taint
test/ruby/test_array.rb
assert_equal([4, 5, 4, 5, 4, 5], b)
assert_raise(TypeError) { [0].concat(:foo) }
assert_raise(RuntimeError) { [0].freeze.concat(:foo) }
assert_raise(FrozenError) { [0].freeze.concat(:foo) }
end
def test_count
......
fa = a.dup.freeze
assert_nothing_raised(RuntimeError) { a.replace(a) }
assert_raise(RuntimeError) { fa.replace(fa) }
assert_raise(FrozenError) { fa.replace(fa) }
assert_raise(ArgumentError) { fa.replace() }
assert_raise(TypeError) { a.replace(42) }
assert_raise(RuntimeError) { fa.replace(42) }
assert_raise(FrozenError) { fa.replace(42) }
end
def test_reverse
......
o2 = o1.clone
ary << o1 << o2
orig = ary.dup
assert_raise(RuntimeError, "frozen during comparison") {ary.sort!}
assert_raise(FrozenError, "frozen during comparison") {ary.sort!}
assert_equal(orig, ary, "must not be modified once frozen")
end
......
f = a.dup.freeze
assert_raise(ArgumentError) { a.uniq!(1) }
assert_raise(ArgumentError) { f.uniq!(1) }
assert_raise(RuntimeError) { f.uniq! }
assert_raise(FrozenError) { f.uniq! }
assert_nothing_raised do
a = [ {c: "b"}, {c: "r"}, {c: "w"}, {c: "g"}, {c: "g"} ]
......
def test_uniq_bang_with_freeze
ary = [1,2]
orig = ary.dup
assert_raise(RuntimeError, "frozen during comparison") {
assert_raise(FrozenError, "frozen during comparison") {
ary.uniq! {|v| ary.freeze; 1}
}
assert_equal(orig, ary, "must not be modified once frozen")
......
assert_raise(ArgumentError) { [0][0, 0, 0] = 0 }
assert_raise(ArgumentError) { [0].freeze[0, 0, 0] = 0 }
assert_raise(TypeError) { [0][:foo] = 0 }
assert_raise(RuntimeError) { [0].freeze[:foo] = 0 }
assert_raise(FrozenError) { [0].freeze[:foo] = 0 }
end
def test_first2
......
end
def test_unshift_error
assert_raise(RuntimeError) { [].freeze.unshift('cat') }
assert_raise(RuntimeError) { [].freeze.unshift() }
assert_raise(FrozenError) { [].freeze.unshift('cat') }
assert_raise(FrozenError) { [].freeze.unshift() }
end
def test_aref
......
assert_equal([0, 1, 2], a.insert(-1, 2))
assert_equal([0, 1, 3, 2], a.insert(-2, 3))
assert_raise_with_message(IndexError, /-6/) { a.insert(-6, 4) }
assert_raise(RuntimeError) { [0].freeze.insert(0)}
assert_raise(FrozenError) { [0].freeze.insert(0)}
assert_raise(ArgumentError) { [0].freeze.insert }
end
......
assert_raise(ArgumentError) { a.flatten!(1, 2) }
assert_raise(TypeError) { a.flatten!(:foo) }
assert_raise(ArgumentError) { f.flatten!(1, 2) }
assert_raise(RuntimeError) { f.flatten! }
assert_raise(RuntimeError) { f.flatten!(:foo) }
assert_raise(FrozenError) { f.flatten! }
assert_raise(FrozenError) { f.flatten!(:foo) }
end
def test_shuffle
......
assert_equal([], a.rotate!(13))
assert_equal([], a.rotate!(-13))
a = [].freeze
assert_raise_with_message(RuntimeError, /can\'t modify frozen/) {a.rotate!}
assert_raise_with_message(FrozenError, /can\'t modify frozen/) {a.rotate!}
a = [1,2,3]
assert_raise(ArgumentError) { a.rotate!(1, 1) }
end
test/ruby/test_class.rb
obj = Object.new
c = obj.singleton_class
obj.freeze
assert_raise_with_message(RuntimeError, /frozen object/) {
assert_raise_with_message(FrozenError, /frozen object/) {
c.class_eval {def f; end}
}
end
def test_singleton_class_message
c = Class.new.freeze
assert_raise_with_message(RuntimeError, /frozen Class/) {
assert_raise_with_message(FrozenError, /frozen Class/) {
def c.f; end
}
end
test/ruby/test_enumerator.rb
enum = @obj.to_enum
assert_raise(NoMethodError) { enum.each {} }
enum.freeze
assert_raise(RuntimeError) {
assert_raise(FrozenError) {
capture_io do
# warning: Enumerator.new without a block is deprecated; use Object#to_enum
enum.__send__(:initialize, @obj, :foo)
......
assert_equal([1, 2, 3], a)
g.freeze
assert_raise(RuntimeError) {
assert_raise(FrozenError) {
g.__send__ :initialize, proc { |y| y << 4 << 5 }
}
test/ruby/test_eval.rb
def test_fstring_instance_eval
bug = "[ruby-core:78116] [Bug #12930]".freeze
assert_same bug, (bug.instance_eval {self})
assert_raise(RuntimeError) {
assert_raise(FrozenError) {
bug.instance_eval {@ivar = true}
}
end
test/ruby/test_hash.rb
assert_raise(TypeError) { h2.replace(1) }
h2.freeze
assert_raise(ArgumentError) { h2.replace() }
assert_raise(RuntimeError) { h2.replace(h1) }
assert_raise(RuntimeError) { h2.replace(42) }
assert_raise(FrozenError) { h2.replace(h1) }
assert_raise(FrozenError) { h2.replace(42) }
end
def test_size2
test/ruby/test_lazy_enumerator.rb
a = [1, 2, 3].lazy
a.freeze
assert_raise(RuntimeError) {
assert_raise(FrozenError) {
a.__send__ :initialize, [4, 5], &->(y, *v) { y << yield(*v) }
}
end
test/ruby/test_marshal.rb
s.instance_variable_set(:@t, 42)
t = Bug8276.new(s)
s = Marshal.dump(t)
assert_raise(RuntimeError) {Marshal.load(s)}
assert_raise(FrozenError) {Marshal.load(s)}
end
def test_marshal_load_ivar
test/ruby/test_module.rb
def bar; end
end
m.freeze
assert_raise(RuntimeError) do
assert_raise(FrozenError) do
m.module_eval do
def foo; end
end
end
assert_raise(RuntimeError) do
assert_raise(FrozenError) do
m.__send__ :private, :bar
end
assert_raise(RuntimeError) do
assert_raise(FrozenError) do
m.private_class_method :baz
end
end
......
def test_frozen_module
m = Module.new
m.freeze
assert_raise(RuntimeError) do
assert_raise(FrozenError) do
m.instance_eval { undef_method(:foo) }
end
end
......
def test_frozen_class
c = Class.new
c.freeze
assert_raise(RuntimeError) do
assert_raise(FrozenError) do
c.instance_eval { undef_method(:foo) }
end
end
......
o = klass.new
c = class << o; self; end
c.freeze
assert_raise_with_message(RuntimeError, /frozen/) do
assert_raise_with_message(FrozenError, /frozen/) do
c.instance_eval { undef_method(:foo) }
end
klass.class_eval do
......
bug11532 = '[ruby-core:70828] [Bug #11532]'
c = Class.new {const_set(:A, 1)}.freeze
assert_raise_with_message(RuntimeError, /frozen class/, bug11532) {
assert_raise_with_message(FrozenError, /frozen class/, bug11532) {
c.class_eval {private_constant :A}
}
c = Class.new {const_set(:A, 1); private_constant :A}.freeze
assert_raise_with_message(RuntimeError, /frozen class/, bug11532) {
assert_raise_with_message(FrozenError, /frozen class/, bug11532) {
c.class_eval {public_constant :A}
}
c = Class.new {const_set(:A, 1)}.freeze
assert_raise_with_message(RuntimeError, /frozen class/, bug11532) {
assert_raise_with_message(FrozenError, /frozen class/, bug11532) {
c.class_eval {deprecate_constant :A}
}
end
test/ruby/test_object.rb
def test_taint_frozen_obj
o = Object.new
o.freeze
assert_raise(RuntimeError) { o.taint }
assert_raise(FrozenError) { o.taint }
o = Object.new
o.taint
o.freeze
assert_raise(RuntimeError) { o.untaint }
assert_raise(FrozenError) { o.untaint }
end
def test_freeze_immediate
......
attr_accessor :foo
}
obj = klass.new.freeze
assert_raise_with_message(RuntimeError, /#{name}/) {
assert_raise_with_message(FrozenError, /#{name}/) {
obj.foo = 1
}
end
......
def test_remove_method
c = Class.new
c.freeze
assert_raise(RuntimeError) do
assert_raise(FrozenError) do
c.instance_eval { remove_method(:foo) }
end
......
class<<x;self;end.class_eval {define_method(:to_s) {name}}
assert_same(name, x.to_s)
assert_not_predicate(name, :tainted?)
assert_raise(RuntimeError) {name.taint}
assert_raise(FrozenError) {name.taint}
assert_equal("X", [x].join(""))
assert_not_predicate(name, :tainted?)
assert_not_predicate(eval('"X".freeze'), :tainted?)
......
b = yield
assert_nothing_raised("copy") {a.instance_eval {initialize_copy(b)}}
c = a.dup.freeze
assert_raise(RuntimeError, "frozen") {c.instance_eval {initialize_copy(b)}}
assert_raise(FrozenError, "frozen") {c.instance_eval {initialize_copy(b)}}
d = a.dup.trust
[a, b, c, d]
end
test/ruby/test_pack.rb
def test_pack_with_buffer
buf = String.new(capacity: 100)
assert_raise_with_message(RuntimeError, /frozen/) {
assert_raise_with_message(FrozenError, /frozen/) {
[0xDEAD_BEEF].pack('N', buffer: 'foo'.freeze)
}
assert_raise_with_message(TypeError, /must be String/) {
test/ruby/test_rand.rb
def test_initialize_frozen
r = Random.new(0)
r.freeze
assert_raise(RuntimeError, '[Bug #6540]') do
assert_raise(FrozenError, '[Bug #6540]') do
r.__send__(:initialize, r)
end
end
......
r = Random.new(0)
d = r.__send__(:marshal_dump)
r.freeze
assert_raise(RuntimeError, '[Bug #6540]') do
assert_raise(FrozenError, '[Bug #6540]') do
r.__send__(:marshal_load, d)
end
end
test/ruby/test_range.rb
def test_frozen_initialize
r = Range.allocate
r.freeze
assert_raise(RuntimeError){r.__send__(:initialize, 1, 2)}
assert_raise(FrozenError){r.__send__(:initialize, 1, 2)}
end
def test_range_string
test/ruby/test_rubyoptions.rb
def test_frozen_string_literal_debug
with_debug_pat = /created at/
wo_debug_pat = /can\'t modify frozen String \(RuntimeError\)\n\z/
wo_debug_pat = /can\'t modify frozen String \(FrozenError\)\n\z/
frozen = [
["--enable-frozen-string-literal", true],
["--disable-frozen-string-literal", false],
test/ruby/test_string.rb
def test_initialize
str = S("").freeze
assert_equal("", str.__send__(:initialize))
assert_raise(RuntimeError){ str.__send__(:initialize, 'abc') }
assert_raise(RuntimeError){ str.__send__(:initialize, capacity: 1000) }
assert_raise(RuntimeError){ str.__send__(:initialize, 'abc', capacity: 1000) }
assert_raise(RuntimeError){ str.__send__(:initialize, encoding: 'euc-jp') }
assert_raise(RuntimeError){ str.__send__(:initialize, 'abc', encoding: 'euc-jp') }
assert_raise(RuntimeError){ str.__send__(:initialize, 'abc', capacity: 1000, encoding: 'euc-jp') }
assert_raise(FrozenError){ str.__send__(:initialize, 'abc') }
assert_raise(FrozenError){ str.__send__(:initialize, capacity: 1000) }
assert_raise(FrozenError){ str.__send__(:initialize, 'abc', capacity: 1000) }
assert_raise(FrozenError){ str.__send__(:initialize, encoding: 'euc-jp') }
assert_raise(FrozenError){ str.__send__(:initialize, 'abc', encoding: 'euc-jp') }
assert_raise(FrozenError){ str.__send__(:initialize, 'abc', capacity: 1000, encoding: 'euc-jp') }
end
def test_initialize_nonstring
......
assert_equal(S("a").hash, S("a\u0101").chomp!(S("\u0101")).hash, '[ruby-core:22414]')
s = S("").freeze
assert_raise_with_message(RuntimeError, /frozen/) {s.chomp!}
assert_raise_with_message(FrozenError, /frozen/) {s.chomp!}
s = S("ax")
o = Struct.new(:s).new(s)
......
s.freeze
"x"
end
assert_raise_with_message(RuntimeError, /frozen/) {s.chomp!(o)}
assert_raise_with_message(FrozenError, /frozen/) {s.chomp!(o)}
s = S("hello")
assert_equal("hel", s.chomp!('lo'))
......
expected = S("\u0300".encode(Encoding::UTF_16LE))
assert_equal(expected, result, bug7090)
assert_raise(TypeError) { 'foo' << :foo }
assert_raise(RuntimeError) { 'foo'.freeze.concat('bar') }
assert_raise(FrozenError) { 'foo'.freeze.concat('bar') }
end
def test_count
......
assert_equal(s2, s)
fs = "".freeze
assert_raise(RuntimeError) { fs.replace("a") }
assert_raise(RuntimeError) { fs.replace(fs) }
assert_raise(FrozenError) { fs.replace("a") }
assert_raise(FrozenError) { fs.replace(fs) }
assert_raise(ArgumentError) { fs.replace() }
assert_raise(RuntimeError) { fs.replace(42) }
assert_raise(FrozenError) { fs.replace(42) }
end
def test_reverse
......
end
def test_frozen_check
assert_raise(RuntimeError) {
assert_raise(FrozenError) {
s = ""
s.sub!(/\A/) { s.freeze; "zzz" }
}
......
assert_equal("bba", s)
s = S("ax").freeze
assert_raise_with_message(RuntimeError, /frozen/) {s.delete_prefix!("a")}
assert_raise_with_message(FrozenError, /frozen/) {s.delete_prefix!("a")}
s = S("ax")
o = Struct.new(:s).new(s)
......
s.freeze
"a"
end
assert_raise_with_message(RuntimeError, /frozen/) {s.delete_prefix!(o)}
assert_raise_with_message(FrozenError, /frozen/) {s.delete_prefix!(o)}
end
def test_delete_suffix
......
assert_raise(TypeError) { 'hello'.delete_suffix!(/hel/) }
s = S("hello").freeze
assert_raise_with_message(RuntimeError, /frozen/) {s.delete_suffix!('lo')}
assert_raise_with_message(FrozenError, /frozen/) {s.delete_suffix!('lo')}
s = S("ax")
o = Struct.new(:s).new(s)
......
s.freeze
"x"
end
assert_raise_with_message(RuntimeError, /frozen/) {s.delete_suffix!(o)}
assert_raise_with_message(FrozenError, /frozen/) {s.delete_suffix!(o)}
s = S("hello")
assert_equal("hel", s.delete_suffix!('lo'))
test/ruby/test_thread.rb
def test_thread_variable_frozen
t = Thread.new { }.join
t.freeze
assert_raise(RuntimeError) do
assert_raise(FrozenError) do
t.thread_variable_set(:foo, "bar")
end
end
......
end
def test_thread_local_security
assert_raise(RuntimeError) do
assert_raise(FrozenError) do
Thread.new do
Thread.current[:foo] = :bar
Thread.current.freeze
test/ruby/test_transcode.rb
assert_raise(Encoding::UndefinedConversionError) { "\x80".encode('utf-8','ASCII-8BIT') }
assert_raise(Encoding::InvalidByteSequenceError) { "\x80".encode('utf-8','US-ASCII') }
assert_raise(Encoding::UndefinedConversionError) { "\xA5".encode('utf-8','iso-8859-3') }
assert_raise(RuntimeError) { 'hello'.freeze.encode!('iso-8859-1') }
assert_raise(RuntimeError) { '\u3053\u3093\u306b\u3061\u306f'.freeze.encode!('iso-8859-1') } # こんにちは
assert_raise(FrozenError) { 'hello'.freeze.encode!('iso-8859-1') }
assert_raise(FrozenError) { '\u3053\u3093\u306b\u3061\u306f'.freeze.encode!('iso-8859-1') } # こんにちは
end
def test_arguments
test/ruby/test_variable.rb
assert_empty v.instance_variables
msg = "can't modify frozen #{v.class}"
assert_raise_with_message(RuntimeError, msg) do
assert_raise_with_message(FrozenError, msg) do
v.instance_variable_set(:@foo, :bar)
end
assert_nil EnvUtil.suppress_warning {v.instance_variable_get(:@foo)}
assert_not_send([v, :instance_variable_defined?, :@foo])
assert_raise_with_message(RuntimeError, msg) do
assert_raise_with_message(FrozenError, msg) do
v.remove_instance_variable(:@foo)
end
end
test/stringio/test_stringio.rb
s = StringIO.new
s.freeze
bug = '[ruby-core:33648]'
assert_raise(RuntimeError, bug) {s.puts("foo")}
assert_raise(RuntimeError, bug) {s.string = "foo"}
assert_raise(RuntimeError, bug) {s.reopen("")}
assert_raise(FrozenError, bug) {s.puts("foo")}
assert_raise(FrozenError, bug) {s.string = "foo"}
assert_raise(FrozenError, bug) {s.reopen("")}
end
def test_frozen_string
test/test_delegate.rb
a = [42, :hello].freeze
d = SimpleDelegator.new(a)
assert_nothing_raised(bug2679) {d.dup[0] += 1}
assert_raise(RuntimeError) {d.clone[0] += 1}
assert_raise(FrozenError) {d.clone[0] += 1}
d.freeze
assert(d.clone.frozen?)
assert(!d.dup.frozen?)
......
def test_frozen
d = SimpleDelegator.new([1, :foo])
d.freeze
assert_raise(RuntimeError, '[ruby-dev:40314]#1') {d.__setobj__("foo")}
assert_raise(FrozenError, '[ruby-dev:40314]#1') {d.__setobj__("foo")}
assert_equal([1, :foo], d)
end
test/test_set.rb
set << 4
assert_same orig, set.freeze
assert_equal true, set.frozen?
assert_raise(RuntimeError) {
assert_raise(FrozenError) {
set << 5
}
assert_equal 4, set.size
......
set2 = set1.clone
assert_predicate set2, :frozen?
assert_raise(RuntimeError) {
assert_raise(FrozenError) {
set2.add 5
}
end
(2-2/3)