home *** CD-ROM | disk | FTP | other *** search
/ PC World 2002 May / PCWorld_2002-05_cd.bin / Software / TemaCD / activepython / ActivePython-2.1.1.msi / Python21_Lib_test_pickletester.py < prev    next >
Encoding:
Python Source  |  2001-07-26  |  5.1 KB  |  220 lines

  1. # test_pickle and test_cpickle both use this.
  2.  
  3. from test_support import TestFailed
  4. import sys
  5.  
  6. # break into multiple strings to please font-lock-mode
  7. DATA = """(lp1
  8. I0
  9. aL1L
  10. aF2
  11. ac__builtin__
  12. complex
  13. p2
  14. """ + \
  15. """(F3
  16. F0
  17. tRp3
  18. aI1
  19. aI-1
  20. aI255
  21. aI-255
  22. aI-256
  23. aI65535
  24. aI-65535
  25. aI-65536
  26. aI2147483647
  27. aI-2147483647
  28. aI-2147483648
  29. a""" + \
  30. """(S'abc'
  31. p4
  32. g4
  33. """ + \
  34. """(i__main__
  35. C
  36. p5
  37. """ + \
  38. """(dp6
  39. S'foo'
  40. p7
  41. I1
  42. sS'bar'
  43. p8
  44. I2
  45. sbg5
  46. tp9
  47. ag9
  48. aI5
  49. a.
  50. """
  51.  
  52. BINDATA = ']q\x01(K\x00L1L\nG@\x00\x00\x00\x00\x00\x00\x00' + \
  53.           'c__builtin__\ncomplex\nq\x02(G@\x08\x00\x00\x00\x00\x00' + \
  54.           '\x00G\x00\x00\x00\x00\x00\x00\x00\x00tRq\x03K\x01J\xff\xff' + \
  55.           '\xff\xffK\xffJ\x01\xff\xff\xffJ\x00\xff\xff\xffM\xff\xff' + \
  56.           'J\x01\x00\xff\xffJ\x00\x00\xff\xffJ\xff\xff\xff\x7fJ\x01\x00' + \
  57.           '\x00\x80J\x00\x00\x00\x80(U\x03abcq\x04h\x04(c__main__\n' + \
  58.           'C\nq\x05oq\x06}q\x07(U\x03fooq\x08K\x01U\x03barq\tK\x02ubh' + \
  59.           '\x06tq\nh\nK\x05e.'
  60.  
  61. class C:
  62.     def __cmp__(self, other):
  63.         return cmp(self.__dict__, other.__dict__)
  64.  
  65. import __main__
  66. __main__.C = C
  67. C.__module__ = "__main__"
  68.  
  69. # Call this with the module to be tested (pickle or cPickle).
  70.  
  71. def dotest(pickle):
  72.     c = C()
  73.     c.foo = 1
  74.     c.bar = 2
  75.     x = [0, 1L, 2.0, 3.0+0j]
  76.     # Append some integer test cases at cPickle.c's internal size
  77.     # cutoffs.
  78.     uint1max = 0xff
  79.     uint2max = 0xffff
  80.     int4max = 0x7fffffff
  81.     x.extend([1, -1,
  82.               uint1max, -uint1max, -uint1max-1,
  83.               uint2max, -uint2max, -uint2max-1,
  84.                int4max,  -int4max,  -int4max-1])
  85.     y = ('abc', 'abc', c, c)
  86.     x.append(y)
  87.     x.append(y)
  88.     x.append(5)
  89.     r = []
  90.     r.append(r)
  91.  
  92.     print "dumps()"
  93.     s = pickle.dumps(x)
  94.  
  95.     print "loads()"
  96.     x2 = pickle.loads(s)
  97.     if x2 == x:
  98.         print "ok"
  99.     else:
  100.         print "bad"
  101.  
  102.     print "loads() DATA"
  103.     x2 = pickle.loads(DATA)
  104.     if x2 == x:
  105.         print "ok"
  106.     else:
  107.         print "bad"
  108.  
  109.     print "dumps() binary"
  110.     s = pickle.dumps(x, 1)
  111.  
  112.     print "loads() binary"
  113.     x2 = pickle.loads(s)
  114.     if x2 == x:
  115.         print "ok"
  116.     else:
  117.         print "bad"
  118.  
  119.     print "loads() BINDATA"
  120.     x2 = pickle.loads(BINDATA)
  121.     if x2 == x:
  122.         print "ok"
  123.     else:
  124.         print "bad"
  125.  
  126.     print "dumps() RECURSIVE"
  127.     s = pickle.dumps(r)
  128.     x2 = pickle.loads(s)
  129.     if x2 == r:
  130.         print "ok"
  131.     else:
  132.         print "bad"
  133.  
  134.     # don't create cyclic garbage
  135.     del x2[0]
  136.     del r[0]
  137.  
  138.     # Test protection against closed files
  139.     import tempfile, os
  140.     fn = tempfile.mktemp()
  141.     f = open(fn, "w")
  142.     f.close()
  143.     try:
  144.         pickle.dump(123, f)
  145.     except ValueError:
  146.         pass
  147.     else:
  148.         print "dump to closed file should raise ValueError"
  149.  
  150.     f = open(fn, "r")
  151.     f.close()
  152.     try:
  153.         pickle.load(f)
  154.     except ValueError:
  155.         pass
  156.     else:
  157.         print "load from closed file should raise ValueError"
  158.     os.remove(fn)
  159.  
  160.     # Test specific bad cases
  161.     for i in range(10):
  162.         try:
  163.             x = pickle.loads('garyp')
  164.         except KeyError, y:
  165.             # pickle
  166.             del y
  167.         except pickle.BadPickleGet, y:
  168.             # cPickle
  169.             del y
  170.         else:
  171.             print "unexpected success!"
  172.             break
  173.  
  174.     # Test insecure strings
  175.     insecure = ["abc", "2 + 2", # not quoted
  176.                 "'abc' + 'def'", # not a single quoted string
  177.                 "'abc", # quote is not closed
  178.                 "'abc\"", # open quote and close quote don't match
  179.                 "'abc'   ?", # junk after close quote
  180.                 # some tests of the quoting rules
  181.                 "'abc\"\''",
  182.                 "'\\\\a\'\'\'\\\'\\\\\''",
  183.                 ]
  184.     for s in insecure:
  185.         buf = "S" + s + "\012p0\012."
  186.         try:
  187.             x = pickle.loads(buf)
  188.         except ValueError:
  189.             pass
  190.         else:
  191.             print "accepted insecure string: %s" % repr(buf)
  192.  
  193.     # Test some Unicode end cases
  194.     endcases = [u'', u'<\\u>', u'<\\\u1234>', u'<\n>',  u'<\\>']
  195.     for u in endcases:
  196.         try:
  197.             u2 = pickle.loads(pickle.dumps(u))
  198.         except Exception, msg:
  199.             print "Endcase exception: %s => %s(%s)" % \
  200.                   (`u`, msg.__class__.__name__, str(msg))
  201.         else:
  202.             if u2 != u:
  203.                 print "Endcase failure: %s => %s" % (`u`, `u2`)
  204.  
  205.     # Test the full range of Python ints.
  206.     n = sys.maxint
  207.     while n:
  208.         for expected in (-n, n):
  209.             for binary_mode in (0, 1):
  210.                 s = pickle.dumps(expected, binary_mode)
  211.                 got = pickle.loads(s)
  212.                 if expected != got:
  213.                     raise TestFailed("for %s-mode pickle of %d, pickle "
  214.                                      "string is %s, loaded back as %s" % (
  215.                                      binary_mode and "binary" or "text",
  216.                                      expected,
  217.                                      repr(s),
  218.                                      got))
  219.         n = n >> 1
  220.