Bytecode and .pyc files
summary
Deep dive into CPython bytecode and .pyc internals: how co_code, co_consts and co_varnames map to evaluation stack operations and instructions like LOAD_CONST, LOAD_GLOBAL, CALL_FUNCTION and BINARY_OP; how .pyc headers store a magic number and timestamp (or hashed variant) and why __pycache__ names include the Python version; tools shown include dis and dis.get_instructions, marshal for reading code objects, and creating functions with FunctionType and code.replace to patch one byte and change an operator — the edit works and executes.
AI-written summary · about
chapters (13)
- 0:00Introduction
- 1:11What is bytecode
- 3:22Bytecode vs machine code
- 4:31Inspecting bytecode
- 8:24Stacks in execution
- 17:27Why .pyc files matter
- 19:58Optimizing with .pyc files
- 21:03Inside .pyc files
- 22:51Bytecode optimization levels
- 23:34Exception handling in bytecode
- 24:18Dynamic function creation
- 28:15Practical bytecode tricks
- 29:51Q&A
related talks
50mExploring CPython's bytecode
50mExploring CPython's bytecode
22mLow Level Python a Deep dive into the python sources
01h 21mExploring Python Code Objects
34mCPython internals and the VM
17mInside a PyPy JIT: is that even legal?
42mOptimizing Code Performance for Python Internals
33mLinear Genetic Programming in Python Bytecode
