Skip to content

Improvements to SWIG typemaps for Python interface#3190

Merged
oskooi merged 8 commits intoNanoComp:masterfrom
oskooi:python_swig_fixes
Apr 4, 2026
Merged

Improvements to SWIG typemaps for Python interface#3190
oskooi merged 8 commits intoNanoComp:masterfrom
oskooi:python_swig_fixes

Conversation

@oskooi
Copy link
Copy Markdown
Collaborator

@oskooi oskooi commented Apr 3, 2026

Various improvements to the SWIG typemaps for the Python interface, organized by priority.

Critical — Crash/Segfault Bugs

1. Missing NULL checks in callback wrappers (meep.i)

py_callback_wrap (line 142), py_amp_func_wrap (line 152), and custom_py_src_time::dipole (meep-python.hpp:32) do not check the return value of PyObject_CallFunctionObjArgs. If the Python callback raises an exception, these dereference NULL →segfault. Same issue in mpb.i callback wrappers.

2. Use-after-free in _get_dft_array (meep.i:456)

The rank == 0 zero-data path passes a stack-allocated d[1] to PyArray_SimpleNewFromData, which creates a NumPy array pointing to stack memory that goes out of scope. The singleton path also leaks dft_arr.

High Priority — Correctness

3. meep::abort() instead of Python exceptions (typemap_utils.cpp)

Many conversion functions (e.g., pyabsorber_to_absorber, pymaterial_grid_to_material_grid, py_list_to_gobj_list) call abort_with_stack_trace() on invalid input. This kills the process instead of raising a catchable Python exception. These should set PyErr_SetString and return an error code.

4. PyFunction_Check vs PyCallable_Check (meep.i:1408)

The kpoint_func typemap rejects lambdas, bound methods, functools.portal, and any callable that isn't a plain function. Should use PyCallable_Check.

Medium Priority — Maintenance & Compatibility

5. Dupilcate typemaps

  • xtics/ytics/ztics typemaps appear twice (lines 1001-1018 and 1083-1105), first block is dead code.
  • freearg for amplitude function appears twice (lines 748 and 1069).
  • double* array input typemaps repeat the same is_array + array_data pattern ~8 times - could use %apply.

Low Priority — Cleanup

6. Duplicate %ignore directives (meep.i:1525-1529)

is_variable, is_file, is_medium each ignored twice.

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Apr 3, 2026

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 73.87%. Comparing base (f29a8c7) to head (f2821a2).
⚠️ Report is 120 commits behind head on master.
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3190      +/-   ##
==========================================
+ Coverage   73.81%   73.87%   +0.05%     
==========================================
  Files          18       18              
  Lines        5423     5458      +35     
==========================================
+ Hits         4003     4032      +29     
- Misses       1420     1426       +6     

see 2 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread python/meep-python.hpp Outdated
Comment thread python/mpb.i
Comment thread python/meep-python.hpp
@oskooi oskooi merged commit c06e6c9 into NanoComp:master Apr 4, 2026
5 checks passed
@oskooi oskooi deleted the python_swig_fixes branch April 4, 2026 20:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants