From 13fe2cf065fc431782459f5d89e2df903e4c40b9 Mon Sep 17 00:00:00 2001 From: Boden R Date: Mon, 15 Aug 2016 13:14:01 -0600 Subject: [PATCH] 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 --- neutron_lib/constants.py | 4 ++++ neutron_lib/tests/unit/test_neutron_lib.py | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/neutron_lib/constants.py b/neutron_lib/constants.py index a49e67a..3af197b 100644 --- a/neutron_lib/constants.py +++ b/neutron_lib/constants.py @@ -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 diff --git a/neutron_lib/tests/unit/test_neutron_lib.py b/neutron_lib/tests/unit/test_neutron_lib.py index b3aeac9..db5089b 100644 --- a/neutron_lib/tests/unit/test_neutron_lib.py +++ b/neutron_lib/tests/unit/test_neutron_lib.py @@ -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))