Skip to content

Commit 7d826a5

Browse files
committed
Swig version mismatch warning (#3000)
Store swig version used for the amici and model extensions. Emit a warning if versions don't match. Closes #2993.
1 parent 5afc9a2 commit 7d826a5

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

swig/amici.i

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,17 @@ nonstandard type conversions.
6464
}
6565
}
6666

67+
// store swig version
68+
%constant int SWIG_VERSION_MAJOR = (SWIG_VERSION >> 16);
69+
%constant int SWIG_VERSION_MINOR = ((SWIG_VERSION >> 8) & 0xff);
70+
%constant int SWIG_VERSION_PATCH = (SWIG_VERSION & 0xff);
71+
72+
%pythoncode %{
73+
# SWIG version used to build the amici extension as `(major, minor, patch)`
74+
_SWIG_VERSION = (SWIG_VERSION_MAJOR, SWIG_VERSION_MINOR, SWIG_VERSION_PATCH)
75+
%}
76+
77+
6778
// Warning 503: Can't wrap 'operator ==' unless renamed to a valid identifier.
6879
%rename("__eq__") operator ==;
6980

swig/modelname.template.i

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,28 @@ if t_imported < t_modified:
3636

3737
%module(package="TPL_MODELNAME",moduleimport=MODULEIMPORT) TPL_MODELNAME
3838

39+
// store swig version
40+
%constant int SWIG_VERSION_MAJOR = (SWIG_VERSION >> 16);
41+
%constant int SWIG_VERSION_MINOR = ((SWIG_VERSION >> 8) & 0xff);
42+
%constant int SWIG_VERSION_PATCH = (SWIG_VERSION & 0xff);
43+
44+
%pythoncode %{
45+
# SWIG version used to build the model extension as `(major, minor, patch)`
46+
_SWIG_VERSION = (SWIG_VERSION_MAJOR, SWIG_VERSION_MINOR, SWIG_VERSION_PATCH)
47+
48+
if (amici_swig := amici.amici._SWIG_VERSION) != (model_swig := _SWIG_VERSION):
49+
import warnings
50+
warnings.warn(
51+
f"SWIG version mismatch between amici ({amici_swig}) and model "
52+
f"({model_swig}). This may lead to unexpected behavior. "
53+
"In that case, please recompile the model with swig=="
54+
f"{amici_swig[0]}.{amici_swig[1]}.{amici_swig[2]} or rebuild amici "
55+
f"with swig=={model_swig[0]}.{model_swig[1]}.{model_swig[2]}.",
56+
RuntimeWarning,
57+
stacklevel=2,
58+
)
59+
%}
60+
3961
%pythoncode %{
4062
# the model-package __init__.py module (will be set during import)
4163
_model_module = None

0 commit comments

Comments
 (0)