0

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.

Community
  • 1
  • 1
aeroNotAuto
  • 260
  • 3
  • 13

1 Answers1

3

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.

alexis
  • 48,685
  • 16
  • 101
  • 161
  • 1
    Indeed, Jython compiles to `.class` files instead of Python bytecode. So, Jython's magic number would be `0xCAFEBABE`. – Fred Foo Mar 09 '12 at 20:56
  • 1
    CAFEBABE?? Java programmers have all the fun. – alexis Mar 09 '12 at 21:02
  • @larsmans so then it sounds like jython's magic number will be the magic number for java class files? that holds different meaning, if i'm understanding correctly, than the python magic number (and would explain the absence of `imp.get_magic()`). so then jython/java bytecode doesn't change with the version it is compiled with, like python? – aeroNotAuto Mar 09 '12 at 21:27
  • 1
    @alexis: I tend to associate Java programming more with the hangover ;) – Fred Foo Mar 09 '12 at 22:22
  • 1
    @asia1281: Jython compiles Python to Java bytecode; there are different versions of that, but which one you get depends on the JDK rather than Jython. – Fred Foo Mar 09 '12 at 22:24
  • But here's a different question, then: Does Jython embed version information in some (other) way in the .class files? There are plenty of potential version dependencies beyond compatibility with the VM. – alexis Mar 10 '12 at 12:35