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:
Boden R
2016-08-15 13:14:01 -06:00
parent b2975156dc
commit 13fe2cf065
2 changed files with 8 additions and 0 deletions

View File

@@ -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

View File

@@ -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))