~/talk

Exploring CPython's bytecode

Talkat EuroPython 2011 · 2011-06-20..06-26· [compilers-and-language-design, python]
slides:none on file

Walks the .pyc layout (magic number, timestamp, code object) and uses the standard library marshal and imp to read Python 3.2 bytecode, dis to inspect opcodes, then implements a tiny Python VM in Python that creates frame objects, an instruction pointer and a stack and executes LOAD_FAST/STORE_FAST, MAKE_FUNCTION, CALL_FUNCTION and arithmetic ops successfully. It skips defaults, keyword and keyword-only args, is intentionally slow but functional, and notes a hidden limit: bytecode argument encoding caps positional args at 255 arguments. VM loop and frame object implemented in pure Python.

AI-written summary · about

  1. 0:00Intro
  2. 1:26Unravelling Bytecode
  3. 2:32Python: Compiler & VM
  4. 3:45Python overview
  5. 4:21Example Module
  6. 5:07Executing the Bytecode
  7. 7:33Optimisation
  8. 8:55Structure of .pyc Files
  9. 10:07Magic Number
  10. 11:53Timestamp
  11. 12:16Code Object Marshal module
  12. 14:08Back to the Compiler Compile your own code object
  13. 16:28Code Objects It's documented: Data model (Language Reference) Everything which is executed becomes a code
  14. 19:21sum Code Object
  15. 20:59Disassembler
  16. 24:10First Taste of Bytecode (2) LOAD FAST
  17. 24:36First Model of the VM
  18. 26:04Reading Bytecode
  19. 27:07Bytecode Arguments dis HAVE ARGUMENT = 90
  20. 27:38First Eval Loop
  21. 29:25Passing Arguments
  22. 31:15Handling Bytecodes
  23. 31:32LOAD FAST LOAD CONST
  24. 31:57BINARY MULTIPLY
  25. 33:37BINARY ADD
  26. 34:01RETURN VALUE
  27. 34:24The VM So Far
  28. 34:48Disassembling the Module
  29. 38:22STORE NAME LOAD NAME
  30. 43:35CALL FUNCTION (2)
  31. 44:05POP JUMP_IF_FALSE
  32. 44:31JUMP FORWARD . POP TOP
  33. 45:14Incomplete
  34. 45:29EXTENDED ARG
  35. 46:00Abusing Bytecode
  36. 46:10The Tools
  37. 46:20Improve Tracing
  38. 46:58Questions
~/search
type to search 6,249 conferences, 138,987 speakers and 185,172 talks
~/help
cmdctrl k
search every conference, speaker and series
/
focus the filter search
h
home
c
conferences
s
speakers
e
series
t
light / dark — the status bar button does the same
?
this help
esc
close this, then clear every filter — or just the search, while you are typing in it

Ignored while you are typing. Pages without a filter rail have no search to focus.