Commit 4f8a84d
Implement PEP 489 multi-phase initialization for C extension
This commit modernizes the C extension to use PEP 489 multi-phase
initialization, enabling proper subinterpreter support and module
isolation for Python 3.12+.
Key changes:
Module State Management:
- Added maxminddb_state struct to store per-module state
- Implemented get_maxminddb_state() helpers to access module state
- Added module lifecycle functions (traverse, clear, free)
Type Conversion:
- Converted Reader_Type, Metadata_Type, and ReaderIter_Type from
static types to heap types using PyType_FromModuleAndSpec()
- Created PyType_Spec definitions for all three types
- Removed static global type declarations
State Threading:
- Updated all functions to access module state instead of globals:
- Reader_init(), get_record(), Reader_metadata()
- Reader_iter(), ReaderIter_next()
- from_entry_data_list(), from_map(), from_array()
- Removed static global variables for MaxMindDB_error and
ipaddress_ip_network
Module Initialization:
- Implemented maxminddb_exec() to initialize module state
- Added module slots declaring:
- Multi-phase initialization support (Py_mod_exec)
- Multiple interpreter support (Py_MOD_MULTIPLE_INTERPRETERS_SUPPORTED)
- GIL-free operation when available (Py_MOD_GIL_NOT_USED)
- Updated PyModuleDef with m_size, m_slots, and GC functions
- Simplified PyInit_extension() to use PyModuleDef_Init()
Testing:
- Updated test regex to handle fully qualified type names in error
messages (heap types include module path)
- All 278 tests pass
Benefits:
- Supports Python 3.12+ isolated subinterpreters
- Enables multiple independent module instances
- Complements existing free-threading support (PEP 703)
- Follows modern Python C API best practices
- Future-proof for Python 3.14's InterpreterPoolExecutor
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>1 parent 2299949 commit 4f8a84d
3 files changed
Lines changed: 248 additions & 120 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
| 6 | + | |
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
18 | 25 | | |
19 | 26 | | |
20 | 27 | | |
| |||
0 commit comments