Replacing `import` with `accio`: Compiling Pythons with Custom Grammar for the sake of a joke

*
Accepted Session
Short Form
Beginner
Scheduled: Thursday, June 26, 2014 from 3:45 – 4:30pm in B301

Excerpt

In Python, overwriting builtin functions is fairly easy. You can even do it in the interpreter! But can you overwrite a statement, like import, just as easily? Let's go on an adventure, discovering how the import statement works, and how Python statements are defined in the CPython source code. We'll face some consequences of bootstrapping, and, to get our custom Harry Potter-themed Grammar to work, we'll have to compile a Python to compile a Python.

Description

In Python, overwriting builtin functions is fairly easy. You can even do it in the interpreter! But can you overwrite a statement, like import, just as easily? Let’s go on an adventure, discovering how the import statement works, and how Python statements are defined in the CPython source code. We’ll face some consequences of bootstrapping, and, to get our custom Harry Potter-themed Grammar to work, we’ll have to compile a Python to compile a Python.

Format:

  • Builtins: What’s in the `builtin` module and can we do mischievous things like shadow or delete builtins?
  • Import: Can we shadow/delete the import statement? How does the import statement work? Is it a builtin?
  • Grammar: Where is the import statement defined? We’ll look at CPython’s Grammar file to figure out what it means to be an import statement, and we’ll try editing the Grammar file to create an accio statement instead.
  • Bootstrapping: When we compile our new Python (with accio instead of import), we’ll get lots of errors! Why? It turns out about half of CPython is written in Python, so compiling Python requires Python on your computer! Mind explodes! We’ll come face-to-face with the concept of bootstrapping, and have to compile a Python to compile another Python.

A note on Audience Level:
The audience will get the most out of the talk if they’re familiar with basic Python syntax, like

>>> import random
>>> random.random()
>>> dir(random)

This talk is in part intended to demystify CPython source code so that even beginners feel comfortable cloning it and fiddling around.

For further reading, here’s the blog post I wrote that was the inspiration for this talk: http://mathamy.com/import-accio-bootstrapping-python-grammar.html

Tags

python, cpython, harry potter

Speaking experience

I've presented this talk at Hacker School, NYC PyLadies, and NYC Hack and Tell. I've also given internal talks at my previous company, Adlucent.

The slides for the talk (which are still being updated as I get more feedback) can be found here: http://www.slideshare.net/AmyHanlon/slides-32517750

Speaker