Support copy() in Sentinel
The current impl of Sentinel doesn't return the singleton instance when used with copy.copy(), but it does with copy.deepcopy(). This behavior is inconsistent. This patch adds support for copy.copy() as well as a UT. Change-Id: I33225f5a6e62be9647edacd4b3bd25702e6d8141 Closes-Bug: #1613409
This commit is contained in:
@@ -258,6 +258,10 @@ class Sentinel(object):
|
||||
# Always return the same object because this is essentially a constant.
|
||||
return self
|
||||
|
||||
def __copy__(self):
|
||||
# called via copy.copy(x)
|
||||
return self
|
||||
|
||||
|
||||
#############################
|
||||
# Attribute related constants
|
||||
|
@@ -25,3 +25,7 @@ class TestNeutronLib(base.BaseTestCase):
|
||||
foo = constants.Sentinel()
|
||||
bar = copy.deepcopy(foo)
|
||||
self.assertEqual(id(foo), id(bar))
|
||||
|
||||
def test_sentinel_copy(self):
|
||||
singleton = constants.Sentinel()
|
||||
self.assertEqual(copy.deepcopy(singleton), copy.copy(singleton))
|
||||
|
Reference in New Issue
Block a user