ruby-changes:51957
From: eregon <ko1@a...>
Date: Fri, 3 Aug 2018 22:52:18 +0900 (JST)
Subject: [ruby-changes:51957] eregon:r64172 (trunk): encoding.c (enc_set_index): raise instead of rb_bug() for non-encoding capable objects
eregon 2018-08-03 22:52:13 +0900 (Fri, 03 Aug 2018) New Revision: 64172 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=64172 Log: encoding.c (enc_set_index): raise instead of rb_bug() for non-encoding capable objects * Add spec. Modified files: trunk/encoding.c trunk/spec/ruby/optional/capi/encoding_spec.rb Index: spec/ruby/optional/capi/encoding_spec.rb =================================================================== --- spec/ruby/optional/capi/encoding_spec.rb (revision 64171) +++ spec/ruby/optional/capi/encoding_spec.rb (revision 64172) @@ -30,6 +30,15 @@ describe :rb_enc_set_index, shared: true https://github.com/ruby/ruby/blob/trunk/spec/ruby/optional/capi/encoding_spec.rb#L30 result = @s.send(@method, str, 1) result.first.should == result.last end + + ruby_version_is "2.6" do + it "raises an ArgumentError for a non-encoding capable object" do + obj = Object.new + -> { + result = @s.send(@method, obj, 1) + }.should raise_error(ArgumentError, "cannot set encoding on non-encoding capable object") + end + end end describe "C-API Encoding function" do Index: encoding.c =================================================================== --- encoding.c (revision 64171) +++ encoding.c (revision 64172) @@ -819,7 +819,9 @@ rb_enc_get_index(VALUE obj) https://github.com/ruby/ruby/blob/trunk/encoding.c#L819 static void enc_set_index(VALUE obj, int idx) { - if (!enc_capable(obj)) rb_bug("enc_set_index: not capable object"); + if (!enc_capable(obj)) { + rb_raise(rb_eArgError, "cannot set encoding on non-encoding capable object"); + } if (idx < ENCODING_INLINE_MAX) { ENCODING_SET_INLINED(obj, idx); -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/