Skip to content

Commit 9038d5f

Browse files
committed
[GR-24201] Make imp.get_magic() as written into the pyc files derived from SerializationUtils.VERSION.
PullRequest: graalpython/1036
2 parents 6a92c8d + a0e559a commit 9038d5f

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
7979
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
8080
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
81+
import com.oracle.graal.python.parser.sst.SerializationUtils;
8182
import com.oracle.graal.python.runtime.ExecutionContext.ForeignCallContext;
8283
import com.oracle.graal.python.runtime.PythonContext;
8384
import com.oracle.graal.python.runtime.PythonOptions;
@@ -151,6 +152,25 @@ public boolean run() {
151152
}
152153
}
153154

155+
@Builtin(name = "get_magic")
156+
@GenerateNodeFactory
157+
public abstract static class GetMagic extends PythonBuiltinNode {
158+
// it's b'\x0c\xaf\xaf\xe1', original magic number of GraalPython
159+
private static int BASE_MAGIC_NUMBER = 212840417;
160+
161+
@Specialization
162+
public PBytes run() {
163+
// The magic number in CPython is usually increased by 10.
164+
int number = BASE_MAGIC_NUMBER + 10 * SerializationUtils.VERSION;
165+
byte[] magicNumber = new byte[4];
166+
magicNumber[0] = (byte) (number >>> 24);
167+
magicNumber[1] = (byte) (number >>> 16);
168+
magicNumber[2] = (byte) (number >>> 8);
169+
magicNumber[3] = (byte) (number);
170+
return factory().createBytes(magicNumber);
171+
}
172+
}
173+
154174
@Builtin(name = "__create_dynamic__", minNumOfPositionalArgs = 2)
155175
@GenerateNodeFactory
156176
public abstract static class CreateDynamic extends PythonBuiltinNode {

graalpython/lib-graalpython/_imp.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,6 @@
4040
# Package context -- the full module name for package imports
4141
_py_package_context = None
4242

43-
44-
@__graalpython__.builtin
45-
def get_magic():
46-
return b'\x0c\xaf\xaf\xe1'
47-
48-
4943
@__graalpython__.builtin
5044
def create_dynamic(module_spec, filename=None):
5145
global _py_package_context

0 commit comments

Comments
 (0)