How to use Codex to build a Python C extension
Simon Willison asked Codex for a Python C extension exposing SQLite queries with timeouts, then read and ran the result to learn about the SQLite C API, Python reference counting, and extension packaging.
Suggested prompt
Suggested prompt written by Agents In Use, not the source author. Replace bracketed inputs and review tool permissions first.
Build a small Python C extension that runs [SQLite queries] with a configurable [timeout]. Include package configuration and tests. Explain the progress handler, result conversion, reference counting, and error cleanup so I can read and run the implementation locally.
Reach people exploring AI agents
See placements →Evidence at a glance
Sourced account- Source poster / publisher
- simonw
- Original account
- Read the source ↗
- Published
- Source link checked
- Reported result
- Willison reported a working local example and lessons about SQLite progress handlers, Python tuples, reference counting, error cleanup, and extension packaging.
- Human involvement
- The author read the generated code and ran it on his own computer.
A linked account supports attribution. It does not mean we independently reproduced its result.
Report an issue with this case
Open correction emailOpens your email app with the case link included. Nothing is sent automatically.
How the agent did it
- Ask Codex to build a Python C extension exposing SQLite queries with a timeout.
- Have Codex produce the C extension and its Python package configuration.
- Read the generated code and run it locally to examine its behavior.
Simon Willison used Codex to explore writing a custom Python C extension, something he had been curious about but had not previously tried. He asked for an extension that exposed simple SQLite queries with a timeout. Codex produced the implementation, and Willison read the generated code and ran it on his own computer to see it work.
The exercise exposed several concrete parts of extension development. Willison learned how SQLite's progress handler could enforce time limits in C, how to construct and populate Python tuples, and how the SQLite C API executes queries and iterates through results. He also examined column type constants, error cleanup with goto, resource release, and Python reference counting through DECREF. The package combined setup.py and pyproject.toml to compile the extension.
Willison described the implementation as roughly 150 lines of readable C. He reported spending about one minute prompting and another 15 minutes learning from the result. Who it's for: Python developers who want a concrete, inspectable introduction to C extensions through a narrowly scoped experiment.
Prompt
Suggested prompt (written by Agents In Use, not the author's)
Source: Hacker News · simonw
Frequently asked questions
How did the author check the generated extension?
Willison read the generated code and ran it on his own computer to see it work.
What did the exercise teach about SQLite timeouts?
Willison reported that sqlite3_progress_handler, which he had used for SQLite time limits in Python, also worked in C.
More details
About this account
Codex produced the extension implementation; the author described it as capable of writing, compiling, and testing Python C extensions.
Platform: Hacker News. Attribution: https://news.ycombinator.com/item?id=46491454. Prompt: Suggested (written by Agents In Use, not the author's). Sourced by the Agents In Use pipeline.
Credit: simonw on Hacker News
Source published
Added to Agents In Use
Source checked
Tools and topics
Codex · Python · SQLite · setup.py · pyproject.toml
Python · C extensions · SQLite
Editorial completeness: 100/100. Measures documented fields, not agent performance or verified outcomes.