home *** CD-ROM | disk | FTP | other *** search
/ Komputer for Alle 2004 #2 / K-CD-2-2004.ISO / OpenOffice Sv / f_0397 / python-core-2.2.2 / lib / test / test_compile.py < prev    next >
Encoding:
Python Source  |  2003-07-18  |  2.9 KB  |  126 lines

  1. from test_support import verbose, TestFailed
  2.  
  3. if verbose:
  4.     print "Testing whether compiler catches assignment to __debug__"
  5.  
  6. try:
  7.     compile('__debug__ = 1', '?', 'single')
  8. except SyntaxError:
  9.     pass
  10.  
  11. import __builtin__
  12. prev = __builtin__.__debug__
  13. setattr(__builtin__, '__debug__', 'sure')
  14. setattr(__builtin__, '__debug__', prev)
  15.  
  16. if verbose:
  17.     print 'Running tests on argument handling'
  18.  
  19. try:
  20.     exec 'def f(a, a): pass'
  21.     raise TestFailed, "duplicate arguments"
  22. except SyntaxError:
  23.     pass
  24.  
  25. try:
  26.     exec 'def f(a = 0, a = 1): pass'
  27.     raise TestFailed, "duplicate keyword arguments"
  28. except SyntaxError:
  29.     pass
  30.  
  31. try:
  32.     exec 'def f(a): global a; a = 1'
  33.     raise TestFailed, "variable is global and local"
  34. except SyntaxError:
  35.     pass
  36.  
  37. if verbose:
  38.     print "testing complex args"
  39.  
  40. def comp_args((a, b)):
  41.     print a,b
  42.  
  43. comp_args((1, 2))
  44.  
  45. def comp_args((a, b)=(3, 4)):
  46.     print a, b
  47.  
  48. comp_args((1, 2))
  49. comp_args()
  50.  
  51. def comp_args(a, (b, c)):
  52.     print a, b, c
  53.  
  54. comp_args(1, (2, 3))
  55.  
  56. def comp_args(a=2, (b, c)=(3, 4)):
  57.     print a, b, c
  58.  
  59. comp_args(1, (2, 3))
  60. comp_args()
  61.  
  62. try:
  63.     exec 'def f(a=1, (b, c)): pass'
  64.     raise TestFailed, "non-default args after default"
  65. except SyntaxError:
  66.     pass
  67.  
  68. if verbose:
  69.     print "testing bad float literals"
  70.  
  71. def expect_error(s):
  72.     try:
  73.         eval(s)
  74.         raise TestFailed("%r accepted" % s)
  75.     except SyntaxError:
  76.         pass
  77.  
  78. expect_error("2e")
  79. expect_error("2.0e+")
  80. expect_error("1e-")
  81. expect_error("3-4e/21")
  82.  
  83.  
  84. if verbose:
  85.     print "testing literals with leading zeroes"
  86.  
  87. def expect_same(test_source, expected):
  88.     got = eval(test_source)
  89.     if got != expected:
  90.         raise TestFailed("eval(%r) gave %r, but expected %r" %
  91.                          (test_source, got, expected))
  92.  
  93. expect_error("077787")
  94. expect_error("0xj")
  95. expect_error("0x.")
  96. expect_error("0e")
  97. expect_same("0777", 511)
  98. expect_same("0777L", 511)
  99. expect_same("000777", 511)
  100. expect_same("0xff", 255)
  101. expect_same("0xffL", 255)
  102. expect_same("0XfF", 255)
  103. expect_same("0777.", 777)
  104. expect_same("0777.0", 777)
  105. expect_same("000000000000000000000000000000000000000000000000000777e0", 777)
  106. expect_same("0777e1", 7770)
  107. expect_same("0e0", 0)
  108. expect_same("0000E-012", 0)
  109. expect_same("09.5", 9.5)
  110. expect_same("0777j", 777j)
  111. expect_same("00j", 0j)
  112. expect_same("00.0", 0)
  113. expect_same("0e3", 0)
  114. expect_same("090000000000000.", 90000000000000.)
  115. expect_same("090000000000000.0000000000000000000000", 90000000000000.)
  116. expect_same("090000000000000e0", 90000000000000.)
  117. expect_same("090000000000000e-0", 90000000000000.)
  118. expect_same("090000000000000j", 90000000000000j)
  119. expect_error("090000000000000")  # plain octal literal w/ decimal digit
  120. expect_error("080000000000000")  # plain octal literal w/ decimal digit
  121. expect_error("000000000000009")  # plain octal literal w/ decimal digit
  122. expect_error("000000000000008")  # plain octal literal w/ decimal digit
  123. expect_same("000000000000007", 7)
  124. expect_same("000000000000008.", 8.)
  125. expect_same("000000000000009.", 9.)
  126.