Barry Warsaw | 04f357c | 2002-07-23 19:04:11 +0000 | [diff] [blame] | 1 | from test.test_support import verbose, TestFailed |
Peter Schneider-Kamp | fdee0f0 | 2000-07-25 22:15:45 +0000 | [diff] [blame] | 2 | |
| 3 | if verbose: |
Jeremy Hylton | 778e265 | 2001-11-09 19:50:08 +0000 | [diff] [blame] | 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__ |
Jeremy Hylton | 79b5b5b | 2001-11-13 22:03:20 +0000 | [diff] [blame] | 12 | prev = __builtin__.__debug__ |
Jeremy Hylton | 778e265 | 2001-11-09 19:50:08 +0000 | [diff] [blame] | 13 | setattr(__builtin__, '__debug__', 'sure') |
Jeremy Hylton | 79b5b5b | 2001-11-13 22:03:20 +0000 | [diff] [blame] | 14 | setattr(__builtin__, '__debug__', prev) |
Jeremy Hylton | 778e265 | 2001-11-09 19:50:08 +0000 | [diff] [blame] | 15 | |
| 16 | if verbose: |
Jeremy Hylton | 047e2c9 | 2001-01-19 03:25:56 +0000 | [diff] [blame] | 17 | print 'Running tests on argument handling' |
Peter Schneider-Kamp | fdee0f0 | 2000-07-25 22:15:45 +0000 | [diff] [blame] | 18 | |
| 19 | try: |
Jeremy Hylton | 121b6eb | 2001-02-19 23:53:42 +0000 | [diff] [blame] | 20 | exec 'def f(a, a): pass' |
Peter Schneider-Kamp | fdee0f0 | 2000-07-25 22:15:45 +0000 | [diff] [blame] | 21 | raise TestFailed, "duplicate arguments" |
| 22 | except SyntaxError: |
| 23 | pass |
| 24 | |
Thomas Heller | 6b17abf | 2002-07-09 09:23:27 +0000 | [diff] [blame] | 25 | if verbose: |
| 26 | print "compiling string with syntax error" |
| 27 | |
| 28 | try: |
| 29 | compile("1+*3", "filename", "exec") |
| 30 | except SyntaxError, detail: |
| 31 | if not detail.filename == "filename": |
| 32 | raise TestFailed, "expected 'filename', got %r" % detail.filename |
| 33 | |
Peter Schneider-Kamp | fdee0f0 | 2000-07-25 22:15:45 +0000 | [diff] [blame] | 34 | try: |
Jeremy Hylton | 121b6eb | 2001-02-19 23:53:42 +0000 | [diff] [blame] | 35 | exec 'def f(a = 0, a = 1): pass' |
Peter Schneider-Kamp | fdee0f0 | 2000-07-25 22:15:45 +0000 | [diff] [blame] | 36 | raise TestFailed, "duplicate keyword arguments" |
| 37 | except SyntaxError: |
| 38 | pass |
Jeremy Hylton | 047e2c9 | 2001-01-19 03:25:56 +0000 | [diff] [blame] | 39 | |
| 40 | try: |
Jeremy Hylton | 121b6eb | 2001-02-19 23:53:42 +0000 | [diff] [blame] | 41 | exec 'def f(a): global a; a = 1' |
Jeremy Hylton | 047e2c9 | 2001-01-19 03:25:56 +0000 | [diff] [blame] | 42 | raise TestFailed, "variable is global and local" |
| 43 | except SyntaxError: |
| 44 | pass |
Jeremy Hylton | 121b6eb | 2001-02-19 23:53:42 +0000 | [diff] [blame] | 45 | |
Jeremy Hylton | 778e265 | 2001-11-09 19:50:08 +0000 | [diff] [blame] | 46 | if verbose: |
| 47 | print "testing complex args" |
Jeremy Hylton | 121b6eb | 2001-02-19 23:53:42 +0000 | [diff] [blame] | 48 | |
| 49 | def comp_args((a, b)): |
Tim Peters | 0009c4e | 2001-02-21 07:29:48 +0000 | [diff] [blame] | 50 | print a,b |
Jeremy Hylton | 121b6eb | 2001-02-19 23:53:42 +0000 | [diff] [blame] | 51 | |
| 52 | comp_args((1, 2)) |
| 53 | |
| 54 | def comp_args((a, b)=(3, 4)): |
| 55 | print a, b |
| 56 | |
| 57 | comp_args((1, 2)) |
| 58 | comp_args() |
| 59 | |
| 60 | def comp_args(a, (b, c)): |
| 61 | print a, b, c |
| 62 | |
| 63 | comp_args(1, (2, 3)) |
| 64 | |
| 65 | def comp_args(a=2, (b, c)=(3, 4)): |
| 66 | print a, b, c |
| 67 | |
| 68 | comp_args(1, (2, 3)) |
| 69 | comp_args() |
| 70 | |
| 71 | try: |
| 72 | exec 'def f(a=1, (b, c)): pass' |
| 73 | raise TestFailed, "non-default args after default" |
| 74 | except SyntaxError: |
| 75 | pass |
Tim Peters | 9aa70d9 | 2001-08-27 19:19:28 +0000 | [diff] [blame] | 76 | |
| 77 | if verbose: |
| 78 | print "testing bad float literals" |
| 79 | |
| 80 | def expect_error(s): |
| 81 | try: |
| 82 | eval(s) |
| 83 | raise TestFailed("%r accepted" % s) |
| 84 | except SyntaxError: |
| 85 | pass |
| 86 | |
| 87 | expect_error("2e") |
| 88 | expect_error("2.0e+") |
| 89 | expect_error("1e-") |
| 90 | expect_error("3-4e/21") |
Tim Peters | d507dab | 2001-08-30 20:51:59 +0000 | [diff] [blame] | 91 | |
| 92 | |
| 93 | if verbose: |
| 94 | print "testing literals with leading zeroes" |
| 95 | |
| 96 | def expect_same(test_source, expected): |
| 97 | got = eval(test_source) |
| 98 | if got != expected: |
| 99 | raise TestFailed("eval(%r) gave %r, but expected %r" % |
| 100 | (test_source, got, expected)) |
| 101 | |
| 102 | expect_error("077787") |
| 103 | expect_error("0xj") |
| 104 | expect_error("0x.") |
| 105 | expect_error("0e") |
| 106 | expect_same("0777", 511) |
| 107 | expect_same("0777L", 511) |
| 108 | expect_same("000777", 511) |
| 109 | expect_same("0xff", 255) |
| 110 | expect_same("0xffL", 255) |
| 111 | expect_same("0XfF", 255) |
| 112 | expect_same("0777.", 777) |
| 113 | expect_same("0777.0", 777) |
| 114 | expect_same("000000000000000000000000000000000000000000000000000777e0", 777) |
| 115 | expect_same("0777e1", 7770) |
| 116 | expect_same("0e0", 0) |
| 117 | expect_same("0000E-012", 0) |
| 118 | expect_same("09.5", 9.5) |
| 119 | expect_same("0777j", 777j) |
| 120 | expect_same("00j", 0j) |
| 121 | expect_same("00.0", 0) |
| 122 | expect_same("0e3", 0) |
| 123 | expect_same("090000000000000.", 90000000000000.) |
| 124 | expect_same("090000000000000.0000000000000000000000", 90000000000000.) |
| 125 | expect_same("090000000000000e0", 90000000000000.) |
| 126 | expect_same("090000000000000e-0", 90000000000000.) |
| 127 | expect_same("090000000000000j", 90000000000000j) |
| 128 | expect_error("090000000000000") # plain octal literal w/ decimal digit |
| 129 | expect_error("080000000000000") # plain octal literal w/ decimal digit |
| 130 | expect_error("000000000000009") # plain octal literal w/ decimal digit |
| 131 | expect_error("000000000000008") # plain octal literal w/ decimal digit |
| 132 | expect_same("000000000000007", 7) |
| 133 | expect_same("000000000000008.", 8.) |
| 134 | expect_same("000000000000009.", 9.) |