Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Allison Kaptur - Bytes in the Machine: Inside t...

Allison Kaptur - Bytes in the Machine: Inside the CPython interpreter

Have you ever wondered how the CPython interpreter works? Do you know where to find a 1,500 line switch statement in CPython? I'll talk about the structure of the interpreter that we all use every day by explaining how Ned Batchelder and I chased down a mysterious bug in Byterun, a Python interpreter written in Python. We'll also see visualizations of the VM as it executes your code.

https://us.pycon.org/2015/schedule/presentation/420/

PyCon 2015

April 18, 2015
Tweet

More Decks by PyCon 2015

Other Decks in Programming

Transcript

  1. Byterun with Ned Batchelder Based on # pyvm2 by Paul

    Swartz (z3p) from http://www.twistedmatrix.com/users/ z3p/
  2. Testing def test_for_loop(self): self.assert_ok("""\ out = "" for i in

    range(5): out = out + str(i) print(out) """)
  3. A problem def test_for_loop(self): self.assert_ok("""\ g = (x*x for x

    in range(5)) h = (y+1 for y in g) print(list(h)) """)
  4. A simple VM what_to_execute = { "instructions": [("LOAD_VALUE", 0), ("LOAD_VALUE",

    1), ("ADD_TWO_VALUES", None), ("PRINT_ANSWER", None)], "numbers": [7, 5] }
  5. class Interpreter(object): def __init__(self): self.stack = [] def value_loader(self, number):

    self.stack.append(number) def answer_printer(self): answer = self.stack.pop() print(answer) def two_value_adder(self): first_num = self.stack.pop() second_num = self.stack.pop() total = first_num + second_num self.stack.append(total)
  6. def run_code(self, what_to_execute): instrs = what_to_execute["instructions"] numbers = what_to_execute["numbers"] for

    each_step in instrs: instruction, argument = each_step if instruction == "LOAD_VALUE": number = numbers[argument] self.value_loader(number) elif instruction == "ADD_TWO_VALUES": self.two_value_adder() elif instruction == "PRINT_ANSWER": self.answer_printer() interpreter = Interpreter() interpreter.run_code(what_to_execute) # 12
  7. Bytecode: it’s bytes! Function Code object Bytecode >>> def mod(a,

    b): ... ans = a % b ... return ans >>> mod.func_code.co_code
  8. Bytecode: it’s bytes! >>> def mod(a, b): ... ans =

    a % b ... return ans >>> mod.func_code.co_code '|\x00\x00| \x01\x00\x16}\x02\x00|\x02\x00S'
  9. Bytecode: it’s bytes! >>> def mod(a, b): ... ans =

    a % b ... return ans >>> mod.func_code.co_code ‘|\x00\x00| \x01\x00\x16}\x02\x00|\x02\x00S' >>> [ord(b) for b in mod.func_code.co_code] [124, 0, 0, 124, 1, 0, 22, 125, 2, 0, 124, 2, 0, 83]
  10. dis, a bytecode disassembler >>> import dis >>> dis.dis(mod) 2

    0 LOAD_FAST 0 (a) 3 LOAD_FAST 1 (b) 6 BINARY_MODULO 7 STORE_FAST 2 (ans) 3 10 LOAD_FAST 2 (ans) 13 RETURN_VALUE
  11. dis, a bytecode disassembler >>> dis.dis(mod) line ind name arg

    hint 2 0 LOAD_FAST 0 (a) 3 LOAD_FAST 1 (b) 6 BINARY_MODULO 7 STORE_FAST 2 (ans) 3 10 LOAD_FAST 2 (ans) 13 RETURN_VALUE
  12. Bytecode: it’s bytes! >>> def mod(a, b): ... ans =

    a % b ... return ans >>> mod(7,5)
  13. 7 5 2 Before After BINARY_ MODULO After LOAD_ FAST

    The Python interpreter After STORE_ FAST
  14. >>> def mod(a, b): ... ans = a % b

    ... return ans >>> mod(7,5) >>> dis.dis(mod) 2 0 LOAD_FAST 0 (a) 3 LOAD_FAST 1 (b) 6 BINARY_MODULO 7 STORE_FAST 2 (ans) 3 10 LOAD_FAST 2 (ans) 13 RETURN_VALUE
  15. c data stack -> a l l s t a

    c k Frame: main Frame: mod 7 5
  16. Frame: main Frame: fact >>> def fact(n): ... if n

    < 2: return 1 ... else: return n * fact(n-1) >>> fact(3) 3 3 fact 1
  17. Frame: main Frame: fact >>> def fact(n): ... if n

    < 2: return 1 ... else: return n * fact(n-1) >>> fact(3) 3 2 fact
  18. Frame: main Frame: fact >>> def fact(n): ... if n

    < 2: return 1 ... else: return n * fact(n-1) >>> fact(3) 3 Frame: fact 2
  19. Frame: main Frame: fact 3 Frame: fact 2 1 >>>

    def fact(n): ... if n < 2: return 1 ... else: return n * fact(n-1) >>> fact(3)
  20. Frame: main Frame: fact 3 2 >>> def fact(n): ...

    if n < 2: return 1 ... else: return n * fact(n-1) >>> fact(3)
  21. Frame: main Frame: fact 6 >>> def fact(n): ... if

    n < 2: return 1 ... else: return n * fact(n-1) >>> fact(3)
  22. Frame: main Frame: fact 6 >>> def fact(n): ... if

    n < 2: return 1 ... else: return n * fact(n-1) >>> fact(3)
  23. Python VM: - A collection of frames - Data stacks

    on frames - A way to run frames
  24. >>> import dis >>> dis.dis(mod) 2 0 LOAD_FAST 0 (a)

    3 LOAD_FAST 1 (b) 6 BINARY_MODULO 7 STORE_FAST 2 (ans) 3 10 LOAD_FAST 2 (ans) 13 RETURN_VALUE Instructions we need
  25. #ifdef CASE_TOO_BIG default: switch (opcode) { #endif /* Turn this

    on if your compiler chokes on the big switch: */ /* #define CASE_TOO_BIG 1 */
  26. Instructions we need >>> import dis >>> dis.dis(mod) 2 0

    LOAD_FAST 0 (a) 3 LOAD_FAST 1 (b) 6 BINARY_MODULO 7 STORE_FAST 2 (ans) 3 10 LOAD_FAST 2 (ans) 13 RETURN_VALUE
  27. case LOAD_FAST: x = GETLOCAL(oparg); if (x != NULL) {

    Py_INCREF(x); PUSH(x); goto fast_next_opcode; } format_exc_check_arg(PyExc_UnboundLocalError, UNBOUNDLOCAL_ERROR_MSG, PyTuple_GetItem(co->co_varnames, oparg)); break;
  28. case BINARY_MODULO: w = POP(); v = TOP(); if (PyString_CheckExact(v))

    x = PyString_Format(v, w); else x = PyNumber_Remainder(v, w); Py_DECREF(v); Py_DECREF(w); SET_TOP(x); if (x != NULL) continue; break;
  29. Back to our problem g = (x*x for x in

    range(5)) h = (y+1 for y in g) print(list(h))
  30. It’s “dynamic” >>> def mod(a, b): ... ans = a

    % b ... return ans >>> mod(15, 4) 3
  31. “Dynamic” >>> def mod(a, b): ... ans = a %

    b ... return ans >>> mod(15, 4) 3 >>> mod(“%s%s”, (“Py”, “Con”))
  32. “Dynamic” >>> def mod(a, b): ... ans = a %

    b ... return ans >>> mod(15, 4) 3 >>> mod(“%s%s”, (“Py”, “Con”)) PyCon
  33. “Dynamic” >>> def mod(a, b): ... ans = a %

    b ... return ans >>> mod(15, 4) 3 >>> mod(“%s%s”, (“Py”, “Con”)) PyCon >>> print “%s%s” % (“Py”, “Con”) PyCon
  34. dis, a bytecode disassembler >>> import dis >>> dis.dis(mod) 2

    0 LOAD_FAST 0 (a) 3 LOAD_FAST 1 (b) 6 BINARY_MODULO 7 STORE_FAST 2 (ans) 3 10 LOAD_FAST 2 (ans) 13 RETURN_VALUE
  35. case BINARY_MODULO: w = POP(); v = TOP(); if (PyString_CheckExact(v))

    x = PyString_Format(v, w); else x = PyNumber_Remainder(v, w); Py_DECREF(v); Py_DECREF(w); SET_TOP(x); if (x != NULL) continue; break;
  36. >>> class Surprising(object): … def __mod__(self, other): … print “Surprise!”

    >>> s = Surprising() >>> t = Surprsing() >>> s % t Surprise!
  37. “In the general absence of type information, almost every instruction

    must be treated as INVOKE_ARBITRARY_METHOD.” - Russell Power and Alex Rubinsteyn, “How Fast Can We Make Interpreted Python?”
  38. More Great blogs http://tech.blog.aknin.name/category/my- projects/pythons-innards/ by @aknin http://eli.thegreenplace.net/ by Eli

    Bendersky Contribute! Find bugs! https://github.com/nedbat/byterun Apply to the Recurse Center! www.recurse.com/apply