CPython Guide

Introduction

This guide is a compilation of many sources of knowledge. I've watched countlesss
videos on YouTube of professors and past PyCon talks, read endless documentation
and blog posts, and written hundreds of print statements to track the process of
CPython's execution (which I highly recommend doing). These experiences have
highly influenced my approach to this document, as well as having provided me
with a solid foundation to jump in to CPython's source code with two feet.

Who is this guide for?

Ultimately, this guide is for anyone with a desire to contribute to CPython or anyone
who wants to learn more about how Python works. That said, there may be sections that
require additional knowledge or skill sets. For example, if you wish to contribute to
the C portions of Python, I would highly recommend a primer on C.

What this guide will cover

This guide will cover
  • Basics of writing a language (VERY high level)
  • How the parser works (+)
  • How to define an object type (or is this in the C/API docs already?) (+)
  • Tools and Common Patterns
  • Lesser known C Patterns used (+)
  • Python conventions
  • Overview of code structure (important directories, what they do)
  • Overview of Python's Interpreter
  • Bytecode*
  • Reference counting*
  • Overview of code interactions/flow (who calls what, what compiles what, etc)
  • Tools and tricks for learning
  • Modules (e.g. dis, .func_cod)
  • Strategies
  • How to find the definition of something (+)
  • How to find where a given error comes from (+)
  • TAGS
  • Tips for contributing
  • Additional random topics
  • The GIL (Larry's Gilectomy notwithstanding)
  • Open hashing
  • Low-level memory allocation
  • Memory locality
  • How to handle errors (e.g. when to use a goto, making sure you deref appropriately)
  • Sources
  • Further reading

What this guide WON'T cover

This guide will not cover anything that is included in the Python Dev Guide <https://docs.python.org/devguide/>_, as that guide is extremely well written and maintained.
Topics that I find particularly useful from the Dev Guide include:
Quick Start <https://docs.python.org/devguide/index.html#quick-start>_
Contributing <https://docs.python.org/devguide/index.html#contributing>_
Writing and Running Tests <https://docs.python.org/devguide/runtests.html>_
Documenting Python <https://docs.python.org/devguide/documenting.html>_
Working wtih Mercurial <https://docs.python.org/devguide/committing.html#working-with-mercurial>_ and Mercurial for git developers <https://docs.python.org/devguide/gitdevs.html>_
Development Cycle <https://docs.python.org/devguide/devcycle.html>_