test_weakref.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 dill
  9. dill.settings['recurse'] = True
  10. import weakref
  11. class _class:
  12. def _method(self):
  13. pass
  14. class _callable_class:
  15. def __call__(self):
  16. pass
  17. def _function():
  18. pass
  19. def test_weakref():
  20. o = _class()
  21. oc = _callable_class()
  22. f = _function
  23. x = _class
  24. # ReferenceType
  25. r = weakref.ref(o)
  26. d_r = weakref.ref(_class())
  27. fr = weakref.ref(f)
  28. xr = weakref.ref(x)
  29. # ProxyType
  30. p = weakref.proxy(o)
  31. d_p = weakref.proxy(_class())
  32. # CallableProxyType
  33. cp = weakref.proxy(oc)
  34. d_cp = weakref.proxy(_callable_class())
  35. fp = weakref.proxy(f)
  36. xp = weakref.proxy(x)
  37. objlist = [r,d_r,fr,xr, p,d_p, cp,d_cp,fp,xp]
  38. #dill.detect.trace(True)
  39. for obj in objlist:
  40. res = dill.detect.errors(obj)
  41. if res:
  42. print ("%r:\n %s" % (obj, res))
  43. # else:
  44. # print ("PASS: %s" % obj)
  45. assert not res
  46. def test_dictproxy():
  47. from dill._dill import DictProxyType
  48. try:
  49. m = DictProxyType({"foo": "bar"})
  50. except Exception:
  51. m = type.__dict__
  52. mp = dill.copy(m)
  53. assert mp.items() == m.items()
  54. if __name__ == '__main__':
  55. test_weakref()
  56. from dill._dill import IS_PYPY
  57. if not IS_PYPY:
  58. test_dictproxy()