Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions gtwrap/matlab_wrapper/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ class WrapperTemplate:
switch(id) {{
{cases} }}
}} catch(const std::exception& e) {{
std::cout.rdbuf(outbuf);
mexErrMsgTxt(("Exception from gtsam:\\n" + std::string(e.what()) + "\\n").c_str());
}}\n
std::cout.rdbuf(outbuf);
Expand Down
2 changes: 0 additions & 2 deletions matlab.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ using gtsam::Matrix;
using gtsam::Point2;
using gtsam::Point3;

extern "C" {
#include <mex.h>
}

#include <cstdint>
#include <limits>
Expand Down
1 change: 1 addition & 0 deletions tests/expected/matlab/class_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1380,6 +1380,7 @@ void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[])
break;
}
} catch(const std::exception& e) {
std::cout.rdbuf(outbuf);
mexErrMsgTxt(("Exception from gtsam:\n" + std::string(e.what()) + "\n").c_str());
}

Expand Down
1 change: 1 addition & 0 deletions tests/expected/matlab/enum_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[])
break;
}
} catch(const std::exception& e) {
std::cout.rdbuf(outbuf);
mexErrMsgTxt(("Exception from gtsam:\n" + std::string(e.what()) + "\n").c_str());
}

Expand Down
1 change: 1 addition & 0 deletions tests/expected/matlab/functions_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[])
break;
}
} catch(const std::exception& e) {
std::cout.rdbuf(outbuf);
mexErrMsgTxt(("Exception from gtsam:\n" + std::string(e.what()) + "\n").c_str());
}

Expand Down
1 change: 1 addition & 0 deletions tests/expected/matlab/geometry_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[])
break;
}
} catch(const std::exception& e) {
std::cout.rdbuf(outbuf);
mexErrMsgTxt(("Exception from gtsam:\n" + std::string(e.what()) + "\n").c_str());
}

Expand Down
1 change: 1 addition & 0 deletions tests/expected/matlab/inheritance_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,7 @@ void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[])
break;
}
} catch(const std::exception& e) {
std::cout.rdbuf(outbuf);
mexErrMsgTxt(("Exception from gtsam:\n" + std::string(e.what()) + "\n").c_str());
}

Expand Down
1 change: 1 addition & 0 deletions tests/expected/matlab/multiple_files_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[])
break;
}
} catch(const std::exception& e) {
std::cout.rdbuf(outbuf);
mexErrMsgTxt(("Exception from gtsam:\n" + std::string(e.what()) + "\n").c_str());
}

Expand Down
1 change: 1 addition & 0 deletions tests/expected/matlab/namespaces_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,7 @@ void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[])
break;
}
} catch(const std::exception& e) {
std::cout.rdbuf(outbuf);
mexErrMsgTxt(("Exception from gtsam:\n" + std::string(e.what()) + "\n").c_str());
}

Expand Down
1 change: 1 addition & 0 deletions tests/expected/matlab/special_cases_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[])
break;
}
} catch(const std::exception& e) {
std::cout.rdbuf(outbuf);
mexErrMsgTxt(("Exception from gtsam:\n" + std::string(e.what()) + "\n").c_str());
}

Expand Down
1 change: 1 addition & 0 deletions tests/expected/matlab/template_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[])
break;
}
} catch(const std::exception& e) {
std::cout.rdbuf(outbuf);
mexErrMsgTxt(("Exception from gtsam:\n" + std::string(e.what()) + "\n").c_str());
}

Expand Down
28 changes: 28 additions & 0 deletions tests/test_matlab_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,34 @@ def test_deconstructors_are_idempotent(self):
' }\n'
' delete self;', cpp_content)

def test_mex_error_path_restores_stream(self):
"""MEX failures restore std::cout before leaving the gateway."""
file = osp.join(self.INTERFACE_DIR, 'functions.i')
wrapper = MatlabWrapper(
module_name='functions',
top_module_namespace=['gtsam'],
ignore_classes=[''],
)
wrapper.wrap([file], path=self.MATLAB_ACTUAL_DIR)

cpp_file = osp.join(self.MATLAB_ACTUAL_DIR,
'functions_wrapper.cpp')
with open(cpp_file, 'r', encoding='UTF-8') as generated_file:
cpp_content = generated_file.read()

self.assertIn(
'} catch(const std::exception& e) {\n'
' std::cout.rdbuf(outbuf);\n'
' mexErrMsgTxt(', cpp_content)

matlab_header = osp.join(self.TEST_DIR, '..', 'matlab.h')
with open(matlab_header, 'r', encoding='UTF-8') as header_file:
header_content = header_file.read()

self.assertIn('#include <mex.h>', header_content)
self.assertNotIn('extern "C" {\n#include <mex.h>\n}',
header_content)

def test_size_t_round_trip(self):
"""Generated size_t wrappers use alias-safe scalar conversions."""
file = osp.join(self.INTERFACE_DIR, 'matlab_integer_aliases.i')
Expand Down
Loading