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