Skip to content

Commit

Permalink
[PyROOT] Retain C++ ownership for TColor
Browse files Browse the repository at this point in the history
  • Loading branch information
vepadulano authored and dpiparo committed Nov 16, 2024
1 parent bfa0b18 commit 9e2df7e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
1 change: 1 addition & 0 deletions bindings/pyroot/pythonizations/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ set(py_sources
ROOT/_pythonization/_tclass.py
ROOT/_pythonization/_tclonesarray.py
ROOT/_pythonization/_tcollection.py
ROOT/_pythonization/_tcolor.py
ROOT/_pythonization/_tcomplex.py
ROOT/_pythonization/_tcontext.py
ROOT/_pythonization/_tdirectory.py
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Author: Vincenzo Eduardo Padulano CERN 11/2024

################################################################################
# Copyright (C) 1995-2024, Rene Brun and Fons Rademakers. #
# All rights reserved. #
# #
# For the licensing terms see $ROOTSYS/LICENSE. #
# For the list of contributors see $ROOTSYS/README/CREDITS. #
################################################################################
from . import pythonization

def _TColor_constructor(self, *args, **kwargs):
"""
Forward the arguments to the C++ constructor and retain ownership. This
helps avoiding double deletes due to ROOT automatic memory management.
"""
self._cpp_constructor(*args, **kwargs)
import ROOT
ROOT.SetOwnership(self, False)


@pythonization("TColor")
def pythonize_tcolor(klass):
klass._cpp_constructor = klass.__init__
klass.__init__ = _TColor_constructor
7 changes: 6 additions & 1 deletion bindings/pyroot/pythonizations/test/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,17 @@ def test_tf2_memory_regulation(self):
# application does not segfault
f2 = ROOT.TF2("f2", "sin(x)*sin(y)/x/y")


def test_tf3_memory_regulation(self):
"""Make sure TF3 is properly managed by the memory regulation logic"""
# The test is just that the memory regulation works correctly and the
# application does not segfault
f3 = ROOT.TF3("f3","[0] * sin(x) + [1] * cos(y) + [2] * z",0,10,0,10,0,10)

def test_tcolor_memory_regulation(self):
"""Make sure TColor is properly managed by the memory regulation logic"""
# The test is just that the memory regulation works correctly and the
# application does not segfault
c = ROOT.TColor(42, 42, 42)

if __name__ == '__main__':
unittest.main()

0 comments on commit 9e2df7e

Please sign in to comment.