pickle2.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 pickle2_ext
  6. >>> import pickle
  7. >>> pickle2_ext.world.__module__
  8. 'pickle2_ext'
  9. >>> pickle2_ext.world.__safe_for_unpickling__
  10. 1
  11. >>> pickle2_ext.world.__name__
  12. 'world'
  13. >>> pickle2_ext.world('Hello').__reduce__()
  14. (<class 'pickle2_ext.world'>, ('Hello',), (0,))
  15. >>> for number in (24, 42):
  16. ... wd = pickle2_ext.world('California')
  17. ... wd.set_secret_number(number)
  18. ... pstr = pickle.dumps(wd)
  19. ... wl = pickle.loads(pstr)
  20. ... print(wd.greet(), wd.get_secret_number())
  21. ... print(wl.greet(), wl.get_secret_number())
  22. Hello from California! 24
  23. Hello from California! 24
  24. Hello from California! 42
  25. Hello from California! 0
  26. # Now show that the __dict__ is not taken care of.
  27. >>> wd = pickle2_ext.world('California')
  28. >>> wd.x = 1
  29. >>> wd.__dict__
  30. {'x': 1}
  31. >>> try: pstr = pickle.dumps(wd)
  32. ... except RuntimeError as err: print(err)
  33. ...
  34. Incomplete pickle support (__getstate_manages_dict__ not set)
  35. '''
  36. def run(args = None):
  37. import sys
  38. import doctest
  39. if args is not None:
  40. sys.argv = args
  41. return doctest.testmod(sys.modules.get(__name__))
  42. if __name__ == '__main__':
  43. print("running...")
  44. import sys
  45. status = run()[0]
  46. if (status == 0): print("Done.")
  47. sys.exit(status)