Skip to content

Commit 5ba3d97

Browse files
committed
[GR-13222] can't concat bytearray to memoryview.
PullRequest: graalpython/345
2 parents b0ca5b2 + e778dfd commit 5ba3d97

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
22
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
33
#
44
# The Universal Permissive License (UPL), Version 1.0
@@ -534,3 +534,16 @@ def test_strip_bytes():
534534
assert b'abc'.strip(b'ac') == b'b'
535535
assert b'abc'.lstrip(b'ac') == b'bc'
536536
assert b'abc'.rstrip(b'ac') == b'ab'
537+
538+
def test_add_mv_to_bytes():
539+
b = b'hello '
540+
mv = memoryview(b'world')
541+
b += mv
542+
assert b == b'hello world'
543+
544+
def test_add_mv_to_bytearray():
545+
ba = bytearray(b'hello ')
546+
mv = memoryview(b'world')
547+
ba += mv
548+
assert ba == b'hello world'
549+

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2018, Oracle and/or its affiliates.
2+
* Copyright (c) 2017, 2019, Oracle and/or its affiliates.
33
* Copyright (c) 2014, Regents of the University of California
44
*
55
* All rights reserved.
@@ -244,6 +244,20 @@ public Object add(PByteArray self, PIBytesLike other,
244244
return factory().createByteArray(res);
245245
}
246246

247+
@Specialization
248+
public Object add(PByteArray self, PMemoryView other,
249+
@Cached("create(TOBYTES)") LookupAndCallUnaryNode toBytesNode,
250+
@Cached("createBinaryProfile()") ConditionProfile isBytesProfile,
251+
@Cached("create()") SequenceStorageNodes.ConcatNode concatNode) {
252+
253+
Object bytesObj = toBytesNode.executeObject(other);
254+
if (isBytesProfile.profile(bytesObj instanceof PBytes)) {
255+
SequenceStorage res = concatNode.execute(self.getSequenceStorage(), ((PBytes) bytesObj).getSequenceStorage());
256+
return factory().createByteArray(res);
257+
}
258+
throw raise(SystemError, "could not get bytes of memoryview");
259+
}
260+
247261
@SuppressWarnings("unused")
248262
@Fallback
249263
public Object add(Object self, Object other) {

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2018, Oracle and/or its affiliates.
2+
* Copyright (c) 2017, 2019, Oracle and/or its affiliates.
33
* Copyright (c) 2014, Regents of the University of California
44
*
55
* All rights reserved.
@@ -61,13 +61,16 @@
6161
import com.oracle.graal.python.builtins.objects.dict.PDict;
6262
import com.oracle.graal.python.builtins.objects.iterator.PSequenceIterator;
6363
import com.oracle.graal.python.builtins.objects.list.PList;
64+
import com.oracle.graal.python.builtins.objects.memoryview.PMemoryView;
6465
import com.oracle.graal.python.nodes.PNodeWithContext;
66+
import com.oracle.graal.python.nodes.call.special.LookupAndCallUnaryNode;
6567
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
6668
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
6769
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
6870
import com.oracle.graal.python.nodes.function.builtins.PythonTernaryBuiltinNode;
6971
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
7072
import com.oracle.graal.python.runtime.exception.PythonErrorType;
73+
import static com.oracle.graal.python.runtime.exception.PythonErrorType.SystemError;
7174
import com.oracle.graal.python.runtime.sequence.storage.ByteSequenceStorage;
7275
import com.oracle.graal.python.runtime.sequence.storage.SequenceStorage;
7376
import com.oracle.truffle.api.CompilerDirectives;
@@ -77,6 +80,7 @@
7780
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
7881
import com.oracle.truffle.api.dsl.NodeFactory;
7982
import com.oracle.truffle.api.dsl.Specialization;
83+
import com.oracle.truffle.api.profiles.ConditionProfile;
8084

8185
@CoreFunctions(extendClasses = PythonBuiltinClassType.PBytes)
8286
public class BytesBuiltins extends PythonBuiltins {
@@ -260,6 +264,20 @@ public Object add(PBytes left, PIBytesLike right,
260264
return factory().createBytes(res);
261265
}
262266

267+
@Specialization
268+
public Object add(PBytes self, PMemoryView other,
269+
@Cached("create(TOBYTES)") LookupAndCallUnaryNode toBytesNode,
270+
@Cached("createBinaryProfile()") ConditionProfile isBytesProfile,
271+
@Cached("create()") SequenceStorageNodes.ConcatNode concatNode) {
272+
273+
Object bytesObj = toBytesNode.executeObject(other);
274+
if (isBytesProfile.profile(bytesObj instanceof PBytes)) {
275+
SequenceStorage res = concatNode.execute(self.getSequenceStorage(), ((PBytes) bytesObj).getSequenceStorage());
276+
return factory().createByteArray(res);
277+
}
278+
throw raise(SystemError, "could not get bytes of memoryview");
279+
}
280+
263281
@SuppressWarnings("unused")
264282
@Fallback
265283
public Object add(Object self, Object other) {

0 commit comments

Comments
 (0)