ENH: cannot create two sentinels with same name and different doc

This commit is contained in:
llllllllll
2015-11-24 16:45:52 -05:00
parent 68a08d8c2a
commit c62ac9ba74
2 changed files with 17 additions and 3 deletions
+7 -1
View File
@@ -17,7 +17,13 @@ class SentinelTestCase(TestCase):
self.assertEqual(sentinel('a', 'b').__doc__, 'b')
def test_doc_differentiates(self):
self.assertIsNot(sentinel('a', 'b'), sentinel('a', 'c'))
a = sentinel('sentinel-name', 'original-doc')
with self.assertRaises(ValueError) as e:
sentinel(a.__name__, 'new-doc')
msg = str(e.exception)
self.assertIn(a.__name__, msg)
self.assertIn(a.__doc__, msg)
def test_memo(self):
self.assertIs(sentinel('a'), sentinel('a'))
+10 -2
View File
@@ -8,9 +8,17 @@ import sys
def sentinel(name, doc=None):
try:
return sentinel._cache[name, doc] # memoized
value = sentinel._cache[name] # memoized
except KeyError:
pass
else:
if doc == value.__doc__:
return value
raise ValueError(
'attempted to create sentinel with a used name and a different'
' docstring: %r\noriginal docstring:\n%s' % (name, value.__doc__),
)
@object.__new__ # bind a single instance to the name 'Sentinel'
class Sentinel(object):
@@ -41,6 +49,6 @@ def sentinel(name, doc=None):
# Couldn't get the name from the calling scope, just use None.
cls.__module__ = None
sentinel._cache[name, doc] = Sentinel # cache result
sentinel._cache[name] = Sentinel # cache result
return Sentinel
sentinel._cache = {}