test_selected.py 3.1 KB

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