Skip to content

Commit 87259d2

Browse files
fangerertimfel
authored andcommitted
Add missing specialization to 'binascii.a2b_base64'.
1 parent aba6db5 commit 87259d2

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BinasciiModuleBuiltins.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
*/
4141
package com.oracle.graal.python.builtins.modules;
4242

43+
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.ValueError;
44+
45+
import java.io.UnsupportedEncodingException;
4346
import java.util.List;
4447
import java.util.zip.CRC32;
4548

@@ -48,7 +51,9 @@
4851
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
4952
import com.oracle.graal.python.builtins.PythonBuiltins;
5053
import com.oracle.graal.python.builtins.objects.PNone;
54+
import com.oracle.graal.python.builtins.objects.bytes.BytesNodes;
5155
import com.oracle.graal.python.builtins.objects.bytes.PBytes;
56+
import com.oracle.graal.python.builtins.objects.bytes.PIBytesLike;
5257
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
5358
import com.oracle.graal.python.builtins.objects.module.PythonModule;
5459
import com.oracle.graal.python.builtins.objects.type.PythonClass;
@@ -88,9 +93,28 @@ public void initialize(PythonCore core) {
8893
@GenerateNodeFactory
8994
static abstract class A2bBase64Node extends PythonUnaryBuiltinNode {
9095
@Specialization
96+
PBytes doString(String data) {
97+
return factory().createBytes(b64decode(data));
98+
}
99+
100+
@Specialization
101+
PBytes doBytesLike(PIBytesLike data,
102+
@Cached("create()") BytesNodes.ToBytesNode toBytesNode) {
103+
return factory().createBytes(b64decode(toBytesNode.execute(data)));
104+
}
105+
91106
@TruffleBoundary
92-
PBytes a2b(String data) {
93-
return factory().createBytes(Base64.decode(data));
107+
private static byte[] b64decode(String data) {
108+
return Base64.decode(data);
109+
}
110+
111+
@TruffleBoundary
112+
private byte[] b64decode(byte[] data) {
113+
try {
114+
return Base64.decode(new String(data, "ascii"));
115+
} catch (UnsupportedEncodingException e) {
116+
throw raise(ValueError);
117+
}
94118
}
95119
}
96120

0 commit comments

Comments
 (0)