From cb5c51e459fa9ecd08f019e8e7bb9afa2e0aeb2a Mon Sep 17 00:00:00 2001 From: Sylwester Arabas Date: Sun, 19 Nov 2023 05:43:01 +0100 Subject: [PATCH 1/7] add Matlab GHA workflow (with a struct-definition addition to the basic example) --- .github/workflows/matlab.yml | 47 ++++++++++++++++++++++++++++++++++++ src/main.cpp | 7 ++++++ tests/test_basic.py | 1 + 3 files changed, 55 insertions(+) create mode 100644 .github/workflows/matlab.yml diff --git a/.github/workflows/matlab.yml b/.github/workflows/matlab.yml new file mode 100644 index 0000000..50b614f --- /dev/null +++ b/.github/workflows/matlab.yml @@ -0,0 +1,47 @@ +name: matlab + +defaults: + run: + shell: bash + +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + + + +jobs: + matlab: + runs-on: ubuntu-20.04 + + steps: + - uses: actions/checkout@v2 + with: + submodules: recursive + + - uses: actions/setup-python@v1 + with: + python-version: 3.8 + + - run: | + echo "CC=gcc-9" >> $GITHUB_ENV + echo "CXX=g++-9" >> $GITHUB_ENV + VERBOSE=1 pip install --verbose -e . + + - run: | + echo "m = py.importlib.import_module('cmake_example')" > test.m + echo "AStruct = m.AStruct" >> test.m + echo "AStruct()" >> test.m + cat test.m + + - run: python -c "import cmake_example as m; m.AStruct()" + + - uses: matlab-actions/setup-matlab@v0 + with: + release: R2022a + + - uses: matlab-actions/run-command@v0 + with: + command: test diff --git a/src/main.cpp b/src/main.cpp index d50bce7..1ccff6b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,6 +7,11 @@ int add(int i, int j) { return i + j; } +struct AStruct { + AStruct() { + } +}; + namespace py = pybind11; PYBIND11_MODULE(cmake_example, m) { @@ -35,6 +40,8 @@ PYBIND11_MODULE(cmake_example, m) { Some other explanation about the subtract function. )pbdoc"); + py::class_(m, "AStruct").def(py::init<>()); + #ifdef VERSION_INFO m.attr("__version__") = MACRO_STRINGIFY(VERSION_INFO); #else diff --git a/tests/test_basic.py b/tests/test_basic.py index 070626e..caebcde 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -5,3 +5,4 @@ def test_main(): assert m.__version__ == "0.0.1" assert m.add(1, 2) == 3 assert m.subtract(1, 2) == -1 + assert m.AStruct() is not None From a4d99d88818842c284f7962eee3115f95269150c Mon Sep 17 00:00:00 2001 From: Sylwester Arabas Date: Wed, 22 Nov 2023 22:28:43 +0100 Subject: [PATCH 2/7] try providing Matlab with a dummy `pybind11_type` module --- .github/workflows/matlab.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/matlab.yml b/.github/workflows/matlab.yml index 50b614f..9041908 100644 --- a/.github/workflows/matlab.yml +++ b/.github/workflows/matlab.yml @@ -38,6 +38,10 @@ jobs: - run: python -c "import cmake_example as m; m.AStruct()" + - run: | + echo "pybind11_type=type" > pybind11_type.py + echo "PYTHONPATH=." >> $GITHUB_ENV + - uses: matlab-actions/setup-matlab@v0 with: release: R2022a From 5bebb1f9efeeda4b8ea7978135ce64a9e9dea5b9 Mon Sep 17 00:00:00 2001 From: Sylwester Arabas Date: Wed, 22 Nov 2023 22:34:32 +0100 Subject: [PATCH 3/7] fix module name --- .github/workflows/matlab.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/matlab.yml b/.github/workflows/matlab.yml index 9041908..c23ed34 100644 --- a/.github/workflows/matlab.yml +++ b/.github/workflows/matlab.yml @@ -39,7 +39,7 @@ jobs: - run: python -c "import cmake_example as m; m.AStruct()" - run: | - echo "pybind11_type=type" > pybind11_type.py + echo "pybind11_type=type" > pybind11_builtins.py echo "PYTHONPATH=." >> $GITHUB_ENV - uses: matlab-actions/setup-matlab@v0 From e1a73a4c9d8fe5706f6c8b6e6bd4acb2e017e586 Mon Sep 17 00:00:00 2001 From: Sylwester Arabas Date: Thu, 1 Feb 2024 06:18:37 +0100 Subject: [PATCH 4/7] try out if using py::metaclass helps? --- .github/workflows/matlab.yml | 6 +++--- src/main.cpp | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/matlab.yml b/.github/workflows/matlab.yml index c23ed34..abbf6a5 100644 --- a/.github/workflows/matlab.yml +++ b/.github/workflows/matlab.yml @@ -38,9 +38,9 @@ jobs: - run: python -c "import cmake_example as m; m.AStruct()" - - run: | - echo "pybind11_type=type" > pybind11_builtins.py - echo "PYTHONPATH=." >> $GITHUB_ENV + #- run: | + # echo "pybind11_type=type" > pybind11_builtins.py + # echo "PYTHONPATH=." >> $GITHUB_ENV - uses: matlab-actions/setup-matlab@v0 with: diff --git a/src/main.cpp b/src/main.cpp index 1ccff6b..085e6bd 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -40,7 +40,7 @@ PYBIND11_MODULE(cmake_example, m) { Some other explanation about the subtract function. )pbdoc"); - py::class_(m, "AStruct").def(py::init<>()); + py::class_(m, "AStruct", py::metaclass((PyObject *) &PyType_Type)).def(py::init<>()); #ifdef VERSION_INFO m.attr("__version__") = MACRO_STRINGIFY(VERSION_INFO); From 2257222550b8efed30ac13e8581d46a955aa8fd6 Mon Sep 17 00:00:00 2001 From: Sylwester Arabas Date: Thu, 1 Feb 2024 17:32:45 +0100 Subject: [PATCH 5/7] try depicting the problem with fun argument type --- src/main.cpp | 2 ++ tests/test_basic.py | 5 ++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index 085e6bd..6c85f03 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -42,6 +42,8 @@ PYBIND11_MODULE(cmake_example, m) { py::class_(m, "AStruct", py::metaclass((PyObject *) &PyType_Type)).def(py::init<>()); + m.def("fun", [](AStruct &a) { return 44; } ); + #ifdef VERSION_INFO m.attr("__version__") = MACRO_STRINGIFY(VERSION_INFO); #else diff --git a/tests/test_basic.py b/tests/test_basic.py index caebcde..707fffa 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -5,4 +5,7 @@ def test_main(): assert m.__version__ == "0.0.1" assert m.add(1, 2) == 3 assert m.subtract(1, 2) == -1 - assert m.AStruct() is not None + + a = m.AStruct() + assert a is not None + m.fun(a) From e68fd75d17b03a0d60cd07418dcfb277614674c3 Mon Sep 17 00:00:00 2001 From: Sylwester Arabas Date: Thu, 1 Feb 2024 17:44:21 +0100 Subject: [PATCH 6/7] check if py::arg matters? --- src/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index 6c85f03..0e5c23f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -42,7 +42,7 @@ PYBIND11_MODULE(cmake_example, m) { py::class_(m, "AStruct", py::metaclass((PyObject *) &PyType_Type)).def(py::init<>()); - m.def("fun", [](AStruct &a) { return 44; } ); + m.def("fun", [](AStruct &a) { return 44; }, "doc", py::arg("a") ); #ifdef VERSION_INFO m.attr("__version__") = MACRO_STRINGIFY(VERSION_INFO); From bbf064f20de6fb3ee7707074b974a8593e1f543b Mon Sep 17 00:00:00 2001 From: Sylwester Arabas Date: Thu, 1 Feb 2024 17:55:48 +0100 Subject: [PATCH 7/7] check if the problem could be depicted with a ctor arg? --- src/main.cpp | 5 +++++ tests/test_basic.py | 1 + 2 files changed, 6 insertions(+) diff --git a/src/main.cpp b/src/main.cpp index 0e5c23f..f4dde68 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,6 +11,10 @@ struct AStruct { AStruct() { } }; +struct BStruct { + BStruct(AStruct &a) { + } +}; namespace py = pybind11; @@ -41,6 +45,7 @@ PYBIND11_MODULE(cmake_example, m) { )pbdoc"); py::class_(m, "AStruct", py::metaclass((PyObject *) &PyType_Type)).def(py::init<>()); + py::class_(m, "BStruct", py::metaclass((PyObject *) &PyType_Type)).def(py::init()); m.def("fun", [](AStruct &a) { return 44; }, "doc", py::arg("a") ); diff --git a/tests/test_basic.py b/tests/test_basic.py index 707fffa..f00045f 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -9,3 +9,4 @@ def test_main(): a = m.AStruct() assert a is not None m.fun(a) + b = m.BStruct(a)