1 """Utility to compile possibly incomplete Python source code."""
6 __all__
= ["compile_command"]
8 def compile_command(source
, filename
="<input>", symbol
="single"):
9 r
"""Compile a command and determine whether it is incomplete.
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"
17 Return value / exceptions raised:
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)
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.
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.
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.
50 # Check for source consisting of only blank lines and comments
51 for line
in source
.split("\n"):
53 if line
and line
[0] != '#':
54 break # Leave it alone
56 source
= "pass" # Replace it with a 'pass' statement
58 err
= err1
= err2
= None
59 code
= code1
= code2
= None
62 code
= compile(source
, filename
, symbol
)
63 except SyntaxError, err
:
67 code1
= compile(source
+ "\n", filename
, symbol
)
68 except SyntaxError, err1
:
72 code2
= compile(source
+ "\n\n", filename
, symbol
)
73 except SyntaxError, err2
:
80 except AttributeError:
84 except AttributeError:
86 if not code1
and e1
== e2
:
87 raise SyntaxError, err1