Skip to content

Commit 802927c

Browse files
committed
Restructuring of Array#inspect and missing enc checks rb_inspect.
1 parent 0c9ebf6 commit 802927c

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

core/src/main/java/org/jruby/RubyArray.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939

4040
import org.jcodings.Encoding;
4141
import org.jcodings.specific.USASCIIEncoding;
42+
import org.jcodings.specific.UTF16BEEncoding;
4243
import org.jruby.anno.JRubyClass;
4344
import org.jruby.anno.JRubyMethod;
4445
import org.jruby.common.IRubyWarnings.ID;
@@ -1444,15 +1445,15 @@ private IRubyObject inspectAry(ThreadContext context) {
14441445
boolean tainted = isTaint();
14451446

14461447
for (int i = 0; i < realLength; i++) {
1447-
if (i > 0) str.cat((byte)',').cat((byte)' ');
14481448

1449-
RubyString str2 = inspect(context, safeArrayRef(values, begin + i));
1450-
if (i == 0 && str2.getEncoding() != encoding) str.setEncoding(str2.getEncoding());
1451-
if (str2.isTaint()) tainted = true;
1452-
1453-
str.cat19(str2);
1449+
RubyString s = inspect(context, safeArrayRef(values, begin + i));
1450+
if (s.isTaint()) tainted = true;
1451+
if (i > 0) str.cat(',', UTF16BEEncoding.INSTANCE).cat(' ', UTF16BEEncoding.INSTANCE);
1452+
else str.setEncoding(s.getEncoding());
1453+
1454+
str.cat19(s);
14541455
}
1455-
str.cat((byte)']');
1456+
str.cat(']', UTF16BEEncoding.INSTANCE);
14561457

14571458
if (tainted) str.setTaint(true);
14581459

core/src/main/java/org/jruby/RubyObject.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import java.util.List;
4646
import java.util.Set;
4747

48+
import org.jcodings.Encoding;
4849
import org.jruby.anno.JRubyClass;
4950
import org.jruby.runtime.Helpers;
5051
import org.jruby.runtime.Block;
@@ -532,7 +533,19 @@ private int nonFixnumHashCode(IRubyObject hashValue) {
532533
* Prefered over callMethod(context, "inspect")
533534
*/
534535
public static RubyString inspect(ThreadContext context, IRubyObject object) {
535-
return RubyString.objAsString(context, object.callMethod(context, "inspect"));
536+
Ruby runtime = context.runtime;
537+
RubyString str = RubyString.objAsString(context, object.callMethod(context, "inspect"));
538+
Encoding ext = runtime.getDefaultExternalEncoding();
539+
if (!ext.isAsciiCompatible()) {
540+
if (!str.isAsciiOnly()) {
541+
throw runtime.newEncodingCompatibilityError("inspected result must be ASCII only if default external encoding is ASCII incompatible");
542+
}
543+
return str;
544+
}
545+
if (str.getEncoding() != ext && !str.isAsciiOnly()) {
546+
throw runtime.newEncodingCompatibilityError("inspected result must be ASCII only or use the default external encoding");
547+
}
548+
return str;
536549
}
537550

538551
/**

0 commit comments

Comments
 (0)