PyCon JP 2024

Crimes with the Python syntax
2024-09-27 , 4F Track4

Over the past few years digging into Python's runtime and language features, I've often run into some weird, obscure parts of the language. Very weird and obscure parts.

Such as imports being hackable enough to directly import JSON files. Or being able to change the syntax of the language, at runtime.

And I'd note them down. All the weird, esoteric, not-to-be-named features of the language. And eventually, I'd find some way to abuse that feature. Do something heinous. Write code that would make any code reviewer question my sanity. Committing crimes, with the Python syntax.

This talk is an explainer of all these features, and why you should never use them. Unless, of course, you need to.


Outline:

  • Introduction (2 minutes)
  • The convoluted Python import machinery (5 minutes)
  • Frame-hacking, and the sys._getframe() API. (5 minutes)
  • Tracers, debuggers, and changing your code at runtime. (5 minutes)
  • Bonus: goto statements in Python? (3 minutes)
  • Creating our own mini language inside Python, at runtime. (7-8 minutes)
  • Abusing the Python startup machinery to run arbitrary code. (7-8 minutes)
  • The end, and questionnaire. (10 minutes)

Why did you choose this topic?

Python on the surface may seem like a very simple, beginner friendly language, but underneath the familiarity, it is an extremely powerful, complex tool. And learning about the inner workings of the interpreter, and how you can tweak it for your use cases allows you to build amazing Python projects and libraries.

Knowledges and know-how the audience can get from your talk

Learning about the interpreter, the stages it takes to read and run your code, and how you can modify each step of the process.

Prior knowledges speakers assume the audience to have

Understanding Python programs, and having at least a year or so of Python development experience would be great for following along.

Audience experiment

Advanced

Language of presentation

English

Language of presentation material

English

Long-term Python developer, author, OSS contributor and speaker, Tushar currently works as a Software Engineer building language tools at DeepSource.

When not working, he tries to contribute to the Python developer tools ecosystem, like black, ruff, flake8 and mypy.