Python bytecode tools
Project description
Bytecode Tools
Bytecode tools are combination of multiple necessary modules to play with Python bytecode. Most of the bytecode related modules are version specific and they won't support other python versions.
Bytecode won't be same across versions, new opcodes will be added or opcodes will be removed or modified, so python standard library modules won't work with other version generated bytecodes.
Let's say marshal module, it's purpose is to serialize or deserialize code objects, they are version specific. Like wise dis module, there is a heavy difference in disassembling bytecode to wordcode AND opcode differences make this version incompatible.
Our goal is to make bytecode_tools work with any Cpython version, Aim is to build below tools.
| Tool | Purpose |
|---|---|
| unmarshal | deserialize the code obejcts. |
| pydis | Disassembler for any Cpython version. |
| pycdecode | Decoder for bytecode chache files (pyc files) |
| hackdis | Hack the disassembled bytecode |
| decompiler | A decompiler for Cpython x.x |
How to deal with bytecode_tools?
Install
-
Clone this repo and
python setup.py installor usepip install . -
Install directly from PyPi
pip install bytecode-tools
Usage
pydis
What is pydis?
pydis is a python disassembler, it can be a drop in replacement for cpython's
Lib/dis.py.
Pydis supports all the cpython versions above 2.5, every verion above 2.5 supports other versions. This means, pydis decodes 2.6 bytes code in 3.6 and vice versa.
Why pydis?
Python's dis moduel is super helpful for looking inside code objects, but it
won't support other python versions. If the code object is created through
python 3.5 and try to disassemble with python3.6, it won't work.
Each python version gets changes to opcodes, there will be ne ones added and few are deleted. Unless you recreate the code object with new python version, the same code object can't be interpreted with old versions.
Disassemble a statement.
>>> from bytecode_tools import pydis
>>> pydis.dis("a=1")
1 0 LOAD_CONST 0 (1)
2 STORE_NAME 0 (a)
4 LOAD_CONST 1 (None)
6 RETURN_VALUE
Disassble a function object.
>>> def foo():
print(123)
a = 1
b = 2
c = a + b
return c
>>> pydis.dis(foo)
2 0 LOAD_GLOBAL 0 (print)
2 LOAD_CONST 1 (123)
4 CALL_FUNCTION 1
6 POP_TOP
3 8 LOAD_CONST 2 (1)
10 STORE_FAST 0 (a)
4 12 LOAD_CONST 3 (2)
14 STORE_FAST 1 (b)
5 16 LOAD_FAST 0 (a)
18 LOAD_FAST 1 (b)
20 BINARY_ADD
22 STORE_FAST 2 (c)
6 24 LOAD_FAST 2 (c)
26 RETURN_VALUE
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file bytecode_tools-0.0.2.tar.gz.
File metadata
- Download URL: bytecode_tools-0.0.2.tar.gz
- Upload date:
- Size: 34.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.4.0 requests-toolbelt/0.9.1 tqdm/4.31.0 CPython/3.6.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
37f1b5180bdfdc356666ddf6d60c6f58b7a7984f29130eb8e9d830992aca375d
|
|
| MD5 |
87da9a536d5d4e9cb2190b3e707e1265
|
|
| BLAKE2b-256 |
f9631d316cf429088799672b4a2ceb55454a1eb2fe267283af4d91db3c06c8a9
|
File details
Details for the file bytecode_tools-0.0.2-py3-none-any.whl.
File metadata
- Download URL: bytecode_tools-0.0.2-py3-none-any.whl
- Upload date:
- Size: 48.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.4.0 requests-toolbelt/0.9.1 tqdm/4.31.0 CPython/3.6.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a58045a7290396c46a21d620d26198530d58814b0341669b54d8cf641644229a
|
|
| MD5 |
073388805914558f9eba92cb245deb42
|
|
| BLAKE2b-256 |
dc1ea211b82078564f1f74eae455029f3d2fa959968c3856f57c70d7fbd7f6e3
|