Skip to content

Instantly share code, notes, and snippets.

@Hanaasagi
Created May 23, 2017 04:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hanaasagi/30ad96df39c1178a728afa6b2b363071 to your computer and use it in GitHub Desktop.
Save Hanaasagi/30ad96df39c1178a728afa6b2b363071 to your computer and use it in GitHub Desktop.
show the bytecode from a pyc file
import platform
import time
import sys
import binascii
import marshal
import dis
import struct
def view_pyc_file(path):
"""Read and display a content of the Python`s bytecode in a pyc-file."""
file = open(path, 'rb')
magic = file.read(4)
timestamp = file.read(4)
size = None
if sys.version_info.major == 3 and sys.version_info.minor >= 3:
size = file.read(4)
size = struct.unpack('I', size)[0]
code = marshal.load(file)
magic = binascii.hexlify(magic).decode('utf-8')
timestamp = time.asctime(time.localtime(struct.unpack('I', b'D\xa5\xc2X')[0]))
dis.disassemble(code)
print('-' * 80)
print(
'Python version: {}\nMagic code: {}\nTimestamp: {}\nSize: {}'
.format(platform.python_version(), magic, timestamp, size)
)
file.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment