-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathroot_module.py
More file actions
168 lines (128 loc) · 4.87 KB
/
Copy pathroot_module.py
File metadata and controls
168 lines (128 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import unittest
# Helper functions for the tests
def hasattr_recursive(obj, keys):
"""
Check that a Python object has a nested attribute that can be retrieved by
recursively using a given list of keys.
"""
has_first = hasattr(obj, keys[0])
if not has_first:
return False
if len(keys) == 1:
return has_first
return hasattr_recursive(getattr(obj, keys[0]), keys[1:])
def root_module_has(path):
"""
Check if the ROOT module has a certain attribute, that can also be nested.
For example:
root_module_has("RooFit.Experimental")
"""
import ROOT
return hasattr_recursive(ROOT, path.split("."))
class ROOTModule(unittest.TestCase):
"""
Testing features of the ROOT module implemented in the ROOT module facade
"""
def test_import(self):
"""
Test import
"""
import ROOT
self.assertEqual(ROOT.__name__, "ROOT")
def test_relative_import(self):
"""
Test relative import
"""
from ROOT import TH1F
self.assertEqual(TH1F.__name__, "TH1F")
from ROOT import TH1F as Foo
self.assertEqual(Foo.__name__, "TH1F")
self.assertEqual(TH1F, Foo)
def test_version(self):
"""
Test __version__ property
"""
import ROOT
v = ROOT.__version__
self.assertTrue(type(v) == str)
# Fix for #14068: we take into account the different way of expressing the version
# number before and starting with 6.32.00
self.assertIn("/", v) if ROOT.gROOT.GetVersionInt() < 63200 else self.assertNotIn("/", v)
self.assertIn(".", v)
self.assertEqual(v, ROOT.gROOT.GetVersion())
def test_ignore_cmdline_options(self):
"""
Test module flag to ignore command line options
"""
import ROOT
self.assertEqual(ROOT.PyConfig.IgnoreCommandLineOptions, True)
ROOT.PyConfig.IgnoreCommandLineOptions = False
def test_implicit_root_namespace(self):
"""
Test importing implicitly from the ROOT namespace
"""
import ROOT
ROOT.RVec
ROOT.ROOT.RVec
ROOT.VecOps.RVec
ROOT.ROOT.VecOps.RVec
ROOT.EnableImplicitMT()
ROOT.ROOT.EnableImplicitMT()
def test_import_nested_submodules(self):
"""
Test that we can correctly import C++ namespaces and other things that
should behave like Python modules, including nested cases.
"""
#
if root_module_has("RDF.Experimental.Distributed"):
import ROOT.RDF.Distributed
if root_module_has("RNTuple"):
from ROOT import RNTuple
if root_module_has("RooFit.Evaluator"):
from ROOT.RooFit import Evaluator
def test_deprecated_features(self):
"""
Verify that deprecated features are removed in the release where we
announced that they will be removed.
This serves as a reminder to us to delete the deprecated code after
increasing the version number.
Once a deprecated feature was deleted, we can remove the corresponding
check in this test.
"""
import ROOT
version = tuple(int(n) for n in ROOT.__version__.split("."))
if version >= (6, 43, 0):
# Verify that SetHeuristicMemoryPolicy is not available
with self.assertRaises(AttributeError):
ROOT.SetHeuristicMemoryPolicy(True)
ROOT.SetHeuristicMemoryPolicy(False)
if version >= (6, 43, 0) and hasattr(ROOT, "RooArgSet"):
# Verify that the implicit conversion from Python set to RooArgSet
# is gone
x = ROOT.RooRealVar("x", "x", 1.0)
y = ROOT.RooRealVar("y", "y", -1.0)
with self.assertRaises(TypeError):
ROOT.RooDataSet("data", "data", {x, y})
def test_lazy_gdirectory(self):
"""Check that gDirectory is always lazy evaluated to the current
directory.
Reproducer for GitHub issue
https://github.com/root-project/root/issues/21693
"""
import ROOT
# Should be gROOT initially
self.assertEqual(ROOT.gDirectory, ROOT.gROOT)
gDirectory_1 = ROOT.gDirectory
f = ROOT.TFile("Hybrid.root", "RECREATE")
f.cd()
gDirectory_2 = ROOT.gDirectory
# Now, both gDirectory_1 and _2 should resolve to the current TFile
# directory, no matter when the adapters were created.
self.assertEqual(gDirectory_1, gDirectory_2)
self.assertNotEqual(gDirectory_1, ROOT.gROOT)
# If we reset the global directory to nullptr now, it should be considered
with ROOT.TDirectory.TContext(ROOT.nullptr):
self.assertEqual(gDirectory_1, ROOT.nullptr)
self.assertEqual(gDirectory_2, ROOT.nullptr)
if __name__ == "__main__":
unittest.main()