Commit 9fabbb3
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 <[email protected]>1 parent 2f45722 commit 9fabbb3
2 files changed
+240
-119
lines changed
0 commit comments