Skip to content

Commit 6fde7f5

Browse files
committed
make repr of collections show appropriate subclass
To reflect the changes made in CPython 3.7 and make the test pass for it.
1 parent a200448 commit 6fde7f5

File tree

6 files changed

+29
-20
lines changed

6 files changed

+29
-20
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_bytes.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ def test_delitem():
311311
assert b == bytearray()
312312

313313

314+
@unittest.skipIf(sys.version_info.minor < 7, "Requires Python 3.7+")
314315
def test_subclass():
315316

316317
class MyByteArray(bytearray):
@@ -321,7 +322,7 @@ def __str__(self):
321322
b1 = bytearray(range(10))
322323
b2 = MyByteArray(range(10))
323324
assert b1 == b2
324-
assert "<<%s>>" % str(b1) == str(b2)
325+
assert "<<%s>>" % str(b1).replace('bytearray', 'MyByteArray') == str(b2)
325326

326327
class MyBytes(bytes):
327328

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
5555
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes.GenNodeSupplier;
5656
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes.GeneralizationNode;
57+
import com.oracle.graal.python.builtins.objects.type.TypeNodes;
5758
import com.oracle.graal.python.nodes.SpecialMethodNames;
5859
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
5960
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
@@ -203,7 +204,12 @@ String str(PArray self) {
203204
typeCode = "d";
204205
array = Arrays.toString(((DoubleSequenceStorage) sequenceStorage).getInternalDoubleArray());
205206
}
206-
return String.format("array('%s', %s)", typeCode, array);
207+
String typeName = TypeNodes.GetNameNode.doSlowPath(self.getLazyPythonClass());
208+
if (sequenceStorage.length() == 0) {
209+
return String.format("%s('%s')", typeName, typeCode);
210+
} else {
211+
return String.format("%s('%s', %s)", typeName, typeCode, array);
212+
}
207213
}
208214
}
209215

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

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
import static com.oracle.graal.python.nodes.SpecialMethodNames.__REPR__;
4545
import static com.oracle.graal.python.nodes.SpecialMethodNames.__RMUL__;
4646
import static com.oracle.graal.python.nodes.SpecialMethodNames.__SETITEM__;
47-
import static com.oracle.graal.python.nodes.SpecialMethodNames.__STR__;
4847
import static com.oracle.graal.python.runtime.exception.PythonErrorType.SystemError;
4948
import static com.oracle.graal.python.runtime.exception.PythonErrorType.TypeError;
5049
import static com.oracle.graal.python.runtime.exception.PythonErrorType.ValueError;
@@ -65,6 +64,7 @@
6564
import com.oracle.graal.python.builtins.objects.range.PRange;
6665
import com.oracle.graal.python.builtins.objects.slice.PSlice;
6766
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
67+
import com.oracle.graal.python.builtins.objects.type.TypeNodes;
6868
import com.oracle.graal.python.nodes.SpecialMethodNames;
6969
import com.oracle.graal.python.nodes.call.special.LookupAndCallUnaryNode;
7070
import com.oracle.graal.python.nodes.control.GetIteratorExpressionNode.GetIteratorNode;
@@ -333,21 +333,13 @@ public Object mul(Object self, Object other) {
333333
public abstract static class RMulNode extends MulNode {
334334
}
335335

336-
@Builtin(name = __STR__, minNumOfPositionalArgs = 1)
337-
@GenerateNodeFactory
338-
public abstract static class StrNode extends PythonUnaryBuiltinNode {
339-
@Specialization
340-
public Object str(PByteArray self) {
341-
return self.toString();
342-
}
343-
}
344-
345336
@Builtin(name = __REPR__, minNumOfPositionalArgs = 1)
346337
@GenerateNodeFactory
347338
public abstract static class ReprNode extends PythonUnaryBuiltinNode {
348339
@Specialization
349-
public Object repr(PByteArray self) {
350-
return self.toString();
340+
public Object repr(PByteArray self, @Cached("create()") TypeNodes.GetNameNode getNameNode) {
341+
String typeName = getNameNode.execute(self.getLazyPythonClass());
342+
return self.formatByteArray(typeName);
351343
}
352344
}
353345

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,16 @@ public final void setSequenceStorage(SequenceStorage store) {
6969
@Override
7070
@TruffleBoundary
7171
public String toString() {
72-
if (store instanceof ByteSequenceStorage) {
73-
byte[] barr = ((ByteSequenceStorage) store).getInternalByteArray();
74-
return String.format("bytearray(%s)", BytesUtils.bytesRepr(barr, barr.length));
72+
return formatByteArray("bytearray");
73+
}
74+
75+
@TruffleBoundary
76+
public String formatByteArray(String typeName) {
77+
if (getSequenceStorage() instanceof ByteSequenceStorage) {
78+
byte[] barr = ((ByteSequenceStorage) getSequenceStorage()).getInternalByteArray();
79+
return String.format("%s(%s)", typeName, BytesUtils.bytesRepr(barr, barr.length));
7580
} else {
76-
return String.format("bytearray(%s)", store);
81+
return String.format("%s(%s)", typeName, getSequenceStorage());
7782
}
7883
}
7984

graalpython/lib-graalpython/_collections.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ def __repr__(self):
341341
maxlen_repr = ''
342342
else:
343343
maxlen_repr = ', maxlen=%d' % (self.maxlen,)
344-
return 'deque(%s%s)' % (list_repr, maxlen_repr)
344+
return '%s(%s%s)' % (type(self).__name__, list_repr, maxlen_repr)
345345

346346
def __compare__(self, other, op):
347347
if not isinstance(other, deque):

graalpython/lib-graalpython/itertools.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ def __next__(self):
3838
self.step += 1
3939
return self.obj
4040

41+
def __repr__(self):
42+
if self.times is not None:
43+
return "{}({}, {})".format(type(self).__name__, self.obj, self.times)
44+
return "{}({})".format(type(self).__name__, self.obj)
45+
4146

4247
class chain():
4348
"""
@@ -120,7 +125,7 @@ def __next__(self):
120125
return _cnt
121126

122127
def __repr__(self):
123-
_repr = 'count({}'.format(self._cnt)
128+
_repr = '{}({}'.format(type(self).__name__, self._cnt)
124129
if not isinstance(self._step, int) or self._step != 1:
125130
_repr += ', {}'.format(self._step)
126131
return _repr + ')'

0 commit comments

Comments
 (0)