Skip to content

Switch to heap types using PyType_FromSpec #643

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 17, 2025
Merged

Switch to heap types using PyType_FromSpec #643

merged 5 commits into from
Jul 17, 2025

Conversation

cjdoris
Copy link
Collaborator

@cjdoris cjdoris commented Jul 17, 2025

This PR implements the suggestion from @ZeroIntensity to switch from static PyTypeObject structures to heap-allocated types using PyType_FromSpec.

Changes

  • Added PyType_Spec and PyType_Slot structs to C API bindings in src/C/consts.jl
  • Added all Python type slot constants from ypeslots.h for proper slot definitions
  • Converted PyJuliaBase_Type creation from manual PyTypeObject to PyType_FromSpec in src/JlWrap/C.jl
  • Used weaklistoffset member definition for backward compatibility with older Python versions
  • Fixed PyType_FromSpec function signature in src/C/pointers.jl

Key Benefits

  • Avoids ABI compatibility issues by using slot-based type definitions instead of structure fields
  • Future-proof approach that works with different Python versions
  • Cleaner implementation using the modern Python C API

Implementation Details

The conversion replaces the old approach of manually filling a PyTypeObject struct with a modern PyType_FromSpec approach that:

  1. Creates a PyType_Spec with basic type information
  2. Defines an array of PyType_Slot structures for all type methods and protocols
  3. Uses weaklistoffset member definition for weakref support (compatible with older Python versions)
  4. Properly handles the buffer protocol through slots

Closes #641

Christopher Doris added 5 commits July 17, 2025 10:55
- Convert PyJuliaBase_Type creation from manual PyTypeObject to PyType_FromSpec
- Add PyType_Spec and PyType_Slot structs to C API bindings
- Add all Python type slot constants from typeslots.h
- Use __weaklistoffset__ member definition for older Python compatibility
- Implement proper slot-based type definition with buffer protocol support

Closes #641
- Remove unnecessary itemsize field (defaults to 0)
- Remove unnecessary Cint conversion for basicsize (happens automatically)
Buffer protocol methods are now handled through PyType_Slot mechanism,
making the old _pyjlbase_as_buffer constant unnecessary.
@cjdoris cjdoris merged commit 2cb07d1 into main Jul 17, 2025
14 checks passed
@cjdoris cjdoris deleted the heap-types branch July 17, 2025 13:09
@cjdoris cjdoris mentioned this pull request Jul 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Switch to heap types
1 participant