Project

General

Profile

Actions

Bug #5848

closed

Array#inspect having an element with non-ASCII compatible #inspect result

Added by john_firebaugh (John Firebaugh) about 12 years ago. Updated about 12 years ago.

Status:
Closed
Target version:
-
ruby -v:
ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-darwin10.8.0]
Backport:
[ruby-core:41931]

Description

Is this a bug?

class O
def inspect
"foo".encode!(Encoding::UTF_16BE)
end
end
=> nil
[O.new].inspect
=> "\u5B00\u6600\u6F00\u6F5D"

Actions #1

Updated by naruse (Yui NARUSE) about 12 years ago

  • Status changed from Open to Closed
  • % Done changed from 0 to 100

This issue was solved with changeset r34218.
John, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


  • object.c (rb_inspect): raises Encoding::CompatibilityError if the
    result is incompatible with the default external encoding.
    [ruby-core:41931] [Bug #5848]

Updated by john_firebaugh (John Firebaugh) about 12 years ago

=begin
Hmm, not sure that r34218 fully addresses the issue.

o = Object.new
def o.inspect
  "abc".encode(Encoding.default_external)
end

Encoding.default_external = Encoding::UTF_16BE
[o].inspect #=> "\u5B00\u6100\u6200\u635D"

=end

Updated by naruse (Yui NARUSE) about 12 years ago

  • Status changed from Closed to Assigned
  • Assignee set to naruse (Yui NARUSE)

Updated by phasis68 (Heesob Park) about 12 years ago

Here is a patch for this issue.
But I suspect it may cause another issue.

diff --git a/array.c b/array.c.new
index e58ac0b..bdad94d 100644
--- a/array.c
+++ b/array.c.new
@@ -1792,19 +1792,24 @@ inspect_ary(VALUE ary, VALUE dummy, int recur)
int tainted = OBJ_TAINTED(ary);
int untrust = OBJ_UNTRUSTED(ary);
long i;

  • VALUE s, str;
  • VALUE s, str, temp;

    if (recur) return rb_usascii_str_new_cstr("[...]");
    str = rb_str_buf_new2("[");

  • temp = rb_str_buf_new2(", ");
    for (i=0; i<RARRAY_LEN(ary); i++) {
    s = rb_inspect(RARRAY_PTR(ary)[i]);
    if (OBJ_TAINTED(s)) tainted = TRUE;
    if (OBJ_UNTRUSTED(s)) untrust = TRUE;

  • if (i > 0) rb_str_buf_cat2(str, ", ");
  • else rb_enc_copy(str, s);
  • if (i > 0) {
  • temp = rb_str_conv_enc(temp,rb_enc_get(temp),rb_enc_get(str));	  
    
  • rb_str_buf_append(str, temp);
    
  • } else
  • str = rb_str_conv_enc(str,rb_enc_get(str),rb_enc_get(s));	
    
    rb_str_buf_append(str, s);
    }
  • rb_str_buf_cat2(str, "]");
  • temp = rb_str_conv_enc(rb_str_buf_new2("]"),rb_usascii_encoding(),rb_enc_get(str));
  • rb_str_buf_append(str, temp);
    if (tainted) OBJ_TAINT(str);
    if (untrust) OBJ_UNTRUST(str);
    return str;
Actions #5

Updated by naruse (Yui NARUSE) about 12 years ago

  • Status changed from Assigned to Closed

This issue was solved with changeset r34308.
John, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


  • object.c (rb_inspect): raise the result is not compatible with
    the default external encoding. [ruby-core:42095] [Bug #5848]
    If the default external encoding is ASCII compatible, the encoding of
    inspected result must be compatible with it.
    If the default external encoding is ASCII incomapatible,
    the result must be ASCII only.
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0