Created
May 23, 2017 04:21
-
-
Save Hanaasagi/30ad96df39c1178a728afa6b2b363071 to your computer and use it in GitHub Desktop.
show the bytecode from a pyc file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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