__init__.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. from .__version__ import __version__
  2. try:
  3. # python3 renamed copy_reg to copyreg
  4. import copyreg
  5. except ImportError:
  6. import copy_reg as copyreg
  7. class Sentinel(object):
  8. _existing_instances = {}
  9. def __init__(self, name):
  10. super(Sentinel, self).__init__()
  11. self._name = name
  12. self._existing_instances[self._name] = self
  13. def __repr__(self):
  14. return "<{0}>".format(self._name)
  15. def __getnewargs__(self):
  16. return (self._name,)
  17. def __new__(cls, name, obj_id=None): # obj_id is for compatibility with previous versions
  18. existing_instance = cls._existing_instances.get(name)
  19. if existing_instance is not None:
  20. return existing_instance
  21. return super(Sentinel, cls).__new__(cls)
  22. def _sentinel_unpickler(name, obj_id=None): # obj_id is for compat. with prev. versions
  23. if name in Sentinel._existing_instances:
  24. return Sentinel._existing_instances[name]
  25. return Sentinel(name)
  26. def _sentinel_pickler(sentinel):
  27. return _sentinel_unpickler, sentinel.__getnewargs__()
  28. copyreg.pickle(Sentinel, _sentinel_pickler, _sentinel_unpickler)
  29. NOTHING = Sentinel('NOTHING')