Fix classes that select a subclass based on platform (#17034)
When unittesting this we found that the platform selecting class hierarchies weren't working in all cases. If the subclass was directly created (ie: LinuxHardware()), then it would use its inherited __new__() to try to create itself. The inherited __new__ would look for subclasses and end up calling its own __new__() again. This would recurse endlessly. The new code detects when we want to find a subclass to create (when the base class is used, ie: Hardware()) vs when to create the class itself (when the subclass is used, ie: LinuxHardware()).
This commit is contained in:
parent
b82d150c42
commit
854d47826c
1 changed files with 18 additions and 0 deletions
|
@ -961,6 +961,12 @@ class Hardware(Facts):
|
||||||
platform = 'Generic'
|
platform = 'Generic'
|
||||||
|
|
||||||
def __new__(cls, *arguments, **keyword):
|
def __new__(cls, *arguments, **keyword):
|
||||||
|
# When Hardware is created, it chooses a subclass to create instead.
|
||||||
|
# This check prevents the subclass from then trying to find a subclass
|
||||||
|
# and create that.
|
||||||
|
if cls is not Hardware:
|
||||||
|
return super(Hardware, cls).__new__(cls)
|
||||||
|
|
||||||
subclass = cls
|
subclass = cls
|
||||||
for sc in get_all_subclasses(Hardware):
|
for sc in get_all_subclasses(Hardware):
|
||||||
if sc.platform == platform.system():
|
if sc.platform == platform.system():
|
||||||
|
@ -2052,6 +2058,12 @@ class Network(Facts):
|
||||||
'80' : 'organization' }
|
'80' : 'organization' }
|
||||||
|
|
||||||
def __new__(cls, *arguments, **keyword):
|
def __new__(cls, *arguments, **keyword):
|
||||||
|
# When Network is created, it chooses a subclass to create instead.
|
||||||
|
# This check prevents the subclass from then trying to find a subclass
|
||||||
|
# and create that.
|
||||||
|
if cls is not Network:
|
||||||
|
return super(Network, cls).__new__(cls)
|
||||||
|
|
||||||
subclass = cls
|
subclass = cls
|
||||||
for sc in get_all_subclasses(Network):
|
for sc in get_all_subclasses(Network):
|
||||||
if sc.platform == platform.system():
|
if sc.platform == platform.system():
|
||||||
|
@ -2836,6 +2848,12 @@ class Virtual(Facts):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __new__(cls, *arguments, **keyword):
|
def __new__(cls, *arguments, **keyword):
|
||||||
|
# When Virtual is created, it chooses a subclass to create instead.
|
||||||
|
# This check prevents the subclass from then trying to find a subclass
|
||||||
|
# and create that.
|
||||||
|
if cls is not Virtual:
|
||||||
|
return super(Virtual, cls).__new__(cls)
|
||||||
|
|
||||||
subclass = cls
|
subclass = cls
|
||||||
for sc in get_all_subclasses(Virtual):
|
for sc in get_all_subclasses(Virtual):
|
||||||
if sc.platform == platform.system():
|
if sc.platform == platform.system():
|
||||||
|
|
Loading…
Add table
Reference in a new issue