|
68 | 68 | import com.oracle.graal.python.builtins.objects.common.HashingCollectionNodes.SetItemNode;
|
69 | 69 | import com.oracle.graal.python.builtins.objects.dict.PDict;
|
70 | 70 | import com.oracle.graal.python.builtins.objects.exception.PBaseException;
|
| 71 | +import com.oracle.graal.python.builtins.objects.ints.IntBuiltins; |
71 | 72 | import com.oracle.graal.python.builtins.objects.ints.PInt;
|
72 | 73 | import com.oracle.graal.python.builtins.objects.module.PythonModule;
|
73 | 74 | import com.oracle.graal.python.builtins.objects.object.PythonObject;
|
|
85 | 86 | import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
|
86 | 87 | import com.oracle.graal.python.nodes.statement.ExceptionHandlingStatementNode;
|
87 | 88 | import com.oracle.graal.python.nodes.util.CastToJavaStringNode;
|
88 |
| -import com.oracle.graal.python.parser.sst.SerializationUtils; |
89 | 89 | import com.oracle.graal.python.runtime.ExecutionContext.ForeignCallContext;
|
90 | 90 | import com.oracle.graal.python.runtime.PythonContext;
|
91 | 91 | import com.oracle.graal.python.runtime.PythonCore;
|
@@ -174,19 +174,23 @@ public boolean run() {
|
174 | 174 | @Builtin(name = "get_magic")
|
175 | 175 | @GenerateNodeFactory
|
176 | 176 | public abstract static class GetMagic extends PythonBuiltinNode {
|
177 |
| - // it's b'\x0c\xaf\xaf\xe1', original magic number of GraalPython |
178 |
| - private static int BASE_MAGIC_NUMBER = 212840417; |
179 |
| - |
180 | 177 | @Specialization
|
181 |
| - public PBytes run() { |
182 |
| - // The magic number in CPython is usually increased by 10. |
183 |
| - int number = BASE_MAGIC_NUMBER + 10 * SerializationUtils.VERSION; |
184 |
| - byte[] magicNumber = new byte[4]; |
185 |
| - magicNumber[0] = (byte) (number >>> 24); |
186 |
| - magicNumber[1] = (byte) (number >>> 16); |
187 |
| - magicNumber[2] = (byte) (number >>> 8); |
188 |
| - magicNumber[3] = (byte) (number); |
189 |
| - return factory().createBytes(magicNumber); |
| 178 | + public PBytes run(@SuppressWarnings("unused") VirtualFrame frame, |
| 179 | + @SuppressWarnings("unused") @Cached IntBuiltins.ToBytesNode toBytesNode, |
| 180 | + @SuppressWarnings("unused") @CachedLibrary(limit = "1") PythonObjectLibrary pol, |
| 181 | + @Cached("getMagicNumberBytes(frame, toBytesNode, pol)") PBytes magicBytes) { |
| 182 | + return magicBytes; |
| 183 | + } |
| 184 | + |
| 185 | + protected PBytes getMagicNumberBytes(VirtualFrame frame, IntBuiltins.ToBytesNode toBytesNode, PythonObjectLibrary pol) { |
| 186 | + try { |
| 187 | + PBytes magic = toBytesNode.execute(frame, 3413, 2, "little", false); |
| 188 | + byte[] magicBytes = pol.getBufferBytes(magic); |
| 189 | + return factory().createBytes(new byte[]{magicBytes[0], magicBytes[1], '\r', '\n'}); |
| 190 | + } catch (UnsupportedMessageException e) { |
| 191 | + CompilerDirectives.transferToInterpreterAndInvalidate(); |
| 192 | + throw new IllegalStateException("magicBytes does not support getBufferBytes()"); |
| 193 | + } |
190 | 194 | }
|
191 | 195 | }
|
192 | 196 |
|
|
0 commit comments