Skip to content

Commit eb4bd36

Browse files
committed
Fix repr of bytearray with native storage
1 parent 1e2df9f commit eb4bd36

File tree

1 file changed

+25
-7
lines changed
  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/bytes

1 file changed

+25
-7
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/bytes/BytesBuiltins.java

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -444,33 +444,51 @@ public abstract static class RMulNode extends MulNode {
444444
@GenerateNodeFactory
445445
public abstract static class ReprNode extends PythonUnaryBuiltinNode {
446446
@TruffleBoundary
447-
private static final StringBuilder newStringBuilder() {
448-
return new StringBuilder("b'");
447+
private static StringBuilder newStringBuilder() {
448+
return new StringBuilder();
449449
}
450450

451451
@TruffleBoundary
452-
private static final String sbFinishAndToString(StringBuilder sb) {
453-
sb.append("'");
452+
private static String sbToString(StringBuilder sb) {
454453
return sb.toString();
455454
}
456455

456+
@TruffleBoundary
457+
private static void sbAppend(StringBuilder sb, String s) {
458+
sb.append(s);
459+
}
460+
457461
@Specialization
458462
public Object repr(VirtualFrame frame, PBytes self,
459463
@Cached SequenceStorageNodes.LenNode lenNode,
460464
@Cached SequenceStorageNodes.GetItemNode getItemNode) {
461465
SequenceStorage store = self.getSequenceStorage();
462466
int len = lenNode.execute(store);
463467
StringBuilder sb = newStringBuilder();
468+
sbAppend(sb, "b'");
464469
for (int i = 0; i < len; i++) {
465470
BytesUtils.byteRepr(sb, (byte) getItemNode.executeInt(frame, store, i));
466471
}
467-
return sbFinishAndToString(sb);
472+
sbAppend(sb, "'");
473+
return sbToString(sb);
468474
}
469475

470476
@Specialization
471-
public Object repr(PByteArray self, @Cached("create()") TypeNodes.GetNameNode getNameNode) {
477+
public Object repr(VirtualFrame frame, PByteArray self,
478+
@Cached("create()") TypeNodes.GetNameNode getNameNode,
479+
@Cached SequenceStorageNodes.LenNode lenNode,
480+
@Cached SequenceStorageNodes.GetItemNode getItemNode) {
472481
String typeName = getNameNode.execute(self.getLazyPythonClass());
473-
return self.formatByteArray(typeName);
482+
SequenceStorage store = self.getSequenceStorage();
483+
int len = lenNode.execute(store);
484+
StringBuilder sb = newStringBuilder();
485+
sb.append(typeName);
486+
sb.append("(b'");
487+
for (int i = 0; i < len; i++) {
488+
BytesUtils.byteRepr(sb, (byte) getItemNode.executeInt(frame, store, i));
489+
}
490+
sb.append("')");
491+
return sbToString(sb);
474492
}
475493
}
476494

0 commit comments

Comments
 (0)