test_properties.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/usr/bin/env python
  2. #
  3. # Author: Mike McKerns (mmckerns @caltech and @uqfoundation)
  4. # Copyright (c) 2008-2016 California Institute of Technology.
  5. # Copyright (c) 2016-2024 The Uncertainty Quantification Foundation.
  6. # License: 3-clause BSD. The full license text is available at:
  7. # - https://github.com/uqfoundation/dill/blob/master/LICENSE
  8. import sys
  9. import dill
  10. dill.settings['recurse'] = True
  11. class Foo(object):
  12. def __init__(self):
  13. self._data = 1
  14. def _get_data(self):
  15. return self._data
  16. def _set_data(self, x):
  17. self._data = x
  18. data = property(_get_data, _set_data)
  19. def test_data_not_none():
  20. FooS = dill.copy(Foo)
  21. assert FooS.data.fget is not None
  22. assert FooS.data.fset is not None
  23. assert FooS.data.fdel is None
  24. def test_data_unchanged():
  25. FooS = dill.copy(Foo)
  26. try:
  27. res = FooS().data
  28. except Exception:
  29. e = sys.exc_info()[1]
  30. raise AssertionError(str(e))
  31. else:
  32. assert res == 1
  33. def test_data_changed():
  34. FooS = dill.copy(Foo)
  35. try:
  36. f = FooS()
  37. f.data = 1024
  38. res = f.data
  39. except Exception:
  40. e = sys.exc_info()[1]
  41. raise AssertionError(str(e))
  42. else:
  43. assert res == 1024
  44. if __name__ == '__main__':
  45. test_data_not_none()
  46. test_data_unchanged()
  47. test_data_changed()