Playing with Python Bytecode

11:15 AM - 12:10 PM on July 16, 2016, Room CR4

Scott Sanderson and Joe Jevnik

Audience level:
advanced
Watch:
https://www.youtube.com/watch?v=rpZz_emWIQA

Description

Ever wondered what Python is actually doing when it executes your code? Want to learn to hand-craft artisanal Python bytecode? In this talk, we explain CPython's internal code representation, and we demonstrate techniques for modifying code objects for fun and profit.

Abstract

In this talk, we explain CPython's internal code representation, demonstrating techniques for modifying code objects. We break down the attributes of Python's code type, showing how one might construct a function or module "from scratch" without the help of the compiler. We then introduce techniques for manipulating the bytecode of existing code objects, which allows us express functions that can't be written directly in Python. We discuss some of the challenges that must be addressed to safely alter bytecode, and we conclude by demonstrating some of the toys we've built with codetransformer, our bytecode-manipulation library.