Skip to content
HN On Hacker News ↗

Formalizing CPython

▲ 137 points • 66 comments • by carnevalem • 2w ago • HN discussion ↗

Pangram verdict · v3.3

We believe that this text is a mix of AI and human-written content.

66 %

AI likelihood · overall

Mixed
34% human-written 66% AI-generated
SEGMENTS · HUMAN 3 of 22
SEGMENTS · AI 19 of 22
WORD COUNT 1,703
PEAK AI % 100% · §1
Analyzed
Nov 10
backend: pangram/v3.3
Segments scanned
22 windows
avg 77 words each
Distribution
34 / 66%
human / AI fraction
Verdict
Mixed
Pangram v3.3

Article text · 1,703 words · 22 segments analyzed

Human AI-generated
§1 AI · 100%

AI agents wrote 613,318 lines of Lean that copy Python's internals line by line, tested every piece against the real Python, had a computer check every proof, and turned up 418 bug reports in Python along the way.

§2 Human · 0%

CPython · Objects/rangeobject.c static unsigned longget_len_of_range(long lo, long hi, long step){ assert(step != 0); if (step > 0 && lo < hi) return 1UL + (hi - 1UL - lo) / step; else if (step < 0 && lo > hi) return 1UL + (lo - 1UL - hi) / (0UL - step); else return 0UL;} Lean copy · RangeSlice/Model.lean def getLenOfRange (lo hi step : Int) : Int := if step > 0 ∧ lo < hi then uadd 1 (usub (usub (toU hi) 1) (toU lo) / toU step) else if step < 0 ∧ lo > hi then uadd 1 (usub (usub (toU lo) 1) (toU hi) / usub 0 (toU step)) else 0 -- step ≠ 0 is assumed by every proof-- uadd, usub: C's unsigned arithmetic,-- which wraps around past 2^64 A function from CPython's C source (left) and the Lean copy an agent wrote of it (right).

§3 AI · 100%

Hover over a line to see its counterpart.

§4 AI · 100%

TL;DR We pointed Opus 5.5 at CPython, the program that runs almost all Python code, and had it rewrite its functions in Lean, a language for writing machine-checked proofs. Each rewrite copies the original line by line and is tested against real Python on tens of thousands of inputs before anyone tries to prove anything about it. A function only counts once a second agent, which didn't write it, checks the work and agrees it holds up. 4,071 functions have passed that check so far. The result is 613,318 lines of Lean across 1,539 files, with 18,898 theorems and lemmas, all checked by Lean's proof checker on every build. Along the way the proofs turned up 418 bug reports. Most are small, but about 55 can crash the interpreter or corrupt memory from ordinary Python code. Checked and acceptedWritten, not yet acceptedNot started All of CPython16%Interpreter core10%Built-in types35%C modules19%Parser31%Grammar rules100%Standard library9% How much of each part of CPython 3.15.2 has been copied, tested, proved and accepted by review. Code with nothing to check, such as memory bookkeeping, is left out. Why bother Python doesn't have a precise written definition. There is a reference manual, but in practice "Python" means whatever CPython does, and CPython is hundreds of thousands of lines of C plus a large standard library. The manual covers normal use. It says little about the odd cases, and that's where bugs hide: an object that changes a dictionary while that dictionary is searching for it, an error thrown halfway through an update, a number one past the largest the C code was written for. Writing a proof about a function forces you to say exactly what it does in all of those cases. You can't leave anything vague, because the proof checker won't accept it. That makes proofs a good way to find the places where the code doesn't do what everyone assumes. We wanted to see how far this goes on a code base as large and old as CPython, and whether the results stay trustworthy when most of the work is done by AI. How it works Every function goes through the same four steps. CopyRewrite the function in Lean, following the original code line by line, including its error messages and its odd corners. TestRun the copy and the real Python side by side on at least 50,000 tricky inputs. Any disagreement means the copy is wrong. ProveState something that should always be true and have the computer check the proof. If it's actually false, prove that instead. CheckA second agent that didn't do the work reviews it against the original and tries to break it.

§5 AI · 100%

Only what survives counts.

§6 AI · 100%

The code at the top of this page is step one for a small piece of CPython: the C function that works out how many numbers are in a range, next to its Lean copy. The copy keeps the C code's exact arithmetic, including how its numbers wrap around when they get too big. The proof about it shows that this function, the slower version CPython uses for very large numbers, and the code that chooses between them always give the same, correct count. Proofs that prove nothing The hardest part wasn't getting AI agents to write proofs. It was getting proofs that actually mean something. Our first attempt asked the agents for breadth: copy every function and prove at least one thing about each.

§7 AI · 100%

They did.

§8 AI · 100%

Then a separate review found that fewer than half of the functions it checked held up, and in one file only 21%.

§9 AI · 100%

The proofs were all correct.

§10 AI · 100%

