https://github.com/evanw/unwind
Universal disassembler for Python bytecode (supports Python 2 and 3)
https://github.com/evanw/unwind
Last synced: 9 months ago
JSON representation
Universal disassembler for Python bytecode (supports Python 2 and 3)
- Host: GitHub
- URL: https://github.com/evanw/unwind
- Owner: evanw
- Created: 2011-10-13T21:34:46.000Z (about 14 years ago)
- Default Branch: master
- Last Pushed: 2011-10-18T06:03:55.000Z (about 14 years ago)
- Last Synced: 2025-03-17T12:02:08.700Z (9 months ago)
- Language: Python
- Homepage:
- Size: 360 KB
- Stars: 50
- Watchers: 6
- Forks: 4
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# unwind - A disassembler for Python bytecode
This module provides a universal disassembler that is able to disassemble *.pyc files from both Python 2 and Python 3. Example usage:
import unwind
print(unwind.disassemble('example.pyc'))
The disassembler allows one version of Python to unmarshal code compiled by all other versions of Python. This is made possible by scraping information from the official Python repository at http://hg.python.org/cpython. In the example below, the code `print('Hello, World')` is compiled and disassembled from both Python 2.5 and Python 3.1. Notice how Python 2.5 uses the `PRINT_ITEM` opcode but Python 3.1 uses the `CALL_FUNCTION` opcode, since the print statement was removed in Python 3.
$ cat > example.py
print('Hello, World')
$ python2.5 -m py_compile example.py
$ python -c 'import unwind; print unwind.disassemble("example.pyc")'
Module(
magic = 168686259,
timestamp = 1318574250,
python_version = 'Python 2.6a0',
body = CodeObject(
co_argcount = 0,
co_kwonlyargcount = 0,
co_nlocals = 0,
co_stacksize = 1,
co_flags = 64,
co_filename = 'example.py',
co_name = '',
co_firstlineno = 1,
opcodes = [
Opcode(offset = 0, opcode = 'LOAD_CONST', argument = 'Hello, World'),
Opcode(offset = 3, opcode = 'PRINT_ITEM', argument = None),
Opcode(offset = 4, opcode = 'PRINT_NEWLINE', argument = None),
Opcode(offset = 5, opcode = 'LOAD_CONST', argument = None),
Opcode(offset = 8, opcode = 'RETURN_VALUE', argument = None)])))
$ python3.1 -m py_compile example.py
$ python -c 'import unwind; print unwind.disassemble("example.pyc")'
Module(
magic = 168627279,
timestamp = 1318574250,
python_version = 'Python 3.2a0',
body = CodeObject(
co_argcount = 0,
co_kwonlyargcount = 0,
co_nlocals = 0,
co_stacksize = 2,
co_flags = 64,
co_filename = u'example.py',
co_name = u'',
co_firstlineno = 1,
opcodes = [
Opcode(offset = 0, opcode = 'LOAD_NAME', argument = u'print'),
Opcode(offset = 3, opcode = 'LOAD_CONST', argument = u'Hello, World'),
Opcode(offset = 6, opcode = 'CALL_FUNCTION', argument = 1),
Opcode(offset = 9, opcode = 'POP_TOP', argument = None),
Opcode(offset = 10, opcode = 'LOAD_CONST', argument = None),
Opcode(offset = 13, opcode = 'RETURN_VALUE', argument = None)])))