mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-28 13:21:25 -07:00
Don't use deepcopy when creating attributes unless really needed
This commit is contained in:
parent
79ff2f5e9a
commit
a0748c0837
1 changed files with 15 additions and 3 deletions
|
@ -19,7 +19,10 @@
|
||||||
from __future__ import (absolute_import, division, print_function)
|
from __future__ import (absolute_import, division, print_function)
|
||||||
__metaclass__ = type
|
__metaclass__ = type
|
||||||
|
|
||||||
from copy import deepcopy
|
from copy import copy, deepcopy
|
||||||
|
|
||||||
|
|
||||||
|
_CONTAINERS = frozenset(('list', 'dict', 'set'))
|
||||||
|
|
||||||
|
|
||||||
class Attribute:
|
class Attribute:
|
||||||
|
@ -84,8 +87,17 @@ class Attribute:
|
||||||
self.extend = extend
|
self.extend = extend
|
||||||
self.prepend = prepend
|
self.prepend = prepend
|
||||||
|
|
||||||
if default is not None and self.isa in ('list', 'dict', 'set'):
|
if default is not None and self.isa in _CONTAINERS:
|
||||||
self.default = deepcopy(default)
|
if default:
|
||||||
|
self.default = deepcopy(default)
|
||||||
|
else:
|
||||||
|
# Don't need to deepcopy default if the container is empty
|
||||||
|
# Note: switch to try: except once Python3 is more widespread
|
||||||
|
if hasattr(default, 'copy'):
|
||||||
|
self.default = default.copy()
|
||||||
|
else:
|
||||||
|
# list on python2 does not have .copy()
|
||||||
|
self.default = copy(default)
|
||||||
else:
|
else:
|
||||||
self.default = default
|
self.default = default
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue