| Conditions | 3 |
| Total Lines | 37 |
| Lines | 0 |
| Ratio | 0 % |
| 1 | """ |
||
| 9 | def sentinel(name, doc=None): |
||
| 10 | try: |
||
| 11 | return sentinel._cache[name, doc] # memoized |
||
| 12 | except KeyError: |
||
| 13 | pass |
||
| 14 | |||
| 15 | @object.__new__ # bind a single instance to the name 'Sentinel' |
||
| 16 | class Sentinel(object): |
||
| 17 | __doc__ = doc |
||
| 18 | __slots__ = ('__weakref__',) |
||
| 19 | __name__ = name |
||
| 20 | |||
| 21 | def __new__(cls): |
||
| 22 | raise TypeError("Can't construct new instances of %r" % name) |
||
| 23 | |||
| 24 | def __repr__(self): |
||
| 25 | return 'sentinel(%r)' % name |
||
| 26 | |||
| 27 | def __reduce__(self): |
||
| 28 | return sentinel, (name, doc) |
||
| 29 | |||
| 30 | def __deepcopy__(self, _memo): |
||
| 31 | return self |
||
| 32 | |||
| 33 | def __copy__(self): |
||
| 34 | return self |
||
| 35 | |||
| 36 | cls = type(Sentinel) |
||
| 37 | try: |
||
| 38 | # traverse up one frame to find the module where this is defined |
||
| 39 | cls.__module__ = sys._getframe(1).f_globals['__name__'] |
||
| 40 | except (AttributeError, ValueError, KeyError): |
||
| 41 | # Couldn't get the name from the calling scope, just use None. |
||
| 42 | cls.__module__ = None |
||
| 43 | |||
| 44 | sentinel._cache[name, doc] = Sentinel # cache result |
||
| 45 | return Sentinel |
||
| 46 | sentinel._cache = {} |
||
| 47 |