Skip to content

Commit 4f8a84d

Browse files
oschwaldclaude
andcommitted
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

File tree

HISTORY.rst

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
History
44
-------
55

6-
2.9.0
6+
3.0.0
77
++++++++++++++++++
88

99
* IMPORTANT: Python 3.10 or greater is required. If you are using an older
@@ -15,6 +15,13 @@ History
1515
thread-safe for concurrent reads on platforms with pthread support (such as
1616
Linux and macOS) and Windows. On other platforms, the extension will use
1717
GIL-based protection.
18+
* The C extension now uses PEP 489 multi-phase initialization, enabling
19+
proper subinterpreter support and module isolation for Python 3.12+. This
20+
modernizes the extension to use heap types instead of static types and
21+
implements per-module state management. Key benefits include support for
22+
Python 3.12+ isolated subinterpreters, multiple independent module
23+
instances, and future-proofing for Python 3.14's InterpreterPoolExecutor.
24+
Requested by R. Christian McDonald in GitHub #105.
1825

1926
2.8.2 (2025-07-25)
2027
++++++++++++++++++

0 commit comments

Comments
 (0)