test_temp.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 sys
  9. from dill.temp import dump, dump_source, dumpIO, dumpIO_source
  10. from dill.temp import load, load_source, loadIO, loadIO_source
  11. WINDOWS = sys.platform[:3] == 'win'
  12. f = lambda x: x**2
  13. x = [1,2,3,4,5]
  14. # source code to tempfile
  15. def test_code_to_tempfile():
  16. if not WINDOWS: #see: https://bugs.python.org/issue14243
  17. pyfile = dump_source(f, alias='_f')
  18. _f = load_source(pyfile)
  19. assert _f(4) == f(4)
  20. # source code to stream
  21. def test_code_to_stream():
  22. pyfile = dumpIO_source(f, alias='_f')
  23. _f = loadIO_source(pyfile)
  24. assert _f(4) == f(4)
  25. # pickle to tempfile
  26. def test_pickle_to_tempfile():
  27. if not WINDOWS: #see: https://bugs.python.org/issue14243
  28. dumpfile = dump(x)
  29. _x = load(dumpfile)
  30. assert _x == x
  31. # pickle to stream
  32. def test_pickle_to_stream():
  33. dumpfile = dumpIO(x)
  34. _x = loadIO(dumpfile)
  35. assert _x == x
  36. ### now testing the objects ###
  37. f = lambda x: x**2
  38. def g(x): return f(x) - x
  39. def h(x):
  40. def g(x): return x
  41. return g(x) - x
  42. class Foo(object):
  43. def bar(self, x):
  44. return x*x+x
  45. _foo = Foo()
  46. def add(x,y):
  47. return x+y
  48. # yes, same as 'f', but things are tricky when it comes to pointers
  49. squared = lambda x:x**2
  50. class Bar:
  51. pass
  52. _bar = Bar()
  53. # test function-type objects that take 2 args
  54. def test_two_arg_functions():
  55. for obj in [add]:
  56. pyfile = dumpIO_source(obj, alias='_obj')
  57. _obj = loadIO_source(pyfile)
  58. assert _obj(4,2) == obj(4,2)
  59. # test function-type objects that take 1 arg
  60. def test_one_arg_functions():
  61. for obj in [g, h, squared]:
  62. pyfile = dumpIO_source(obj, alias='_obj')
  63. _obj = loadIO_source(pyfile)
  64. assert _obj(4) == obj(4)
  65. # test instance-type objects
  66. #for obj in [_bar, _foo]:
  67. # pyfile = dumpIO_source(obj, alias='_obj')
  68. # _obj = loadIO_source(pyfile)
  69. # assert type(_obj) == type(obj)
  70. # test the rest of the objects
  71. def test_the_rest():
  72. for obj in [Bar, Foo, Foo.bar, _foo.bar]:
  73. pyfile = dumpIO_source(obj, alias='_obj')
  74. _obj = loadIO_source(pyfile)
  75. assert _obj.__name__ == obj.__name__
  76. if __name__ == '__main__':
  77. test_code_to_tempfile()
  78. test_code_to_stream()
  79. test_pickle_to_tempfile()
  80. test_pickle_to_stream()
  81. test_two_arg_functions()
  82. test_one_arg_functions()
  83. test_the_rest()