Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: mutable to immutable object conversion with python bindings #564

Merged
merged 4 commits into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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: 0 additions & 1 deletion python/templates/MutableObject.cc.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
{{ utils.namespace_open(class.namespace) }}

{{ macros.constructors_destructors(class.bare_type, Members, prefix='Mutable') }}
Mutable{{ class.bare_type }}::operator {{ class.bare_type }}() const { return {{ class.bare_type }}(m_obj); }

{{ macros.member_getters(class, Members, use_get_syntax, prefix='Mutable') }}
{{ macros.single_relation_getters(class, OneToOneRelations, use_get_syntax, prefix='Mutable') }}
Expand Down
3 changes: 0 additions & 3 deletions python/templates/MutableObject.h.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ public:

{{ macros.constructors_destructors(class.bare_type, Members, prefix='Mutable') }}

/// conversion to const object
operator {{ class.bare_type }}() const;

public:

{{ macros.member_getters(Members, use_get_syntax) }}
Expand Down
2 changes: 2 additions & 0 deletions python/templates/Object.cc.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

{{ macros.constructors_destructors(class.bare_type, Members) }}

{{ class.bare_type }}::{{ class.bare_type }}(const Mutable{{ class.bare_type }}& other): {{ class.bare_type }}(other.m_obj) {}

{{ class.bare_type }}::{{ class.bare_type }}({{ class.bare_type }}Obj* obj) : m_obj(podio::utils::MaybeSharedPtr<{{ class.bare_type }}Obj>(obj)) {}

{{ class.bare_type }} {{ class.bare_type }}::makeEmpty() {
Expand Down
2 changes: 2 additions & 0 deletions python/templates/Object.h.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public:
using collection_type = {{ class.bare_type }}Collection;

{{ macros.constructors_destructors(class.bare_type, Members) }}
/// converting constructor from mutable object
{{ class.bare_type }}(const Mutable{{ class.bare_type }}& other);
m-fila marked this conversation as resolved.
Show resolved Hide resolved

static {{ class.bare_type }} makeEmpty();

Expand Down
4 changes: 4 additions & 0 deletions tests/unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,7 @@ else()
PODIO_SIOBLOCK_PATH=${CMAKE_CURRENT_BINARY_DIR}
)
endif()

add_test(NAME unittest.py COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/unittests.py)
PODIO_SET_TEST_ENV(unittest.py)
set_tests_properties(unittest.py PROPERTIES WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/tests)
43 changes: 43 additions & 0 deletions tests/unittests/unittests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env python3
m-fila marked this conversation as resolved.
Show resolved Hide resolved
"""cppyy python binding tests"""

import unittest
import ROOT

res = ROOT.gSystem.Load("libTestDataModel.so")
if res < 0:
raise RuntimeError("Failed to load libTestDataModel.so")

from ROOT import MutableExampleMC # pylint: disable=wrong-import-position # noqa: E402
from ROOT import ExampleMCCollection # pylint: disable=wrong-import-position # noqa: E402


class ObjectConversionsTest(unittest.TestCase):
"""Object conversion binding tests"""

def test_conversion_mutable_to_immutable(self):
ROOT.gInterpreter.Declare(
"""
void test_accepts_immutable(ExampleMC) {}
"""
)
accepts_immutable = ROOT.test_accepts_immutable
mutable_obj = MutableExampleMC()
accepts_immutable(mutable_obj)


class OneToManyRelationsTest(unittest.TestCase):
"""OneToManyRelations binding tests"""

def test_add(self):
particles = ExampleMCCollection()
parent_particle = particles.create()
daughter_particle = particles.create()

self.assertEqual(len(daughter_particle.parents()), 0)
daughter_particle.addparents(parent_particle)
self.assertEqual(len(daughter_particle.parents()), 1)


if __name__ == "__main__":
unittest.main()
Loading