|
| 1 | +From b4096c724e5bc6f0c2cca8d24665fa3d534b341d Mon Sep 17 00:00:00 2001 |
| 2 | +From: Ludovic Henry <git@ludovic.dev> |
| 3 | +Date: Fri, 18 Sep 2026 05:11:46 +0000 |
| 4 | +Subject: [PATCH] swig: guard the direct %stubcode sites for SWIG without the |
| 5 | + -stubs patch |
| 6 | + |
| 7 | +casadi.i carries a compat shim that defines the %stub_* primitives empty |
| 8 | +when SWIG_STUBS_ENABLED is unset, and its own comment says direct |
| 9 | +`%stubcode %{...%}` use sites have to be guarded with |
| 10 | +`#ifdef SWIG_STUBS_ENABLED` separately, because `%insert("stubs")` is not |
| 11 | +a known target in vanilla SWIG. Four of the use sites are unguarded, so |
| 12 | +generating the Python bindings with stock SWIG fails at the first one: |
| 13 | + |
| 14 | + casadi.i:3815: Error: Unknown directive '%stubcode'. |
| 15 | + |
| 16 | +Guard them like the others. With the patched fork (jaeandersson/swig, |
| 17 | +which upstream's own CI image ships) SWIG_STUBS_ENABLED is set and the |
| 18 | +blocks are emitted exactly as before; the manylinux image's stock SWIG |
| 19 | +now parses the file instead of aborting. |
| 20 | + |
| 21 | +Upstream-Status: To upstream [not yet submitted; the same sites are still unguarded on upstream's main] |
| 22 | +--- |
| 23 | + swig/casadi.i | 8 ++++++++ |
| 24 | + 1 file changed, 8 insertions(+) |
| 25 | + |
| 26 | +diff --git a/swig/casadi.i b/swig/casadi.i |
| 27 | +index 84ebbaadd..f2551e213 100644 |
| 28 | +--- a/swig/casadi.i |
| 29 | ++++ b/swig/casadi.i |
| 30 | +@@ -3145,6 +3145,7 @@ class _SupportsMX(Protocol): |
| 31 | + * DM-backed array satisfies _SupportsDM (hence _DM, and via the unions _SX |
| 32 | + * and _MX too) -- matching casadi's runtime to_ptr rules. */ |
| 33 | + #if defined(SWIG_STUBS_ENABLED) && defined(SWIGPYTHON) |
| 34 | ++#ifdef SWIG_STUBS_ENABLED |
| 35 | + %stubcode %{class ArrayInterface(Generic[_T]): |
| 36 | + """numpy-semantics array view over a casadi DM/SX/MX. Constructing one |
| 37 | + dispatches to the backing-typed subclass ArrayInterfaceDM/SX/MX.""" |
| 38 | +@@ -3237,6 +3238,7 @@ class ArrayInterfaceMX(ArrayInterface[MX]): |
| 39 | + |
| 40 | + %} |
| 41 | + #endif |
| 42 | ++#endif |
| 43 | + |
| 44 | + /* Matrix input aliases. SX/MX both extend DM; they don't extend each |
| 45 | + * other -- to_ptr<SX>(MX) and to_ptr<MX>(SX) fail at runtime. |
| 46 | +@@ -3812,9 +3814,11 @@ PyOS_setsig(SIGINT, SigIntHandler); |
| 47 | + %stub_unary_math(arcsinh) |
| 48 | + %stub_unary_math(arccosh) |
| 49 | + /* Re-exported from numpy at runtime (`from numpy import pi, inf`). */ |
| 50 | ++#ifdef SWIG_STUBS_ENABLED |
| 51 | + %stubcode %{pi: float |
| 52 | + inf: float |
| 53 | + %} |
| 54 | ++#endif |
| 55 | + %stub_overload_func(arctan2, DM, x: _DM, y: _DM) |
| 56 | + %stub_overload_func(arctan2, SX, x: _SX, y: _SX) |
| 57 | + %stub_overload_func(arctan2, MX, x: _MX, y: _MX) |
| 58 | +@@ -4058,8 +4062,10 @@ class NZproxy: |
| 59 | + * masks -- the _selftyped overload matches only when _T=MX, keeping |
| 60 | + * DM.nz / SX.nz narrow. */ |
| 61 | + #ifdef SWIG_STUBS_ENABLED |
| 62 | ++#ifdef SWIG_STUBS_ENABLED |
| 63 | + %stubcode %{class NZproxy(Generic[_TNZ]): |
| 64 | + %} |
| 65 | ++#endif |
| 66 | + %stub_method(__init__, None, matrix: _TNZ) |
| 67 | + %stub_overload_method_selftyped(__getitem__, MX, NZproxy[MX], s: _MIndex | MX) |
| 68 | + %stub_overload_method(__getitem__, _TNZ, s: _MIndex) |
| 69 | +@@ -4154,8 +4160,10 @@ class NZproxy: |
| 70 | + * scalar-object boxing for symbolic. Keeps scipy.linalg.solve(A,b) |
| 71 | + * etc. typing without forcing users to wrap calls in np.array(). */ |
| 72 | + %stub_method(__array__, %arg(NDArray[arraydtype]), *args: Any, **kwargs: Any) |
| 73 | ++#ifdef SWIG_STUBS_ENABLED |
| 74 | + %stubcode %{ __array_priority__: float |
| 75 | + %} |
| 76 | ++#endif |
| 77 | + %stub_method(__array_ufunc__, Any, ufunc: Any, method: builtins.str, *inputs: Any, **kwargs: Any) |
| 78 | + %stub_method(__array_function__, Any, func: Any, types: Any, args: Any, kwargs: Any) |
| 79 | + %enddef |
0 commit comments