SlideShare una empresa de Scribd logo
1 de 25
Descargar para leer sin conexión
The Python Interpreter is Fun and
 Not At All Terrifying: Opcodes
                              name: Alex Golec
                         twitter: @alexandergolec
                             not @alexgolec : (
  email: akg2136 (rhymes with cat) columbia dot (short for education)
                   this talk lives at: blog.alexgolec.com




                                                                        1
Python is Bytecode-Interpreted


•   Your python program is compiled down to bytecode

    •   Sort of like assembly for the python virtual machine

•   The interpreter executes each of these bytecodes one by one




                                                                  2
Before we Begin


•   This presentation was written using the CPython 2.7.2 which ships with
    Mac OS X Mountain Lion GM Image

•   The more adventurous among you will find that minor will details differ
    on PyPy / IronPython / Jython




                                                                             3
The Interpreter is Responsible For:

•   Issuing commands to objects and maintaining stack state

•   Flow Control

•   Managing namespaces

•   Turning code objects into functions and classes



                                                              4
Issuing Commands to Objects and
      Maintaining Stack State


                                  5
The dis Module
                          >>> def parabola(x):
                          ...     return x*x + 4*x + 4
                          ...
                          >>> dis.dis(parabola)
                            2           0 LOAD_FAST                  0 (x)
                                        3 LOAD_FAST                  0 (x)
                                        6 BINARY_MULTIPLY
                                        7 LOAD_CONST                 1 (4)
                                       10 LOAD_FAST                  0 (x)
                                       13 BINARY_MULTIPLY
                                       14 BINARY_ADD
                                       15 LOAD_CONST                 1 (4)
                                       18 BINARY_ADD
                                       19 RETURN_VALUE


Each instruction is exactly three bytes             Opcodes have friendly (ish) mnemonics


                                                                                            6
Example: Arithmetic Operations
>>> def parabola(x):
                                          •   We don’t know the type of x!
...
...
        return x*x + 4*x + 4
                                              •   How does BINARY_MULTIPLY
>>> dis.dis(parabola)
  2           0 LOAD_FAST         0 (x)
                                                  know how to perform
              3 LOAD_FAST         0 (x)           multiplication?
              6 BINARY_MULTIPLY


                                              •
              7 LOAD_CONST        1 (4)
             10 LOAD_FAST
             13 BINARY_MULTIPLY
                                  0 (x)           What is I pass a string?
             14 BINARY_ADD
             15 LOAD_CONST
             18 BINARY_ADD
                                  1 (4)
                                          •   Note the lack of registers; the
             19 RETURN_VALUE                  Python virtual machine is stack-
                                              based

                                                                                 7
Things the Interpreter Doesn’t Do:
     Typed Method Dispatch

•   The python interpreter does not know anything about how to add
    two numbers (or objects, for that matter)

•   Instead, it simply maintains a stack of objects, and when it comes time
    to perform an operation, asks them to perform the operation

•   The result gets pushed onto the stack



                                                                              8
Flow Control



               9
Flow Control
>>> def abs(x):
...
...
        if x < 0:
                 x = -x
                                                  •       Jumps can be relative or absolute
...
...
        return x

>>> dis.dis(abs)
                                                  •       Relevant opcodes:
  2           0 LOAD_FAST
              3 LOAD_CONST
                                      0 (x)
                                      1 (0)           •    JUMP_FORWARD

                                                      •
              6 COMPARE_OP            0 (<)
              9 POP_JUMP_IF_FALSE    22                    POP_JUMP_IF_[TRUE/FALSE]

  3          12   LOAD_FAST           0 (x)           •    JUMP_IF_[TRUE/FALSE]_OR_POP
             15   UNARY_NEGATIVE
             16   STORE_FAST          0 (x)           •    JUMP_ABSOLUTE
             19   JUMP_FORWARD        0 (to 22)
                                                      •    SETUP_LOOP
  4     >>   22 LOAD_FAST             0 (x)
             25 RETURN_VALUE                          •    [BREAK/CONTINUE]_LOOP


                                                                                              10
Managing Namespaces



                      11
Simple Namespaces
>>> def example():
...     variable = 1
...     def function():
...             print 'function'
...     del variable
...     del function
...
>>> dis.dis(example)
  2           0 LOAD_CONST         1 (1)           •   Variables, functions, etc. are all
              3 STORE_FAST         0 (variable)        treated identically
  3           6 LOAD_CONST         2 (<code object b at 0x10c545930, file "<stdin>", line 3>)

                                                  •
              9 MAKE_FUNCTION      0
             12 STORE_FAST         1 (function)        Once the name is assigned to the
  5          15 DELETE_FAST        0 (variable)        object, the interpreter completely
  6          18 DELETE_FAST        1 (function)
                                                       forgets everything about it except
             21 LOAD_CONST         0 (None)            the name
             24 RETURN_VALUE

                                                                                                12
Turning Code Objects into
  Functions and Classes


                            13
Functions First!
>>> def square(inputfunc):
...     def f(x):
...              return inputfunc(x) * inputfunc(x)
...     return f
...
>>> dis.dis(square)
  2           0 LOAD_CLOSURE              0 (inputfunc)
              3 BUILD_TUPLE               1
              6 LOAD_CONST                1 (<code object f at 0x10c545a30, file "<stdin>", line 2>)
              9 MAKE_CLOSURE              0

                                                         •
             12 STORE_FAST                1 (f)
                                                             The compiler generates code
  4          15 LOAD_FAST                1 (f)
             18 RETURN_VALUE                                 objects and sticks them in
                                                             memory


                                                                                                       14
Now Classes!
>>> def make_point(dimension, names):
...     class Point:
...             def __init__(self, *data):
...                     pass
...             dimension = dimensions
...     return Point
...
>>> dis.dis(make_point)
  2           0 LOAD_CONST               1   ('Point')
              3 LOAD_CONST               3   (())
              6 LOAD_CONST               2   (<code object Point at 0x10c545c30, file "<stdin>", line 2>)
              9 MAKE_FUNCTION            0
             12 CALL_FUNCTION            0
             15 BUILD_CLASS                             BUILD_CLASS()
             16 STORE_FAST               2   (Point)

  6          19 LOAD_FAST                2 (Point)      Creates a new class object. TOS is the methods
             22 RETURN_VALUE                            dictionary, TOS1 the tuple of the names of the base
                                                        classes, and TOS2 the class name.



                                                                                                              15
Other Things


•   Exceptions

•   Loops

    •   Technically flow control, but they’re a little more involved




                                                                      16
Now, We Have Some Fun



                        17
What to Do With Our Newly
Acquired Knowledge of Dark
          Magic?


                             18
Write your own Python
     interpreter!


                        19
Static Code Analysis!



                        20
Understand How PyPy Does It!



                               21
Buy Me Alcohol!
Or at least provide me with pleasant conversation




                                                    22
Slideshare-only Bonus Slide:
    Exception Handling!


                               23
>>> def list_get(lst, pos):                                    •   The exception context is pushed by
...     try:                                                       SETUP_EXCEPT
...
...
                return lst[pos]
        except IndexError:                                         •   If an exception is thrown, control jumps to the
...             return None
                                                                       address of the top exception context, in this case
...     # there is an invisible “return None” here                     opcode 15
>>> dis.dis(list_get)
  2           0 SETUP_EXCEPT            12 (to 15)                 •   If there is no top exception context, the
                                                                       interpreter halts and notifies you of the error
  3           3
              6
                  LOAD_FAST
                  LOAD_FAST
                                         0 (lst)
                                         1 (pos)
                                                               •   The yellow opcodes check if the exception thrown
                                                                   matches the type of the one in the except
              9   BINARY_SUBSCR
             10   RETURN_VALUE                                     statement, and execute the except block
             11
             12
                  POP_BLOCK
                  JUMP_FORWARD          18 (to 33)             •   At END_FINALLY, the interpreter is responsible for
                                                                   popping the exception context, and either re-raising
  4     >>   15   DUP_TOP                                          the exception, in which case the next-topmost
             16   LOAD_GLOBAL            0 (IndexError)            exception context will trigger, or returning from the
             19   COMPARE_OP            10 (exception match)       function
             22   POP_JUMP_IF_FALSE     32
             25   POP_TOP                                      •   Notice that the red opcodes will never be executed
             26
             27
                  POP_TOP
                  POP_TOP                                          •   The first: between a return and a jump target
                                                                   •   The second: only reachable by jumping from dead
  5          28   LOAD_CONST             0 (None)                      code.
             31   RETURN_VALUE
        >>   32   END_FINALLY                                      •   CPython’s philosophy of architectural and
        >>   33   LOAD_CONST             0 (None)                      implementation simplicity tolerates such minor
             36   RETURN_VALUE                                         inefficiencies

                                                                                                                            24
Thanks!



          25

Más contenido relacionado

Destacado (6)

Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPython
Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPythonByterun, a Python bytecode interpreter - Allison Kaptur at NYCPython
Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPython
 
Infecting Python Bytecode
Infecting Python BytecodeInfecting Python Bytecode
Infecting Python Bytecode
 
"A 1,500 line (!!) switch statement powers your Python!" - Allison Kaptur, !!...
"A 1,500 line (!!) switch statement powers your Python!" - Allison Kaptur, !!..."A 1,500 line (!!) switch statement powers your Python!" - Allison Kaptur, !!...
"A 1,500 line (!!) switch statement powers your Python!" - Allison Kaptur, !!...
 
GRUPPO 5: La pubblicità è un gioco (di parole)
GRUPPO 5: La pubblicità è un gioco (di parole)GRUPPO 5: La pubblicità è un gioco (di parole)
GRUPPO 5: La pubblicità è un gioco (di parole)
 
Il linguaggio pubblicitario
Il linguaggio pubblicitarioIl linguaggio pubblicitario
Il linguaggio pubblicitario
 
AngularJS Security: defend your Single Page Application
AngularJS Security: defend your Single Page Application AngularJS Security: defend your Single Page Application
AngularJS Security: defend your Single Page Application
 

Similar a Python opcodes

Bytes in the Machine: Inside the CPython interpreter
Bytes in the Machine: Inside the CPython interpreterBytes in the Machine: Inside the CPython interpreter
Bytes in the Machine: Inside the CPython interpreter
akaptur
 
C++totural file
C++totural fileC++totural file
C++totural file
halaisumit
 

Similar a Python opcodes (20)

Diving into byte code optimization in python
Diving into byte code optimization in python Diving into byte code optimization in python
Diving into byte code optimization in python
 
Pydiomatic
PydiomaticPydiomatic
Pydiomatic
 
Python idiomatico
Python idiomaticoPython idiomatico
Python idiomatico
 
Bytes in the Machine: Inside the CPython interpreter
Bytes in the Machine: Inside the CPython interpreterBytes in the Machine: Inside the CPython interpreter
Bytes in the Machine: Inside the CPython interpreter
 
Exploiting Concurrency with Dynamic Languages
Exploiting Concurrency with Dynamic LanguagesExploiting Concurrency with Dynamic Languages
Exploiting Concurrency with Dynamic Languages
 
Faster Python, FOSDEM
Faster Python, FOSDEMFaster Python, FOSDEM
Faster Python, FOSDEM
 
Android UI Tips, Tricks and Techniques
Android UI Tips, Tricks and TechniquesAndroid UI Tips, Tricks and Techniques
Android UI Tips, Tricks and Techniques
 
Android UI Development: Tips, Tricks, and Techniques
Android UI Development: Tips, Tricks, and TechniquesAndroid UI Development: Tips, Tricks, and Techniques
Android UI Development: Tips, Tricks, and Techniques
 
From Java to Scala - advantages and possible risks
From Java to Scala - advantages and possible risksFrom Java to Scala - advantages and possible risks
From Java to Scala - advantages and possible risks
 
ScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin OderskyScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin Odersky
 
Engineering Fast Indexes for Big-Data Applications: Spark Summit East talk by...
Engineering Fast Indexes for Big-Data Applications: Spark Summit East talk by...Engineering Fast Indexes for Big-Data Applications: Spark Summit East talk by...
Engineering Fast Indexes for Big-Data Applications: Spark Summit East talk by...
 
Engineering fast indexes
Engineering fast indexesEngineering fast indexes
Engineering fast indexes
 
Engineering Fast Indexes for Big-Data Applications: Spark Summit East talk by...
Engineering Fast Indexes for Big-Data Applications: Spark Summit East talk by...Engineering Fast Indexes for Big-Data Applications: Spark Summit East talk by...
Engineering Fast Indexes for Big-Data Applications: Spark Summit East talk by...
 
Engineering fast indexes (Deepdive)
Engineering fast indexes (Deepdive)Engineering fast indexes (Deepdive)
Engineering fast indexes (Deepdive)
 
Yin Yangs of Software Development
Yin Yangs of Software DevelopmentYin Yangs of Software Development
Yin Yangs of Software Development
 
What's New In Python 2.6
What's New In Python 2.6What's New In Python 2.6
What's New In Python 2.6
 
C++totural file
C++totural fileC++totural file
C++totural file
 
Options and trade offs for parallelism and concurrency in Modern C++
Options and trade offs for parallelism and concurrency in Modern C++Options and trade offs for parallelism and concurrency in Modern C++
Options and trade offs for parallelism and concurrency in Modern C++
 
Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014
Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014
Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014
 
C++ tutorial
C++ tutorialC++ tutorial
C++ tutorial
 

Último

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Último (20)

08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 

Python opcodes

  • 1. The Python Interpreter is Fun and Not At All Terrifying: Opcodes name: Alex Golec twitter: @alexandergolec not @alexgolec : ( email: akg2136 (rhymes with cat) columbia dot (short for education) this talk lives at: blog.alexgolec.com 1
  • 2. Python is Bytecode-Interpreted • Your python program is compiled down to bytecode • Sort of like assembly for the python virtual machine • The interpreter executes each of these bytecodes one by one 2
  • 3. Before we Begin • This presentation was written using the CPython 2.7.2 which ships with Mac OS X Mountain Lion GM Image • The more adventurous among you will find that minor will details differ on PyPy / IronPython / Jython 3
  • 4. The Interpreter is Responsible For: • Issuing commands to objects and maintaining stack state • Flow Control • Managing namespaces • Turning code objects into functions and classes 4
  • 5. Issuing Commands to Objects and Maintaining Stack State 5
  • 6. The dis Module >>> def parabola(x): ... return x*x + 4*x + 4 ... >>> dis.dis(parabola) 2 0 LOAD_FAST 0 (x) 3 LOAD_FAST 0 (x) 6 BINARY_MULTIPLY 7 LOAD_CONST 1 (4) 10 LOAD_FAST 0 (x) 13 BINARY_MULTIPLY 14 BINARY_ADD 15 LOAD_CONST 1 (4) 18 BINARY_ADD 19 RETURN_VALUE Each instruction is exactly three bytes Opcodes have friendly (ish) mnemonics 6
  • 7. Example: Arithmetic Operations >>> def parabola(x): • We don’t know the type of x! ... ... return x*x + 4*x + 4 • How does BINARY_MULTIPLY >>> dis.dis(parabola) 2 0 LOAD_FAST 0 (x) know how to perform 3 LOAD_FAST 0 (x) multiplication? 6 BINARY_MULTIPLY • 7 LOAD_CONST 1 (4) 10 LOAD_FAST 13 BINARY_MULTIPLY 0 (x) What is I pass a string? 14 BINARY_ADD 15 LOAD_CONST 18 BINARY_ADD 1 (4) • Note the lack of registers; the 19 RETURN_VALUE Python virtual machine is stack- based 7
  • 8. Things the Interpreter Doesn’t Do: Typed Method Dispatch • The python interpreter does not know anything about how to add two numbers (or objects, for that matter) • Instead, it simply maintains a stack of objects, and when it comes time to perform an operation, asks them to perform the operation • The result gets pushed onto the stack 8
  • 10. Flow Control >>> def abs(x): ... ... if x < 0: x = -x • Jumps can be relative or absolute ... ... return x >>> dis.dis(abs) • Relevant opcodes: 2 0 LOAD_FAST 3 LOAD_CONST 0 (x) 1 (0) • JUMP_FORWARD • 6 COMPARE_OP 0 (<) 9 POP_JUMP_IF_FALSE 22 POP_JUMP_IF_[TRUE/FALSE] 3 12 LOAD_FAST 0 (x) • JUMP_IF_[TRUE/FALSE]_OR_POP 15 UNARY_NEGATIVE 16 STORE_FAST 0 (x) • JUMP_ABSOLUTE 19 JUMP_FORWARD 0 (to 22) • SETUP_LOOP 4 >> 22 LOAD_FAST 0 (x) 25 RETURN_VALUE • [BREAK/CONTINUE]_LOOP 10
  • 12. Simple Namespaces >>> def example(): ... variable = 1 ... def function(): ... print 'function' ... del variable ... del function ... >>> dis.dis(example) 2 0 LOAD_CONST 1 (1) • Variables, functions, etc. are all 3 STORE_FAST 0 (variable) treated identically 3 6 LOAD_CONST 2 (<code object b at 0x10c545930, file "<stdin>", line 3>) • 9 MAKE_FUNCTION 0 12 STORE_FAST 1 (function) Once the name is assigned to the 5 15 DELETE_FAST 0 (variable) object, the interpreter completely 6 18 DELETE_FAST 1 (function) forgets everything about it except 21 LOAD_CONST 0 (None) the name 24 RETURN_VALUE 12
  • 13. Turning Code Objects into Functions and Classes 13
  • 14. Functions First! >>> def square(inputfunc): ... def f(x): ... return inputfunc(x) * inputfunc(x) ... return f ... >>> dis.dis(square) 2 0 LOAD_CLOSURE 0 (inputfunc) 3 BUILD_TUPLE 1 6 LOAD_CONST 1 (<code object f at 0x10c545a30, file "<stdin>", line 2>) 9 MAKE_CLOSURE 0 • 12 STORE_FAST 1 (f) The compiler generates code 4 15 LOAD_FAST 1 (f) 18 RETURN_VALUE objects and sticks them in memory 14
  • 15. Now Classes! >>> def make_point(dimension, names): ... class Point: ... def __init__(self, *data): ... pass ... dimension = dimensions ... return Point ... >>> dis.dis(make_point) 2 0 LOAD_CONST 1 ('Point') 3 LOAD_CONST 3 (()) 6 LOAD_CONST 2 (<code object Point at 0x10c545c30, file "<stdin>", line 2>) 9 MAKE_FUNCTION 0 12 CALL_FUNCTION 0 15 BUILD_CLASS BUILD_CLASS() 16 STORE_FAST 2 (Point) 6 19 LOAD_FAST 2 (Point) Creates a new class object. TOS is the methods 22 RETURN_VALUE dictionary, TOS1 the tuple of the names of the base classes, and TOS2 the class name. 15
  • 16. Other Things • Exceptions • Loops • Technically flow control, but they’re a little more involved 16
  • 17. Now, We Have Some Fun 17
  • 18. What to Do With Our Newly Acquired Knowledge of Dark Magic? 18
  • 19. Write your own Python interpreter! 19
  • 21. Understand How PyPy Does It! 21
  • 22. Buy Me Alcohol! Or at least provide me with pleasant conversation 22
  • 23. Slideshare-only Bonus Slide: Exception Handling! 23
  • 24. >>> def list_get(lst, pos): • The exception context is pushed by ... try: SETUP_EXCEPT ... ... return lst[pos] except IndexError: • If an exception is thrown, control jumps to the ... return None address of the top exception context, in this case ... # there is an invisible “return None” here opcode 15 >>> dis.dis(list_get) 2 0 SETUP_EXCEPT 12 (to 15) • If there is no top exception context, the interpreter halts and notifies you of the error 3 3 6 LOAD_FAST LOAD_FAST 0 (lst) 1 (pos) • The yellow opcodes check if the exception thrown matches the type of the one in the except 9 BINARY_SUBSCR 10 RETURN_VALUE statement, and execute the except block 11 12 POP_BLOCK JUMP_FORWARD 18 (to 33) • At END_FINALLY, the interpreter is responsible for popping the exception context, and either re-raising 4 >> 15 DUP_TOP the exception, in which case the next-topmost 16 LOAD_GLOBAL 0 (IndexError) exception context will trigger, or returning from the 19 COMPARE_OP 10 (exception match) function 22 POP_JUMP_IF_FALSE 32 25 POP_TOP • Notice that the red opcodes will never be executed 26 27 POP_TOP POP_TOP • The first: between a return and a jump target • The second: only reachable by jumping from dead 5 28 LOAD_CONST 0 (None) code. 31 RETURN_VALUE >> 32 END_FINALLY • CPython’s philosophy of architectural and >> 33 LOAD_CONST 0 (None) implementation simplicity tolerates such minor 36 RETURN_VALUE inefficiencies 24
  • 25. Thanks! 25