Skip to content

Commit b8ad4a3

Browse files
committed
Fix: operations on bytes did not return a bytes object.
1 parent 2206833 commit b8ad4a3

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,34 @@ def test_setitem():
185185
pass
186186

187187

188+
class BaseGetSlice:
189+
def assertEqualWithType(self, a, b):
190+
self.assertEqual(a, b)
191+
self.assertEqual(type(a), type(b))
192+
193+
def test_getslice(self):
194+
# whole sequence
195+
b = self.type2test(b"hello")
196+
self.assertEqualWithType(b[:], self.type2test(b"hello"))
197+
198+
# whole same length as slice
199+
b = self.type2test(b"hellohellohello")
200+
self.assertEqualWithType(b[5:10], self.type2test(b"hello"))
201+
202+
# shrink
203+
b = self.type2test(b"hellohellohello")
204+
self.assertEqualWithType(b[:10], self.type2test(b"hellohello"))
205+
206+
# extend
207+
b = self.type2test(b"hellohelloworld")
208+
self.assertEqualWithType(b[5:], self.type2test(b"helloworld"))
209+
210+
class BytesGetSliceTest(BaseGetSlice, unittest.TestCase):
211+
type2test = bytes
212+
213+
class ByteArrayGetSliceTest(BaseGetSlice, unittest.TestCase):
214+
type2test = bytearray
215+
188216
def test_setslice():
189217
# whole sequence
190218
b = bytearray(b"hello")
@@ -646,6 +674,7 @@ def test_add_mv_to_bytes():
646674
mv = memoryview(b'world')
647675
b += mv
648676
assert b == b'hello world'
677+
assert type(b) == bytes
649678

650679
def test_add_mv_to_bytearray():
651680
ba = bytearray(b'hello ')

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ public Object add(VirtualFrame frame, PBytes self, PMemoryView other,
270270
Object bytesObj = toBytesNode.executeObject(frame, other);
271271
if (isBytesProfile.profile(bytesObj instanceof PBytes)) {
272272
SequenceStorage res = concatNode.execute(self.getSequenceStorage(), ((PBytes) bytesObj).getSequenceStorage());
273-
return factory().createByteArray(res);
273+
return factory().createBytes(res);
274274
}
275275
throw raise(SystemError, "could not get bytes of memoryview");
276276
}
@@ -511,7 +511,7 @@ Object doSlice(PBytes self, Object key,
511511
}
512512

513513
protected static SequenceStorageNodes.GetItemNode createGetItem() {
514-
return SequenceStorageNodes.GetItemNode.create(NormalizeIndexNode.create(), (s, f) -> f.createByteArray(s));
514+
return SequenceStorageNodes.GetItemNode.create(NormalizeIndexNode.create(), (s, f) -> f.createBytes(s));
515515
}
516516
}
517517

0 commit comments

Comments
 (0)