Skip to content

Python wrapping import fails with system SWIG 4.4.1 under Limited API (moduledef/delete_SwigPyIterator); working window is SWIG 4.3.x #6479

Description

@hjmjohnson

Description

Building ITK Python wrapping against system SWIG 4.4.1 (ITK_USE_SYSTEM_SWIG=ON) with the default Python Limited API / Stable ABI (ITK_USE_PYTHON_LIMITED_API=ON) compiles and links cleanly but fails at import with:

AttributeError: 'moduledef' object has no attribute 'delete_SwigPyIterator'

This is distinct from #6303 / #5540 (the SWIG 4.4.1 compile syntax error in ITKCommonBase.i), which is already fixed on main by 74902b3 (PR #5695, pygccxml + --castxml-output=1). That compile fix is confirmed present — the build reaches a full link — but the abi3 runtime glue from SWIG 4.4.1 references _pyBasePython.delete_SwigPyIterator, which the Limited-API moduledef module does not expose.

The verified system-SWIG compatibility window for ITK main (6.0.0) wrapping is SWIG 4.3.x only.

Verified build matrix (macOS arm64, ITK main 6.0.0, pixi-supplied swig/castxml, Limited API on)
System SWIG Result Cause
4.2.1 compile fails error: use of undeclared identifier 'SWIG_Py_DECREF' in ITKCommonPython.cpp (macro absent pre-4.3 runtime)
2024-03-26-master (vendored) works the snapshot ITK CI is tuned to
4.3.1 works — imports + itk.median_image_filter runs descends from the vendored master, has SWIG_Py_DECREF, pre-4.4 ABI codegen
4.4.1 import fails 4.4 Limited-API moduledef / delete_SwigPyIterator codegen change

CastXML was supplied from conda-forge 0.7.0 in all cases and worked.

Import traceback (SWIG 4.4.1)
File ".../itk/pyBasePython.py", line 88, in SwigPyIterator
    __swig_destroy__ = _pyBasePython.delete_SwigPyIterator
AttributeError: 'moduledef' object has no attribute 'delete_SwigPyIterator'

Generated glue header: # This file was automatically generated by SWIG ... Version 4.4.1.

Why CI does not catch this

ITK CI always uses the vendored 2024-03-26-master snapshot (Wrapping/Generators/SwigInterface/CMakeLists.txt:25), never a system SWIG ≥ 4.3. The Limited-API import path with system 4.4.1 is therefore untested upstream. #6303 was reported by a MacPorts build and only reached the compile stage.

Where a fix likely belongs (not igenerator.py)

The Limited-ABI codegen is driven by Wrapping/macro_files/itk_end_wrap_module.cmake (ITK_USE_PYTHON_LIMITED_API), Wrapping/Generators/Python/{main_module_ext,module_ext}.i.in, PyBase/pyBase.i, and CMake/ITKSetPython3Vars.cmake — not igenerator.py (whose 4.4.1-related change, PR #5695, was type-resolution, not ABI). SimpleITK builds with SWIG 4.4.1 and Limited API (SimpleITK_PYTHON_USE_LIMITED_API default ON) successfully, so its hand-written .i handling of the iterator protocol is a useful reference.

Suggested actions

  • Raise swig_version_min from 4.2.0 (which cannot compile) to 4.3.0, and consider documenting 4.3.x as the supported system-SWIG window until 4.4 Limited-API glue is adapted.
  • Track the 4.4.x Limited-API moduledef/SwigPyIterator adaptation as the remaining work to allow system SWIG ≥ 4.4.

Reproducibility

100% — clean ITK_USE_SYSTEM_SWIG=ON build with system SWIG 4.4.1, ITK_USE_PYTHON_LIMITED_API=ON, Python 3.13.

Versions

ITK main (6.0.0), Python 3.13, macOS arm64. conda-forge swig 4.4.1 / 4.3.1 / 4.2.1, castxml 0.7.0.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type:BugInconsistencies or issues which will cause an incorrect result under some or all circumstances

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions