|
40 | 40 | */
|
41 | 41 | package com.oracle.graal.python.builtins.modules;
|
42 | 42 |
|
| 43 | +import static com.oracle.graal.python.builtins.PythonBuiltinClassType.ValueError; |
| 44 | + |
| 45 | +import java.io.UnsupportedEncodingException; |
43 | 46 | import java.util.List;
|
44 | 47 | import java.util.zip.CRC32;
|
45 | 48 |
|
|
48 | 51 | import com.oracle.graal.python.builtins.PythonBuiltinClassType;
|
49 | 52 | import com.oracle.graal.python.builtins.PythonBuiltins;
|
50 | 53 | import com.oracle.graal.python.builtins.objects.PNone;
|
| 54 | +import com.oracle.graal.python.builtins.objects.bytes.BytesNodes; |
51 | 55 | import com.oracle.graal.python.builtins.objects.bytes.PBytes;
|
| 56 | +import com.oracle.graal.python.builtins.objects.bytes.PIBytesLike; |
52 | 57 | import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
|
53 | 58 | import com.oracle.graal.python.builtins.objects.module.PythonModule;
|
54 | 59 | import com.oracle.graal.python.builtins.objects.type.PythonClass;
|
@@ -88,9 +93,28 @@ public void initialize(PythonCore core) {
|
88 | 93 | @GenerateNodeFactory
|
89 | 94 | static abstract class A2bBase64Node extends PythonUnaryBuiltinNode {
|
90 | 95 | @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 | + |
91 | 106 | @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 | + } |
94 | 118 | }
|
95 | 119 | }
|
96 | 120 |
|
|
0 commit comments