mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* object.c (rb_inspect): raises Encoding::CompatibilityError if the
result is incompatible with the default external encoding. [ruby-core:41931] [Bug #5848] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@34218 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
4729382e4e
commit
e945532350
3 changed files with 53 additions and 6 deletions
|
@ -1,3 +1,9 @@
|
||||||
|
Fri Jan 6 12:24:11 2012 NARUSE, Yui <naruse@ruby-lang.org>
|
||||||
|
|
||||||
|
* object.c (rb_inspect): raises Encoding::CompatibilityError if the
|
||||||
|
result is incompatible with the default external encoding.
|
||||||
|
[ruby-core:41931] [Bug #5848]
|
||||||
|
|
||||||
Thu Jan 5 15:26:15 2012 NAKAMURA Usaku <usa@ruby-lang.org>
|
Thu Jan 5 15:26:15 2012 NAKAMURA Usaku <usa@ruby-lang.org>
|
||||||
|
|
||||||
* win32/win32.c (check_valid_dir): strict checking of root.
|
* win32/win32.c (check_valid_dir): strict checking of root.
|
||||||
|
|
38
object.c
38
object.c
|
@ -14,6 +14,7 @@
|
||||||
#include "ruby/ruby.h"
|
#include "ruby/ruby.h"
|
||||||
#include "ruby/st.h"
|
#include "ruby/st.h"
|
||||||
#include "ruby/util.h"
|
#include "ruby/util.h"
|
||||||
|
#include "ruby/encoding.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
@ -370,7 +371,9 @@ rb_any_to_s(VALUE obj)
|
||||||
VALUE
|
VALUE
|
||||||
rb_inspect(VALUE obj)
|
rb_inspect(VALUE obj)
|
||||||
{
|
{
|
||||||
return rb_obj_as_string(rb_funcall(obj, id_inspect, 0, 0));
|
VALUE s = rb_obj_as_string(rb_funcall(obj, id_inspect, 0, 0));
|
||||||
|
rb_enc_check(rb_enc_default_external(), s);
|
||||||
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
|
@ -419,14 +422,37 @@ inspect_obj(VALUE obj, VALUE str, int recur)
|
||||||
* call-seq:
|
* call-seq:
|
||||||
* obj.inspect -> string
|
* obj.inspect -> string
|
||||||
*
|
*
|
||||||
* Returns a string containing a human-readable representation of
|
* Returns a string containing a human-readable representation of <i>obj</i>.
|
||||||
* <i>obj</i>. If not overridden and no instance variables, uses the
|
* By default, if the <i>obj</i> has instance variables, show the class name
|
||||||
* <code>to_s</code> method to generate the string.
|
* and instance variable details which is the list of the name and the result
|
||||||
* <i>obj</i>. If not overridden, uses the <code>to_s</code> method to
|
* of <i>inspect</i> method for each instance variables.
|
||||||
* generate the string.
|
* Otherwise uses the <i>to_s</i> method to generate the string.
|
||||||
|
* If the <i>to_s</i> mthoed is overridden, uses it.
|
||||||
|
* User defined classes should override this method to make better
|
||||||
|
* representation of <i>obj</i>. When overriding this method, it should
|
||||||
|
* return a string whose encoding is compatible with the default external
|
||||||
|
* encoding.
|
||||||
*
|
*
|
||||||
* [ 1, 2, 3..4, 'five' ].inspect #=> "[1, 2, 3..4, \"five\"]"
|
* [ 1, 2, 3..4, 'five' ].inspect #=> "[1, 2, 3..4, \"five\"]"
|
||||||
* Time.new.inspect #=> "2008-03-08 19:43:39 +0900"
|
* Time.new.inspect #=> "2008-03-08 19:43:39 +0900"
|
||||||
|
*
|
||||||
|
* class Foo
|
||||||
|
* end
|
||||||
|
* Foo.new.inspect #=> "#<Foo:0x0300c868>"
|
||||||
|
*
|
||||||
|
* class Bar
|
||||||
|
* def initialize
|
||||||
|
* @bar = 1
|
||||||
|
* end
|
||||||
|
* end
|
||||||
|
* Bar.new.inspect #=> "#<Bar:0x0300c868 @bar=1>"
|
||||||
|
*
|
||||||
|
* class Baz
|
||||||
|
* def to_s
|
||||||
|
* "baz"
|
||||||
|
* end
|
||||||
|
* end
|
||||||
|
* Baz.new.inspect #=> "baz"
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
|
|
|
@ -256,6 +256,21 @@ class TestM17N < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_object_utf16_32_inspect
|
||||||
|
orig_int = Encoding.default_internal
|
||||||
|
orig_ext = Encoding.default_external
|
||||||
|
Encoding.default_internal = nil
|
||||||
|
Encoding.default_external = Encoding::UTF_8
|
||||||
|
o = Object.new
|
||||||
|
[Encoding::UTF_16BE, Encoding::UTF_16LE, Encoding::UTF_32BE, Encoding::UTF_32LE].each do |e|
|
||||||
|
o.instance_eval "def inspect;'abc'.encode('#{e}');end"
|
||||||
|
assert_raise(Encoding::CompatibilityError) { [o].inspect }
|
||||||
|
end
|
||||||
|
ensure
|
||||||
|
Encoding.default_internal = orig_int
|
||||||
|
Encoding.default_external = orig_ext
|
||||||
|
end
|
||||||
|
|
||||||
def test_str_dump
|
def test_str_dump
|
||||||
[
|
[
|
||||||
e("\xfe"),
|
e("\xfe"),
|
||||||
|
|
Loading…
Reference in a new issue