blob: 3865ec6289930061ef06092a8ed5e1c5581bc456 [file] [log] [blame]
Guido van Rossumc41c1a91998-10-22 21:56:15 +00001"""Utility to compile possibly incomplete Python source code."""
2
3import sys
Guido van Rossumc41c1a91998-10-22 21:56:15 +00004import traceback
5
Skip Montanaroe99d5ea2001-01-20 19:54:20 +00006__all__ = ["compile_command"]
7
Guido van Rossumc41c1a91998-10-22 21:56:15 +00008def compile_command(source, filename="<input>", symbol="single"):
9 r"""Compile a command and determine whether it is incomplete.
10
11 Arguments:
12
13 source -- the source string; may contain \n characters
14 filename -- optional filename from which source was read; default "<input>"
15 symbol -- optional grammar start symbol; "single" (default) or "eval"
16
17 Return value / exceptions raised:
18
19 - Return a code object if the command is complete and valid
20 - Return None if the command is incomplete
21 - Raise SyntaxError or OverflowError if the command is a syntax error
22 (OverflowError if the error is in a numeric constant)
23
24 Approach:
25
26 First, check if the source consists entirely of blank lines and
27 comments; if so, replace it with 'pass', because the built-in
28 parser doesn't always do the right thing for these.
29
30 Compile three times: as is, with \n, and with \n\n appended. If
31 it compiles as is, it's complete. If it compiles with one \n
32 appended, we expect more. If it doesn't compile either way, we
33 compare the error we get when compiling with \n or \n\n appended.
34 If the errors are the same, the code is broken. But if the errors
35 are different, we expect more. Not intuitive; not even guaranteed
36 to hold in future releases; but this matches the compiler's
37 behavior from Python 1.4 through 1.5.2, at least.
38
39 Caveat:
40
41 It is possible (but not likely) that the parser stops parsing
42 with a successful outcome before reaching the end of the source;
43 in this case, trailing symbols may be ignored instead of causing an
44 error. For example, a backslash followed by two newlines may be
45 followed by arbitrary garbage. This will be fixed once the API
46 for the parser is better.
47
48 """
49
50 # Check for source consisting of only blank lines and comments
Eric S. Raymond6b71e742001-02-09 08:56:30 +000051 for line in source.split("\n"):
52 line = line.strip()
Guido van Rossumc41c1a91998-10-22 21:56:15 +000053 if line and line[0] != '#':
54 break # Leave it alone
55 else:
56 source = "pass" # Replace it with a 'pass' statement
57
58 err = err1 = err2 = None
59 code = code1 = code2 = None
60
61 try:
62 code = compile(source, filename, symbol)
63 except SyntaxError, err:
64 pass
65
66 try:
67 code1 = compile(source + "\n", filename, symbol)
68 except SyntaxError, err1:
69 pass
70
71 try:
72 code2 = compile(source + "\n\n", filename, symbol)
73 except SyntaxError, err2:
74 pass
75
76 if code:
77 return code
78 try:
79 e1 = err1.__dict__
80 except AttributeError:
81 e1 = err1
82 try:
83 e2 = err2.__dict__
84 except AttributeError:
85 e2 = err2
86 if not code1 and e1 == e2:
87 raise SyntaxError, err1