Reputation: 280
I'm trying to troubleshoot an interesting issue from a user that involves bytecode. I wanted to determine the magic number associated with these files, to see if they were compiled with different versions. Unfortunately, we're using Jython, which does not appear to have imp.get_magic()
. Does anyone know of an alternate way to determine the magic number? I started looking at the files in a text editor to examine the "first word" as this answer does. But I don't know where, among the slashes and numbers, the "first word" ends. Does anyone know how to figure this out by looking at the actual bytecode? Or does anyone know how Jython's handling of bytecode differs from Python's? I was under the impression that it was done the same way (except for writing *$py.class files instead of *.pyc files), but now I'm not so sure.
EDIT:
First, I decided that trying to read the binary files in a text editor was just silly. I created a module and compiled it to a *.pyc
with Python, then determined the magic number with
with open('compiledModule.pyc', 'rb') as binaryFile:
magicNumber = binaryFile.read(4)
print magicNumber.encode('hex')
which matched the magic number (as it should) from the version of Python that created it, as reported by:
print imp.get_magic().encode('hex')
When I compiled the same module with Jython, into a '*$py.class' file, and read it in with
with open('compiledModule$py.class', 'rb') as otherFile:
cafeBabe = otherFile.read(4)
print cafeBabe.encode('hex')
it indeed came back as cafebabe
. So in this case, where I was trying to determine the version of Jython that created the bytecode, it appears that I cannot.
Upvotes: 0
Views: 2860
Reputation: 50200
A "word" in this context is fixed-length (notionally the size of an integer). Python's magic numbers are four bytes long.
I believe Jython compiles to java bytecode (which is then run by the java VM), rather than python bytecode.
Upvotes: 3