Created
July 31, 2021 14:40
-
-
Save darktohka/087866daab336d45419ee707fdf63265 to your computer and use it in GitHub Desktop.
Rewrite all .pyc files to use an older bytecode version (as long as the script is not using newer opcodes)
This file contains hidden or 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 os, struct | |
| # Last known working Python 3.8 version | |
| # Python 3.9a0 3420 (add LOAD_ASSERTION_ERROR #34880) | |
| # Python 3.9a0 3421 (simplified bytecode for with blocks #32949) | |
| # Python 3.9a0 3422 (remove BEGIN_FINALLY, END_FINALLY, CALL_FINALLY, POP_FINALLY bytecodes #33387) | |
| # Python 3.9a2 3423 (add IS_OP, CONTAINS_OP and JUMP_IF_NOT_EXC_MATCH bytecodes #39156) | |
| # Python 3.9a2 3424 (simplify bytecodes for *value unpacking) | |
| # Python 3.9a2 3425 (simplify bytecodes for **value unpacking) | |
| PYTHON38_VERSION = 3413 | |
| for root, _, files in os.walk('.'): | |
| for file in files: | |
| if not file.endswith('.pyc'): | |
| continue | |
| filename = os.path.join(root, file) | |
| with open(filename, 'rb') as f: | |
| version_magic = f.read(2) | |
| version = struct.unpack('<H', version_magic)[0] | |
| if version == PYTHON38_VERSION: | |
| continue | |
| data = f.read() | |
| version = struct.pack('<H', PYTHON38_VERSION) | |
| data = version + data | |
| with open(filename, 'wb') as f: | |
| print('Patching', filename) | |
| f.write(data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment