Tim Peters | 6cd6a82 | 2001-08-17 22:11:27 +0000 | [diff] [blame] | 1 | r"""Utilities to compile possibly incomplete Python source code. |
Guido van Rossum | c41c1a9 | 1998-10-22 21:56:15 +0000 | [diff] [blame] | 2 | |
Tim Peters | 6cd6a82 | 2001-08-17 22:11:27 +0000 | [diff] [blame] | 3 | This module provides two interfaces, broadly similar to the builtin |
Walter Dörwald | 4df3068 | 2003-11-20 13:38:01 +0000 | [diff] [blame] | 4 | function compile(), which take program text, a filename and a 'mode' |
Tim Peters | 6cd6a82 | 2001-08-17 22:11:27 +0000 | [diff] [blame] | 5 | and: |
Skip Montanaro | e99d5ea | 2001-01-20 19:54:20 +0000 | [diff] [blame] | 6 | |
Walter Dörwald | 4df3068 | 2003-11-20 13:38:01 +0000 | [diff] [blame] | 7 | - Return code object if the command is complete and valid |
| 8 | - Return None if the command is incomplete |
| 9 | - Raise SyntaxError, ValueError or OverflowError if the command is a |
Tim Peters | 6cd6a82 | 2001-08-17 22:11:27 +0000 | [diff] [blame] | 10 | syntax error (OverflowError and ValueError can be produced by |
| 11 | malformed literals). |
Guido van Rossum | c41c1a9 | 1998-10-22 21:56:15 +0000 | [diff] [blame] | 12 | |
Tim Peters | 6cd6a82 | 2001-08-17 22:11:27 +0000 | [diff] [blame] | 13 | Approach: |
Guido van Rossum | c41c1a9 | 1998-10-22 21:56:15 +0000 | [diff] [blame] | 14 | |
Tim Peters | 6cd6a82 | 2001-08-17 22:11:27 +0000 | [diff] [blame] | 15 | First, check if the source consists entirely of blank lines and |
| 16 | comments; if so, replace it with 'pass', because the built-in |
| 17 | parser doesn't always do the right thing for these. |
Guido van Rossum | c41c1a9 | 1998-10-22 21:56:15 +0000 | [diff] [blame] | 18 | |
Tim Peters | 6cd6a82 | 2001-08-17 22:11:27 +0000 | [diff] [blame] | 19 | Compile three times: as is, with \n, and with \n\n appended. If it |
| 20 | compiles as is, it's complete. If it compiles with one \n appended, |
| 21 | we expect more. If it doesn't compile either way, we compare the |
| 22 | error we get when compiling with \n or \n\n appended. If the errors |
| 23 | are the same, the code is broken. But if the errors are different, we |
| 24 | expect more. Not intuitive; not even guaranteed to hold in future |
| 25 | releases; but this matches the compiler's behavior from Python 1.4 |
| 26 | through 2.2, at least. |
Guido van Rossum | c41c1a9 | 1998-10-22 21:56:15 +0000 | [diff] [blame] | 27 | |
Tim Peters | 6cd6a82 | 2001-08-17 22:11:27 +0000 | [diff] [blame] | 28 | Caveat: |
Guido van Rossum | c41c1a9 | 1998-10-22 21:56:15 +0000 | [diff] [blame] | 29 | |
Tim Peters | 6cd6a82 | 2001-08-17 22:11:27 +0000 | [diff] [blame] | 30 | It is possible (but not likely) that the parser stops parsing with a |
| 31 | successful outcome before reaching the end of the source; in this |
| 32 | case, trailing symbols may be ignored instead of causing an error. |
| 33 | For example, a backslash followed by two newlines may be followed by |
| 34 | arbitrary garbage. This will be fixed once the API for the parser is |
| 35 | better. |
Guido van Rossum | c41c1a9 | 1998-10-22 21:56:15 +0000 | [diff] [blame] | 36 | |
Tim Peters | 6cd6a82 | 2001-08-17 22:11:27 +0000 | [diff] [blame] | 37 | The two interfaces are: |
Guido van Rossum | c41c1a9 | 1998-10-22 21:56:15 +0000 | [diff] [blame] | 38 | |
Tim Peters | 6cd6a82 | 2001-08-17 22:11:27 +0000 | [diff] [blame] | 39 | compile_command(source, filename, symbol): |
Guido van Rossum | c41c1a9 | 1998-10-22 21:56:15 +0000 | [diff] [blame] | 40 | |
Tim Peters | 6cd6a82 | 2001-08-17 22:11:27 +0000 | [diff] [blame] | 41 | Compiles a single command in the manner described above. |
Guido van Rossum | c41c1a9 | 1998-10-22 21:56:15 +0000 | [diff] [blame] | 42 | |
Tim Peters | 6cd6a82 | 2001-08-17 22:11:27 +0000 | [diff] [blame] | 43 | CommandCompiler(): |
Guido van Rossum | c41c1a9 | 1998-10-22 21:56:15 +0000 | [diff] [blame] | 44 | |
Tim Peters | 6cd6a82 | 2001-08-17 22:11:27 +0000 | [diff] [blame] | 45 | Instances of this class have __call__ methods identical in |
| 46 | signature to compile_command; the difference is that if the |
| 47 | instance compiles program text containing a __future__ statement, |
| 48 | the instance 'remembers' and compiles all subsequent program texts |
| 49 | with the statement in force. |
Guido van Rossum | c41c1a9 | 1998-10-22 21:56:15 +0000 | [diff] [blame] | 50 | |
Tim Peters | 6cd6a82 | 2001-08-17 22:11:27 +0000 | [diff] [blame] | 51 | The module also provides another class: |
| 52 | |
| 53 | Compile(): |
| 54 | |
| 55 | Instances of this class act like the built-in function compile, |
| 56 | but with 'memory' in the sense described above. |
| 57 | """ |
| 58 | |
| 59 | import __future__ |
Cheryl Sabella | 052d3fc | 2020-06-04 19:40:24 -0400 | [diff] [blame] | 60 | import warnings |
Tim Peters | 6cd6a82 | 2001-08-17 22:11:27 +0000 | [diff] [blame] | 61 | |
| 62 | _features = [getattr(__future__, fname) |
| 63 | for fname in __future__.all_feature_names] |
| 64 | |
| 65 | __all__ = ["compile_command", "Compile", "CommandCompiler"] |
| 66 | |
Terry Jan Reedy | b676f5f | 2021-02-13 01:49:18 -0500 | [diff] [blame^] | 67 | PyCF_DONT_IMPLY_DEDENT = 0x200 # Matches pythonrun.h. |
Guido van Rossum | 4b499dd3 | 2003-02-13 22:07:59 +0000 | [diff] [blame] | 68 | |
Tim Peters | 6cd6a82 | 2001-08-17 22:11:27 +0000 | [diff] [blame] | 69 | def _maybe_compile(compiler, source, filename, symbol): |
Terry Jan Reedy | b676f5f | 2021-02-13 01:49:18 -0500 | [diff] [blame^] | 70 | # Check for source consisting of only blank lines and comments. |
Eric S. Raymond | 6b71e74 | 2001-02-09 08:56:30 +0000 | [diff] [blame] | 71 | for line in source.split("\n"): |
| 72 | line = line.strip() |
Guido van Rossum | c41c1a9 | 1998-10-22 21:56:15 +0000 | [diff] [blame] | 73 | if line and line[0] != '#': |
Terry Jan Reedy | b676f5f | 2021-02-13 01:49:18 -0500 | [diff] [blame^] | 74 | break # Leave it alone. |
Guido van Rossum | c41c1a9 | 1998-10-22 21:56:15 +0000 | [diff] [blame] | 75 | else: |
Guido van Rossum | 993bc3a | 2003-05-16 01:24:30 +0000 | [diff] [blame] | 76 | if symbol != "eval": |
| 77 | source = "pass" # Replace it with a 'pass' statement |
Guido van Rossum | c41c1a9 | 1998-10-22 21:56:15 +0000 | [diff] [blame] | 78 | |
Guido van Rossum | c41c1a9 | 1998-10-22 21:56:15 +0000 | [diff] [blame] | 79 | try: |
Terry Jan Reedy | 2068b26 | 2021-02-11 19:31:10 -0500 | [diff] [blame] | 80 | return compiler(source, filename, symbol) |
Terry Jan Reedy | b676f5f | 2021-02-13 01:49:18 -0500 | [diff] [blame^] | 81 | except SyntaxError: # Let other compile() errors propagate. |
Guido van Rossum | c41c1a9 | 1998-10-22 21:56:15 +0000 | [diff] [blame] | 82 | pass |
| 83 | |
Victor Stinner | 369a1cb | 2020-08-12 14:53:28 +0200 | [diff] [blame] | 84 | # Catch syntax warnings after the first compile |
Terry Jan Reedy | c818b15 | 2020-08-13 13:18:49 -0400 | [diff] [blame] | 85 | # to emit warnings (SyntaxWarning, DeprecationWarning) at most once. |
Cheryl Sabella | 052d3fc | 2020-06-04 19:40:24 -0400 | [diff] [blame] | 86 | with warnings.catch_warnings(): |
Terry Jan Reedy | c818b15 | 2020-08-13 13:18:49 -0400 | [diff] [blame] | 87 | warnings.simplefilter("error") |
Victor Stinner | 369a1cb | 2020-08-12 14:53:28 +0200 | [diff] [blame] | 88 | |
Terry Jan Reedy | b676f5f | 2021-02-13 01:49:18 -0500 | [diff] [blame^] | 89 | code1 = err1 = err2 = None |
Cheryl Sabella | 052d3fc | 2020-06-04 19:40:24 -0400 | [diff] [blame] | 90 | try: |
| 91 | code1 = compiler(source + "\n", filename, symbol) |
| 92 | except SyntaxError as e: |
| 93 | err1 = e |
Guido van Rossum | c41c1a9 | 1998-10-22 21:56:15 +0000 | [diff] [blame] | 94 | |
Cheryl Sabella | 052d3fc | 2020-06-04 19:40:24 -0400 | [diff] [blame] | 95 | try: |
| 96 | code2 = compiler(source + "\n\n", filename, symbol) |
| 97 | except SyntaxError as e: |
| 98 | err2 = e |
Guido van Rossum | c41c1a9 | 1998-10-22 21:56:15 +0000 | [diff] [blame] | 99 | |
Mario Corchero | b64334c | 2019-12-06 14:27:38 +0000 | [diff] [blame] | 100 | try: |
Pablo Galindo | dbb2281 | 2021-02-09 20:07:38 +0000 | [diff] [blame] | 101 | if not code1 and _is_syntax_error(err1, err2): |
Mario Corchero | b64334c | 2019-12-06 14:27:38 +0000 | [diff] [blame] | 102 | raise err1 |
Terry Jan Reedy | b676f5f | 2021-02-13 01:49:18 -0500 | [diff] [blame^] | 103 | else: |
| 104 | return None |
Mario Corchero | b64334c | 2019-12-06 14:27:38 +0000 | [diff] [blame] | 105 | finally: |
| 106 | err1 = err2 = None |
Tim Peters | 6cd6a82 | 2001-08-17 22:11:27 +0000 | [diff] [blame] | 107 | |
Pablo Galindo | dbb2281 | 2021-02-09 20:07:38 +0000 | [diff] [blame] | 108 | def _is_syntax_error(err1, err2): |
| 109 | rep1 = repr(err1) |
| 110 | rep2 = repr(err2) |
| 111 | if "was never closed" in rep1 and "was never closed" in rep2: |
| 112 | return False |
| 113 | if rep1 == rep2: |
| 114 | return True |
| 115 | return False |
| 116 | |
Guido van Rossum | 4b499dd3 | 2003-02-13 22:07:59 +0000 | [diff] [blame] | 117 | def _compile(source, filename, symbol): |
| 118 | return compile(source, filename, symbol, PyCF_DONT_IMPLY_DEDENT) |
| 119 | |
Tim Peters | 6cd6a82 | 2001-08-17 22:11:27 +0000 | [diff] [blame] | 120 | def compile_command(source, filename="<input>", symbol="single"): |
| 121 | r"""Compile a command and determine whether it is incomplete. |
| 122 | |
| 123 | Arguments: |
| 124 | |
| 125 | source -- the source string; may contain \n characters |
| 126 | filename -- optional filename from which source was read; default |
| 127 | "<input>" |
Joannah Nanjekye | 7ba1f75 | 2020-05-14 21:59:46 -0300 | [diff] [blame] | 128 | symbol -- optional grammar start symbol; "single" (default), "exec" |
| 129 | or "eval" |
Tim Peters | 6cd6a82 | 2001-08-17 22:11:27 +0000 | [diff] [blame] | 130 | |
| 131 | Return value / exceptions raised: |
| 132 | |
| 133 | - Return a code object if the command is complete and valid |
| 134 | - Return None if the command is incomplete |
| 135 | - Raise SyntaxError, ValueError or OverflowError if the command is a |
| 136 | syntax error (OverflowError and ValueError can be produced by |
| 137 | malformed literals). |
| 138 | """ |
Guido van Rossum | 4b499dd3 | 2003-02-13 22:07:59 +0000 | [diff] [blame] | 139 | return _maybe_compile(_compile, source, filename, symbol) |
Tim Peters | 6cd6a82 | 2001-08-17 22:11:27 +0000 | [diff] [blame] | 140 | |
| 141 | class Compile: |
| 142 | """Instances of this class behave much like the built-in compile |
| 143 | function, but if one is used to compile text containing a future |
| 144 | statement, it "remembers" and compiles all subsequent program texts |
| 145 | with the statement in force.""" |
| 146 | def __init__(self): |
Guido van Rossum | 4b499dd3 | 2003-02-13 22:07:59 +0000 | [diff] [blame] | 147 | self.flags = PyCF_DONT_IMPLY_DEDENT |
Tim Peters | 6cd6a82 | 2001-08-17 22:11:27 +0000 | [diff] [blame] | 148 | |
| 149 | def __call__(self, source, filename, symbol): |
Serhiy Storchaka | 1f21eaa | 2019-09-01 12:16:51 +0300 | [diff] [blame] | 150 | codeob = compile(source, filename, symbol, self.flags, True) |
Tim Peters | 6cd6a82 | 2001-08-17 22:11:27 +0000 | [diff] [blame] | 151 | for feature in _features: |
| 152 | if codeob.co_flags & feature.compiler_flag: |
| 153 | self.flags |= feature.compiler_flag |
| 154 | return codeob |
| 155 | |
| 156 | class CommandCompiler: |
| 157 | """Instances of this class have __call__ methods identical in |
| 158 | signature to compile_command; the difference is that if the |
| 159 | instance compiles program text containing a __future__ statement, |
| 160 | the instance 'remembers' and compiles all subsequent program texts |
| 161 | with the statement in force.""" |
| 162 | |
| 163 | def __init__(self,): |
| 164 | self.compiler = Compile() |
| 165 | |
| 166 | def __call__(self, source, filename="<input>", symbol="single"): |
| 167 | r"""Compile a command and determine whether it is incomplete. |
| 168 | |
| 169 | Arguments: |
| 170 | |
| 171 | source -- the source string; may contain \n characters |
| 172 | filename -- optional filename from which source was read; |
| 173 | default "<input>" |
| 174 | symbol -- optional grammar start symbol; "single" (default) or |
| 175 | "eval" |
| 176 | |
| 177 | Return value / exceptions raised: |
| 178 | |
| 179 | - Return a code object if the command is complete and valid |
| 180 | - Return None if the command is incomplete |
| 181 | - Raise SyntaxError, ValueError or OverflowError if the command is a |
| 182 | syntax error (OverflowError and ValueError can be produced by |
| 183 | malformed literals). |
| 184 | """ |
| 185 | return _maybe_compile(self.compiler, source, filename, symbol) |