Python Byte Code Hacks

Vijay Kumar (~bravegnu)


13

Votes

Description:

Overview

Through this talk I hope to cover how the CPython interpreter works. Specifically we will be looking at how to decipher the generated code objects and the associated byte code, using a poke and learn approach.

Objective

The objective of the talk is to show people how interpreters in general and the CPython interpreter in particular, is implemented. While this is not really required for people to use the Python, it depeens people's understanding of how modern interpreted languages work.

Content Outline

  • Introduction to Virtual Machines - 5 min
    • Microprocessor code execution model
    • Introduction to Python interpreter code execution model
    • Stack based virtual machine
      • Code Objects - 15 min
    • Analyzing a Code Object
    • Overview of byte code Instructions
    • Deciphering the byte code string
    • Writing a simple byte code dis-assembler
      • Code Object Hacks - 15 min
    • Modifying code object constants
    • Modifying the byte code string
    • Writing code objects by hand
      • Questions - 5 min

Prerequisites:

  • Should be familiar with basic object oriented programming with Python.
  • Knowledge of microprocessors will be useful.

Content URLs:

Slides: https://www.dropbox.com/s/4pu5ko5uxpvxnzx/slides.html?dl=1 The slides are self-contained and have all the code embedded within them as data-uris.

The slides are distributed under CC-Attribution-ShareAlike.

Blog article on the subject: http://www.bravegnu.org/blog/python-byte-code-hacks.html

Content from previous talks I had done on the topic is available from https://github.com/bravegnu/python-byte-code/blob/master/slides.asciidoc

The talk itself derives inspiration from the Active State Recipe "The goto decorator". I wanted to take the ideas behind the recipe to a wider audience. I did a couple of talks on the recipe first, and found that people were excited, but found it hard to digest. Later on, I refined the talk to take a step-by-step approach, that provides the required background to understand the recipe, resulting in the current form of the talk.

Speaker Info:

I am an embedded software developer and trainer by profession. As part of my work at Zilogic Systems, I do training in various topics related to Embedded Systems, Linux and Python. I coordinate the activities of the Chennai Python User Group http://chennaipy.org In the past 5 years I have done over 50 talks at Chennaipy. My work was recognized by the Python Software Society of India with the Kenneth Gonsalves Award for the year 2015. I help promote usage of Linux in embedded systems through the meetup group http://meetup.com/embedded-linux by organizing workshops and coding dojos.

Links to recordings of previous talks at conferences / meetups:

  • I did a lightning talk at PyCon India 2015, titled "Bitten by Python". [Video Recording]
  • I did a full length talk at PyCon India 2010, titled "Device Interfacing with Python and ZIO": [Proposal]
  • Talk at Chennaipy, titled Managing Desktops with Ansible [Screencast]
  • Talk at Chennaipy, titled Hangman Design and Implementation [Screencast]

I have made contributions to the Linux Kernel, U-Boot, Qemu and the Linux Testing Project.

Speaker Links:

  • GitHub: http://github.org/bravegnu
  • LinkedIn: http://linkedin.com/in/bravegnu
  • Blog:
    • Personal: http://bravegnu.org
    • Company: http://www.zilogic.com/author/vijaykumar.html
  • Twitter: http://twitter.com/bravegnu
  • SlideShare: http://www.slideshare.net/VijayKumarBagavathSi/

Section: Core Python
Type: Talks
Target Audience: Intermediate
Last Updated: