diff --git a/bindings/pyroot/pythonizations/CMakeLists.txt b/bindings/pyroot/pythonizations/CMakeLists.txt index dc05451a1f56d..dd2fc030a180b 100644 --- a/bindings/pyroot/pythonizations/CMakeLists.txt +++ b/bindings/pyroot/pythonizations/CMakeLists.txt @@ -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 diff --git a/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_tcolor.py b/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_tcolor.py new file mode 100644 index 0000000000000..a4ddfd2411b8c --- /dev/null +++ b/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_tcolor.py @@ -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 diff --git a/bindings/pyroot/pythonizations/test/memory.py b/bindings/pyroot/pythonizations/test/memory.py index 488e6a2052261..48b653d779c47 100644 --- a/bindings/pyroot/pythonizations/test/memory.py +++ b/bindings/pyroot/pythonizations/test/memory.py @@ -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()