test_dictviews.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. #!/usr/bin/env python
  2. #
  3. # Author: Mike McKerns (mmckerns @caltech and @uqfoundation)
  4. # Author: Anirudh Vegesana (avegesan@cs.stanford.edu)
  5. # Copyright (c) 2021-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 dill
  9. from dill._dill import OLD310, MAPPING_PROXY_TRICK, DictProxyType
  10. def test_dictproxy():
  11. assert dill.copy(DictProxyType({'a': 2}))
  12. def test_dictviews():
  13. x = {'a': 1}
  14. assert dill.copy(x.keys())
  15. assert dill.copy(x.values())
  16. assert dill.copy(x.items())
  17. def test_dictproxy_trick():
  18. if not OLD310 and MAPPING_PROXY_TRICK:
  19. x = {'a': 1}
  20. all_views = (x.values(), x.items(), x.keys(), x)
  21. seperate_views = dill.copy(all_views)
  22. new_x = seperate_views[-1]
  23. new_x['b'] = 2
  24. new_x['c'] = 1
  25. assert len(new_x) == 3 and len(x) == 1
  26. assert len(seperate_views[0]) == 3 and len(all_views[0]) == 1
  27. assert len(seperate_views[1]) == 3 and len(all_views[1]) == 1
  28. assert len(seperate_views[2]) == 3 and len(all_views[2]) == 1
  29. assert dict(all_views[1]) == x
  30. assert dict(seperate_views[1]) == new_x
  31. if __name__ == '__main__':
  32. test_dictproxy()
  33. test_dictviews()
  34. test_dictproxy_trick()