test_selected.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. """
  9. testing some selected object types
  10. """
  11. import dill
  12. dill.settings['recurse'] = True
  13. verbose = False
  14. def test_dict_contents():
  15. c = type.__dict__
  16. for i,j in c.items():
  17. #try:
  18. ok = dill.pickles(j)
  19. #except Exception:
  20. # print ("FAIL: %s with %s" % (i, dill.detect.errors(j)))
  21. if verbose: print ("%s: %s, %s" % (ok, type(j), j))
  22. assert ok
  23. if verbose: print ("")
  24. def _g(x): yield x;
  25. def _f():
  26. try: raise
  27. except Exception:
  28. from sys import exc_info
  29. e, er, tb = exc_info()
  30. return er, tb
  31. class _d(object):
  32. def _method(self):
  33. pass
  34. from dill import objects
  35. from dill import load_types
  36. load_types(pickleable=True,unpickleable=False)
  37. _newclass = objects['ClassObjectType']
  38. # some clean-up #FIXME: should happen internal to dill
  39. objects['TemporaryFileType'].close()
  40. objects['TextWrapperType'].close()
  41. if 'BufferedRandomType' in objects:
  42. objects['BufferedRandomType'].close()
  43. objects['BufferedReaderType'].close()
  44. objects['BufferedWriterType'].close()
  45. objects['FileType'].close()
  46. del objects
  47. # getset_descriptor for new-style classes (fails on '_method', if not __main__)
  48. def test_class_descriptors():
  49. d = _d.__dict__
  50. for i in d.values():
  51. ok = dill.pickles(i)
  52. if verbose: print ("%s: %s, %s" % (ok, type(i), i))
  53. assert ok
  54. if verbose: print ("")
  55. od = _newclass.__dict__
  56. for i in od.values():
  57. ok = dill.pickles(i)
  58. if verbose: print ("%s: %s, %s" % (ok, type(i), i))
  59. assert ok
  60. if verbose: print ("")
  61. # (__main__) class instance for new-style classes
  62. def test_class():
  63. o = _d()
  64. oo = _newclass()
  65. ok = dill.pickles(o)
  66. if verbose: print ("%s: %s, %s" % (ok, type(o), o))
  67. assert ok
  68. ok = dill.pickles(oo)
  69. if verbose: print ("%s: %s, %s" % (ok, type(oo), oo))
  70. assert ok
  71. if verbose: print ("")
  72. # frames, generators, and tracebacks (all depend on frame)
  73. def test_frame_related():
  74. g = _g(1)
  75. f = g.gi_frame
  76. e,t = _f()
  77. _is = lambda ok: ok
  78. ok = dill.pickles(f)
  79. if verbose: print ("%s: %s, %s" % (ok, type(f), f))
  80. assert not ok
  81. ok = dill.pickles(g)
  82. if verbose: print ("%s: %s, %s" % (ok, type(g), g))
  83. assert _is(not ok) #XXX: dill fails
  84. ok = dill.pickles(t)
  85. if verbose: print ("%s: %s, %s" % (ok, type(t), t))
  86. assert not ok #XXX: dill fails
  87. ok = dill.pickles(e)
  88. if verbose: print ("%s: %s, %s" % (ok, type(e), e))
  89. assert ok
  90. if verbose: print ("")
  91. def test_typing():
  92. import typing
  93. x = typing.Any
  94. assert x == dill.copy(x)
  95. x = typing.Dict[int, str]
  96. assert x == dill.copy(x)
  97. x = typing.List[int]
  98. assert x == dill.copy(x)
  99. x = typing.Tuple[int, str]
  100. assert x == dill.copy(x)
  101. x = typing.Tuple[int]
  102. assert x == dill.copy(x)
  103. x = typing.Tuple[()]
  104. assert x == dill.copy(x)
  105. x = typing.Tuple[()].copy_with(())
  106. assert x == dill.copy(x)
  107. return
  108. if __name__ == '__main__':
  109. test_frame_related()
  110. test_dict_contents()
  111. test_class()
  112. test_class_descriptors()
  113. test_typing()