test_selected.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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-2026 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['FileType'].close()
  41. del objects
  42. # getset_descriptor for new-style classes (fails on '_method', if not __main__)
  43. def test_class_descriptors():
  44. d = _d.__dict__
  45. for i in d.values():
  46. ok = dill.pickles(i)
  47. if verbose: print ("%s: %s, %s" % (ok, type(i), i))
  48. assert ok
  49. if verbose: print ("")
  50. od = _newclass.__dict__
  51. for i in od.values():
  52. ok = dill.pickles(i)
  53. if verbose: print ("%s: %s, %s" % (ok, type(i), i))
  54. assert ok
  55. if verbose: print ("")
  56. # (__main__) class instance for new-style classes
  57. def test_class():
  58. o = _d()
  59. oo = _newclass()
  60. ok = dill.pickles(o)
  61. if verbose: print ("%s: %s, %s" % (ok, type(o), o))
  62. assert ok
  63. ok = dill.pickles(oo)
  64. if verbose: print ("%s: %s, %s" % (ok, type(oo), oo))
  65. assert ok
  66. if verbose: print ("")
  67. # frames, generators, and tracebacks (all depend on frame)
  68. def test_frame_related():
  69. g = _g(1)
  70. f = g.gi_frame
  71. e,t = _f()
  72. _is = lambda ok: ok
  73. ok = dill.pickles(f)
  74. if verbose: print ("%s: %s, %s" % (ok, type(f), f))
  75. assert not ok
  76. ok = dill.pickles(g)
  77. if verbose: print ("%s: %s, %s" % (ok, type(g), g))
  78. assert _is(not ok) #XXX: dill fails
  79. ok = dill.pickles(t)
  80. if verbose: print ("%s: %s, %s" % (ok, type(t), t))
  81. assert not ok #XXX: dill fails
  82. ok = dill.pickles(e)
  83. if verbose: print ("%s: %s, %s" % (ok, type(e), e))
  84. assert ok
  85. if verbose: print ("")
  86. def test_typing():
  87. import typing
  88. x = typing.Any
  89. assert x == dill.copy(x)
  90. x = typing.Dict[int, str]
  91. assert x == dill.copy(x)
  92. x = typing.List[int]
  93. assert x == dill.copy(x)
  94. x = typing.Tuple[int, str]
  95. assert x == dill.copy(x)
  96. x = typing.Tuple[int]
  97. assert x == dill.copy(x)
  98. x = typing.Tuple[()]
  99. assert x == dill.copy(x)
  100. x = typing.Tuple[()].copy_with(())
  101. assert x == dill.copy(x)
  102. return
  103. if __name__ == '__main__':
  104. test_frame_related()
  105. test_dict_contents()
  106. test_class()
  107. test_class_descriptors()
  108. test_typing()