Guido van Rossum | c41c1a9 | 1998-10-22 21:56:15 +0000 | [diff] [blame] | 1 | """Utility to compile possibly incomplete Python source code.""" |
| 2 | |
| 3 | import sys |
Guido van Rossum | c41c1a9 | 1998-10-22 21:56:15 +0000 | [diff] [blame] | 4 | import traceback |
| 5 | |
Skip Montanaro | e99d5ea | 2001-01-20 19:54:20 +0000 | [diff] [blame] | 6 | __all__ = ["compile_command"] |
| 7 | |
Guido van Rossum | c41c1a9 | 1998-10-22 21:56:15 +0000 | [diff] [blame] | 8 | def 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. Raymond | 6b71e74 | 2001-02-09 08:56:30 +0000 | [diff] [blame^] | 51 | for line in source.split("\n"): |
| 52 | line = line.strip() |
Guido van Rossum | c41c1a9 | 1998-10-22 21:56:15 +0000 | [diff] [blame] | 53 | 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 |