Skip to content

Commit

Permalink
Merge pull request #37903 from Dr15Jones/checkModifiers
Browse files Browse the repository at this point in the history
Enforce Modifier consistency between Process instances
  • Loading branch information
cmsbuild committed May 13, 2022
2 parents fb5a19e + 5f88589 commit ae23e23
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions FWCore/ParameterSet/python/Config.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ class Temp(object):

class Process(object):
"""Root class for a CMS configuration process"""
_firstProcess = True
def __init__(self,name,*Mods):
"""The argument 'name' will be the name applied to this Process
Can optionally pass as additional arguments cms.Modifier instances
Expand Down Expand Up @@ -146,6 +147,13 @@ def __init__(self,name,*Mods):
# FWCore.Message(Logger|Service).MessageLogger_cfi
# use the very same MessageLogger object.
self.MessageLogger = MessageLogger
if Process._firstProcess:
Process._firstProcess = False
else:
if len(Mods) > 0:
for m in self.__modifiers:
if not m._isChosen():
raise RuntimeError("The Process {} tried to redefine which Modifiers to use after another Process was already started".format(name))
for m in self.__modifiers:
m._setChosen()

Expand Down Expand Up @@ -4061,6 +4069,11 @@ def testDelete(self):
self.assertEqual(p.schedule_().dumpPython(), 'cms.Schedule(tasks=[cms.Task(process.h)])\n')
def testModifier(self):
m1 = Modifier()
Process._firstProcess = True
p = Process("test")
self.assertRaises(RuntimeError, lambda: Process("test2", m1))
m1 = Modifier()
Process._firstProcess = True
p = Process("test",m1)
p.a = EDAnalyzer("MyAnalyzer", fred = int32(1))
def _mod_fred(obj):
Expand All @@ -4073,6 +4086,7 @@ def _mod_fred(obj):
self.assertTrue(p.isUsingModifier(m1))
#check that Modifier not attached to a process doesn't run
m1 = Modifier()
Process._firstProcess = True
p = Process("test")
p.a = EDAnalyzer("MyAnalyzer", fred = int32(1))
m1.toModify(p.a,_mod_fred)
Expand All @@ -4083,6 +4097,7 @@ def _mod_fred(obj):
self.assertEqual(p.isUsingModifier(m1),False)
#make sure clones get the changes
m1 = Modifier()
Process._firstProcess = True
p = Process("test",m1)
p.a = EDAnalyzer("MyAnalyzer", fred = int32(1), wilma = int32(1))
m1.toModify(p.a, fred = int32(2))
Expand All @@ -4093,6 +4108,7 @@ def _mod_fred(obj):
self.assertEqual(p.b.wilma.value(),3)
#test removal of parameter
m1 = Modifier()
Process._firstProcess = True
p = Process("test",m1)
p.a = EDAnalyzer("MyAnalyzer", fred = int32(1), wilma = int32(1), fintstones = PSet(fred = int32(1)))
m1.toModify(p.a, fred = None, fintstones = dict(fred = None))
Expand All @@ -4101,33 +4117,38 @@ def _mod_fred(obj):
self.assertEqual(p.a.wilma.value(),1)
#test adding a parameter
m1 = Modifier()
Process._firstProcess = True
p = Process("test",m1)
p.a = EDAnalyzer("MyAnalyzer", fred = int32(1))
m1.toModify(p.a, wilma = int32(2))
self.assertEqual(p.a.fred.value(), 1)
self.assertEqual(p.a.wilma.value(),2)
#test setting of value in PSet
m1 = Modifier()
Process._firstProcess = True
p = Process("test",m1)
p.a = EDAnalyzer("MyAnalyzer", flintstones = PSet(fred = int32(1), wilma = int32(1)))
m1.toModify(p.a, flintstones = dict(fred = int32(2)))
self.assertEqual(p.a.flintstones.fred.value(),2)
self.assertEqual(p.a.flintstones.wilma.value(),1)
#test proper exception from nonexisting parameter name
m1 = Modifier()
Process._firstProcess = True
p = Process("test",m1)
p.a = EDAnalyzer("MyAnalyzer", flintstones = PSet(fred = PSet(wilma = int32(1))))
self.assertRaises(KeyError, lambda: m1.toModify(p.a, flintstones = dict(imnothere = dict(wilma=2))))
self.assertRaises(KeyError, lambda: m1.toModify(p.a, foo = 1))
#test setting a value in a VPSet
m1 = Modifier()
Process._firstProcess = True
p = Process("test",m1)
p.a = EDAnalyzer("MyAnalyzer", flintstones = VPSet(PSet(fred = int32(1)), PSet(wilma = int32(1))))
m1.toModify(p.a, flintstones = {1:dict(wilma = int32(2))})
self.assertEqual(p.a.flintstones[0].fred.value(),1)
self.assertEqual(p.a.flintstones[1].wilma.value(),2)
#test setting a value in a list of values
m1 = Modifier()
Process._firstProcess = True
p = Process("test",m1)
p.a = EDAnalyzer("MyAnalyzer", fred = vuint32(1,2,3))
m1.toModify(p.a, fred = {1:7})
Expand All @@ -4136,6 +4157,7 @@ def _mod_fred(obj):
self.assertEqual(p.a.fred[2],3)
#test IndexError setting a value in a list to an item key not in the list
m1 = Modifier()
Process._firstProcess = True
p = Process("test",m1)
p.a = EDAnalyzer("MyAnalyzer", fred = vuint32(1,2,3))
raised = False
Expand All @@ -4144,6 +4166,7 @@ def _mod_fred(obj):
self.assertEqual(raised, True)
#test TypeError setting a value in a list using a key that is not an int
m1 = Modifier()
Process._firstProcess = True
p = Process("test",m1)
p.a = EDAnalyzer("MyAnalyzer", flintstones = VPSet(PSet(fred = int32(1)), PSet(wilma = int32(1))))
raised = False
Expand All @@ -4163,6 +4186,7 @@ def __init__(self):
p.extend(testMod)
self.assertTrue(hasattr(p,"a"))
m1 = Modifier()
Process._firstProcess = True
p = Process("test",m1)
testProcMod = ProcModifierMod(m1,_rem_a)
p.extend(testMod)
Expand All @@ -4171,6 +4195,7 @@ def __init__(self):
#test ModifierChain
m1 = Modifier()
mc = ModifierChain(m1)
Process._firstProcess = True
p = Process("test",mc)
self.assertTrue(p.isUsingModifier(m1))
self.assertTrue(p.isUsingModifier(mc))
Expand Down Expand Up @@ -4198,20 +4223,23 @@ def __init__(self):
#check combining
m1 = Modifier()
m2 = Modifier()
Process._firstProcess = True
p = Process("test",m1)
p.a = EDAnalyzer("MyAnalyzer", fred = int32(1), wilma = int32(1))
(m1 & m2).toModify(p.a, fred = int32(2))
self.assertRaises(TypeError, lambda: (m1 & m2).toModify(p.a, 1, wilma=2))
self.assertEqual(p.a.fred, 1)
m1 = Modifier()
m2 = Modifier()
Process._firstProcess = True
p = Process("test",m1,m2)
p.a = EDAnalyzer("MyAnalyzer", fred = int32(1), wilma = int32(1))
(m1 & m2).toModify(p.a, fred = int32(2))
self.assertEqual(p.a.fred, 2)
m1 = Modifier()
m2 = Modifier()
m3 = Modifier()
Process._firstProcess = True
p = Process("test",m1,m2,m3)
p.a = EDAnalyzer("MyAnalyzer", fred = int32(1), wilma = int32(1))
(m1 & m2 & m3).toModify(p.a, fred = int32(2))
Expand All @@ -4223,6 +4251,7 @@ def __init__(self):
#check inverse
m1 = Modifier()
m2 = Modifier()
Process._firstProcess = True
p = Process("test", m1)
p.a = EDAnalyzer("MyAnalyzer", fred = int32(1), wilma = int32(1))
(~m1).toModify(p.a, fred=2)
Expand All @@ -4235,6 +4264,7 @@ def __init__(self):
m1 = Modifier()
m2 = Modifier()
m3 = Modifier()
Process._firstProcess = True
p = Process("test", m1)
p.a = EDAnalyzer("MyAnalyzer", fred = int32(1), wilma = int32(1))
(m1 | m2).toModify(p.a, fred=2)
Expand All @@ -4256,6 +4286,7 @@ def __init__(self):
m2 = Modifier()
m3 = Modifier()
m4 = Modifier()
Process._firstProcess = True
p = Process("test", m1, m2)
p.a = EDAnalyzer("MyAnalyzer", fred = int32(1), wilma = int32(1))
(m1 & ~m2).toModify(p.a, fred=2)
Expand All @@ -4272,6 +4303,7 @@ def __init__(self):
self.assertEqual(p.a.fred, 5)
#check toReplaceWith
m1 = Modifier()
Process._firstProcess = True
p = Process("test",m1)
p.a =EDAnalyzer("MyAnalyzer", fred = int32(1))
m1.toReplaceWith(p.a, EDAnalyzer("YourAnalyzer", wilma = int32(3)))
Expand Down Expand Up @@ -4310,6 +4342,7 @@ def __init__(self):
self.assertTrue(p.td._collection == OrderedSet([p.e]))
#check toReplaceWith doesn't activate not chosen
m1 = Modifier()
Process._firstProcess = True
p = Process("test")
p.a =EDAnalyzer("MyAnalyzer", fred = int32(1))
m1.toReplaceWith(p.a, EDAnalyzer("YourAnalyzer", wilma = int32(3)))
Expand All @@ -4319,6 +4352,7 @@ def __init__(self):
m2 = Modifier()
m3 = Modifier()
m4 = Modifier()
Process._firstProcess = True
p = Process("test", m1, m2)
p.a = EDAnalyzer("MyAnalyzer", fred = int32(1), wilma = int32(1))
self.assertRaises(TypeError, lambda: (m1 & m2).toReplaceWith(p.a, EDProducer("YourProducer")))
Expand Down

0 comments on commit ae23e23

Please sign in to comment.