Skip to content

Commit e571564

Browse files
committed
pythonization(tcolor): preserve __init__ metadata using functools.wraps (fixes Jupyter TColor.DefinedColors behavior)
1 parent e534276 commit e571564

1 file changed

Lines changed: 19 additions & 8 deletions

File tree

  • bindings/pyroot/pythonizations/python/ROOT/_pythonization

bindings/pyroot/pythonizations/python/ROOT/_pythonization/_tcolor.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,30 @@
77
# For the licensing terms see $ROOTSYS/LICENSE. #
88
# For the list of contributors see $ROOTSYS/README/CREDITS. #
99
################################################################################
10+
11+
import functools
12+
1013
from . import pythonization
1114

12-
def _TColor_constructor(self, *args, **kwargs):
15+
16+
def _tcolor_constructor(original_init):
1317
"""
14-
Forward the arguments to the C++ constructor and retain ownership. This
15-
helps avoiding double deletes due to ROOT automatic memory management.
18+
Wrapper for TColor constructor that retains ownership to avoid double deletes.
19+
Uses functools.wraps to preserve the original function's attributes.
1620
"""
17-
self._cpp_constructor(*args, **kwargs)
18-
import ROOT
19-
ROOT.SetOwnership(self, False)
21+
@functools.wraps(original_init)
22+
def wrapper(self, *args, **kwargs):
23+
"""
24+
Forward the arguments to the C++ constructor and retain ownership. This
25+
helps avoiding double deletes due to ROOT automatic memory management.
26+
"""
27+
original_init(self, *args, **kwargs)
28+
import ROOT
29+
ROOT.SetOwnership(self, False)
30+
return wrapper
2031

2132

2233
@pythonization("TColor")
2334
def pythonize_tcolor(klass):
24-
klass._cpp_constructor = klass.__init__
25-
klass.__init__ = _TColor_constructor
35+
klass.__init__ = _tcolor_constructor(klass.__init__)
36+

0 commit comments

Comments
 (0)