pickle3.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # Copyright David Abrahams 2004. Distributed under the Boost
  2. # Software License, Version 1.0. (See accompanying
  3. # file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. from __future__ import print_function
  5. r'''>>> import pickle3_ext
  6. >>> import pickle
  7. >>> pickle3_ext.world.__module__
  8. 'pickle3_ext'
  9. >>> pickle3_ext.world.__safe_for_unpickling__
  10. 1
  11. >>> pickle3_ext.world.__getstate_manages_dict__
  12. 1
  13. >>> pickle3_ext.world.__name__
  14. 'world'
  15. >>> pickle3_ext.world('Hello').__reduce__()
  16. (<class 'pickle3_ext.world'>, ('Hello',), ({}, 0))
  17. >>> for number in (24, 42):
  18. ... wd = pickle3_ext.world('California')
  19. ... wd.set_secret_number(number)
  20. ... wd.x = 2 * number
  21. ... wd.y = 'y' * number
  22. ... wd.z = 3. * number
  23. ... pstr = pickle.dumps(wd)
  24. ... wl = pickle.loads(pstr)
  25. ... print(wd.greet(), wd.get_secret_number(), wd.x, wd.y, wd.z)
  26. ... print(wl.greet(), wl.get_secret_number(), wl.x, wl.y, wl.z)
  27. Hello from California! 24 48 yyyyyyyyyyyyyyyyyyyyyyyy 72.0
  28. Hello from California! 24 48 yyyyyyyyyyyyyyyyyyyyyyyy 72.0
  29. Hello from California! 42 84 yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy 126.0
  30. Hello from California! 0 84 yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy 126.0
  31. '''
  32. def run(args = None):
  33. import sys
  34. import doctest
  35. if args is not None:
  36. sys.argv = args
  37. return doctest.testmod(sys.modules.get(__name__))
  38. if __name__ == '__main__':
  39. print("running...")
  40. import sys
  41. status = run()[0]
  42. if (status == 0): print("Done.")
  43. sys.exit(status)