Skip to content

Commit 5853489

Browse files
committed
Add repr for unicode arrays
1 parent e1b3212 commit 5853489

File tree

2 files changed

+15
-7
lines changed
  • graalpython

2 files changed

+15
-7
lines changed

graalpython/com.oracle.graal.python.test/src/tests/unittest_tags/test_array.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,7 @@
455455
*graalpython.lib-python.3.test.test_array.UnicodeTest.test_tofromfile
456456
*graalpython.lib-python.3.test.test_array.UnicodeTest.test_tofromlist
457457
*graalpython.lib-python.3.test.test_array.UnicodeTest.test_tofromstring
458+
*graalpython.lib-python.3.test.test_array.UnicodeTest.test_unicode
458459
*graalpython.lib-python.3.test.test_array.UnicodeTest.test_weakref
459460
*graalpython.lib-python.3.test.test_array.UnsignedByteTest.test_add
460461
*graalpython.lib-python.3.test.test_array.UnsignedByteTest.test_assignment

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/array/ArrayBuiltins.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -368,22 +368,29 @@ abstract static class ReprNode extends PythonUnaryBuiltinNode {
368368
static String repr(VirtualFrame frame, PArray self,
369369
@Cached("create(__REPR__)") LookupAndCallUnaryNode reprNode,
370370
@Cached ConditionProfile isEmptyProfile,
371+
@Cached ConditionProfile isUnicodeProfile,
371372
@Cached CastToJavaStringNode cast,
373+
@Cached ToUnicodeNode toUnicodeNode,
372374
@Cached ArrayNodes.GetValueNode getValueNode) {
373375
StringBuilder sb = PythonUtils.newStringBuilder();
374376
PythonUtils.append(sb, "array('");
375377
PythonUtils.append(sb, self.getFormatStr());
376378
PythonUtils.append(sb, '\'');
377379
if (isEmptyProfile.profile(self.getLength() != 0)) {
378-
PythonUtils.append(sb, ", [");
379-
for (int i = 0; i < self.getLength(); i++) {
380-
if (i > 0) {
381-
PythonUtils.append(sb, ", ");
380+
if (isUnicodeProfile.profile(self.getFormat() == BufferFormat.UNICODE)) {
381+
PythonUtils.append(sb, ", ");
382+
PythonUtils.append(sb, cast.execute(reprNode.executeObject(frame, toUnicodeNode.call(frame, self))));
383+
} else {
384+
PythonUtils.append(sb, ", [");
385+
for (int i = 0; i < self.getLength(); i++) {
386+
if (i > 0) {
387+
PythonUtils.append(sb, ", ");
388+
}
389+
Object value = getValueNode.execute(self, i);
390+
PythonUtils.append(sb, cast.execute(reprNode.executeObject(frame, value)));
382391
}
383-
Object value = getValueNode.execute(self, i);
384-
PythonUtils.append(sb, cast.execute(reprNode.executeObject(frame, value)));
392+
PythonUtils.append(sb, ']');
385393
}
386-
PythonUtils.append(sb, ']');
387394
}
388395
PythonUtils.append(sb, ')');
389396
return PythonUtils.sbToString(sb);

0 commit comments

Comments
 (0)