They just didn't say anything useful. For example, "if this function returns a number, the number is at least zero" is true, but a function that always crashes also satisfies it. So the reviewer now asks one question about every proof: could a broken version of the function pass it too? Version of the function"If it returns, the answer is ≥ 0""It always returns the right count" The real onepassespasses Always crashespassescaught Returns 0 for very large numberspassescaught Off by one when counting downpassescaught Two true statements about the same function. The first one lets every broken version through, so a function whose only proof looks like it doesn't count. Fixing a weak proof usually means going back and making the copy more realistic, for example letting a user's code throw an error in the middle of a function. The charts below show how that went for ten files. Each point is one round of review. The lines sometimes go down: when a reviewer finds that an accepted proof is wrong about real Python, everything that relied on it is taken back out.

§11 Human · 0%

Functions accepted100%0%review 1review 5review 1: 0 of 142 acceptedreview 2: 2 of 142 acceptedreview 3: 41 of 142 acceptedreview 4: 130 of 142 acceptedreview 5: 135 of 142 acceptedObjects/typeobject.c135 of 142 accepted · 5 reviews100%0%review 1review 5review 1: 1 of 56 acceptedreview 2: 18 of 56 acceptedreview 3: 54 of 56 acceptedreview 4: 55 of 56 acceptedreview 5: 56 of 56 acceptedObjects/genobject.c56 of 56 accepted · 5 reviews100%0%review 1review 7review 1: 31 of 46 acceptedreview 2: 42 of 46 acceptedreview 3: 44 of 46 acceptedreview 4: 45 of 46 acceptedreview 5: 44 of 46 acceptedreview 6: 45 of 46 acceptedreview 7: 46 of 46 acceptedObjects/setobject.c46 of 46 accepted · 7 reviews100%0%review 1review 5review 1: 31 of 48 acceptedreview 2: 39 of 48 acceptedreview 3: 42 of 48 acceptedreview 4: 43 of 48 acceptedreview 5: 45 of 48 acceptedModules/_json.c45 of 48 accepted · 5 reviews100%0%review 1review 9review 1: 21 of 39 acceptedreview 2: 30 of 39 acceptedreview 3: 35 of 39 acceptedreview 4: 34 of 39 acceptedreview 5: 33 of 39 acceptedreview 6: 34 of 39 acceptedreview 7: 29 of 39 acceptedreview 8: 36 of 39 acceptedreview 9: 37 of 39 acceptedLib/enum.py37 of 39 accepted · 9 reviews100%0%review 1review 3review 1: 11 of 38 acceptedreview 2: 31 of 38 acceptedreview 3: 37 of 38 acceptedLib/random.py37 of 38 accepted · 3 reviews100%0%review 1review 2review 1: 9 of 43 acceptedreview 2: 41 of 43 acceptedObjects/abstract.c41 of 43 accepted · 2 reviews100%0%review 1review 2review 1: 19 of 33 acceptedreview 2: 33 of 33 acceptedObjects/rangeobject.c33 of 33 accepted · 2 reviews100%0%review 1review 2review 1: 14 of 33 acceptedreview 2: 31 of 33 acceptedLib/pickletools.py31 of 33 accepted · 2 reviews100%0%review 1review 2review 1: 29 of 40 acceptedreview 2: 35 of 40 acceptedModules/binascii.c35 of 40 accepted · 2 reviewsRounds of review → Share of each file's reviewed functions accepted after each round of review, taken from the project's history.

§12 AI · 98%

Each point is one review; the scale is the same in every chart, from 0% to all of the functions reviewed in that file.

§13 AI · 99%

Accepted functions include ones the reviewer confirmed have nothing to check, such as memory bookkeeping.

§14 AI · 100%

When a correct proof is wrong anyway A checked proof guarantees that a statement follows from the copy.

§15 AI · 100%

It doesn't guarantee the copy matches real Python, and testing only covers the inputs you thought to try.

§16 AI · 100%

So every accepted statement is also run against real Python.

§17 AI · 100%

That caught several proofs that were valid and still wrong.

§18 AI · 100%

The 16-terabyte string The copy of Python's fast JSON encoder assumed memory requests always succeed.

§19 AI · 100%

So it "proved" the encoder could build its indentation strings for any nesting depth, including one that needs 16 terabytes of memory.

§20 AI · 100%

Real Python runs out of memory.

§21 AI · 100%

The fixed copy now treats every memory request as something that can fail. Nine extra steps, just once A proof said Python's pickle module never goes deeper than a certain number of nested calls. That turned out true except in a brand-new Python process. There, the first time it encodes a string, it quietly loads a text codec, and that load uses nine more nested calls. Our tests never saw it, because the test harness had already loaded the codec. Error types have families In Python, catching a TypeError also catches any more specific error built on top of it. Three separate copies checked an error's exact name instead. All three had proofs that a custom error type could break. Memory that comes back The copy of Python's set assumed that once memory is freed, the same address never comes back. Real memory allocators reuse addresses all the time. CPython uses a "did the address change?" check to notice when user code has swapped out the set's internals. With reuse, that check can be fooled. Fixing the copy turned an accepted proof into a real memory bug, now one of our bug reports. What we found When a proof fails because Python itself is wrong, and not the copy, we write it up as a bug report. Each report includes a short script that shows the bug on Python 3.15.2 (for most, also on the development version), a search for duplicates already reported, and an honest rating of how much it matters.

§22 Human · 0%

C and Python versions disagree129Other122Wrong result56Crash28Memory corruption27Doesn't match the docs26Wrong error or message18Missing input check12 418 bug reports by type.