| 1 | n/a | r"""Utilities to compile possibly incomplete Python source code. |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | This module provides two interfaces, broadly similar to the builtin |
|---|
| 4 | n/a | function compile(), which take program text, a filename and a 'mode' |
|---|
| 5 | n/a | and: |
|---|
| 6 | n/a | |
|---|
| 7 | n/a | - Return code object if the command is complete and valid |
|---|
| 8 | n/a | - Return None if the command is incomplete |
|---|
| 9 | n/a | - Raise SyntaxError, ValueError or OverflowError if the command is a |
|---|
| 10 | n/a | syntax error (OverflowError and ValueError can be produced by |
|---|
| 11 | n/a | malformed literals). |
|---|
| 12 | n/a | |
|---|
| 13 | n/a | Approach: |
|---|
| 14 | n/a | |
|---|
| 15 | n/a | First, check if the source consists entirely of blank lines and |
|---|
| 16 | n/a | comments; if so, replace it with 'pass', because the built-in |
|---|
| 17 | n/a | parser doesn't always do the right thing for these. |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | Compile three times: as is, with \n, and with \n\n appended. If it |
|---|
| 20 | n/a | compiles as is, it's complete. If it compiles with one \n appended, |
|---|
| 21 | n/a | we expect more. If it doesn't compile either way, we compare the |
|---|
| 22 | n/a | error we get when compiling with \n or \n\n appended. If the errors |
|---|
| 23 | n/a | are the same, the code is broken. But if the errors are different, we |
|---|
| 24 | n/a | expect more. Not intuitive; not even guaranteed to hold in future |
|---|
| 25 | n/a | releases; but this matches the compiler's behavior from Python 1.4 |
|---|
| 26 | n/a | through 2.2, at least. |
|---|
| 27 | n/a | |
|---|
| 28 | n/a | Caveat: |
|---|
| 29 | n/a | |
|---|
| 30 | n/a | It is possible (but not likely) that the parser stops parsing with a |
|---|
| 31 | n/a | successful outcome before reaching the end of the source; in this |
|---|
| 32 | n/a | case, trailing symbols may be ignored instead of causing an error. |
|---|
| 33 | n/a | For example, a backslash followed by two newlines may be followed by |
|---|
| 34 | n/a | arbitrary garbage. This will be fixed once the API for the parser is |
|---|
| 35 | n/a | better. |
|---|
| 36 | n/a | |
|---|
| 37 | n/a | The two interfaces are: |
|---|
| 38 | n/a | |
|---|
| 39 | n/a | compile_command(source, filename, symbol): |
|---|
| 40 | n/a | |
|---|
| 41 | n/a | Compiles a single command in the manner described above. |
|---|
| 42 | n/a | |
|---|
| 43 | n/a | CommandCompiler(): |
|---|
| 44 | n/a | |
|---|
| 45 | n/a | Instances of this class have __call__ methods identical in |
|---|
| 46 | n/a | signature to compile_command; the difference is that if the |
|---|
| 47 | n/a | instance compiles program text containing a __future__ statement, |
|---|
| 48 | n/a | the instance 'remembers' and compiles all subsequent program texts |
|---|
| 49 | n/a | with the statement in force. |
|---|
| 50 | n/a | |
|---|
| 51 | n/a | The module also provides another class: |
|---|
| 52 | n/a | |
|---|
| 53 | n/a | Compile(): |
|---|
| 54 | n/a | |
|---|
| 55 | n/a | Instances of this class act like the built-in function compile, |
|---|
| 56 | n/a | but with 'memory' in the sense described above. |
|---|
| 57 | n/a | """ |
|---|
| 58 | n/a | |
|---|
| 59 | n/a | import __future__ |
|---|
| 60 | n/a | |
|---|
| 61 | n/a | _features = [getattr(__future__, fname) |
|---|
| 62 | n/a | for fname in __future__.all_feature_names] |
|---|
| 63 | n/a | |
|---|
| 64 | n/a | __all__ = ["compile_command", "Compile", "CommandCompiler"] |
|---|
| 65 | n/a | |
|---|
| 66 | n/a | PyCF_DONT_IMPLY_DEDENT = 0x200 # Matches pythonrun.h |
|---|
| 67 | n/a | |
|---|
| 68 | n/a | def _maybe_compile(compiler, source, filename, symbol): |
|---|
| 69 | n/a | # Check for source consisting of only blank lines and comments |
|---|
| 70 | n/a | for line in source.split("\n"): |
|---|
| 71 | n/a | line = line.strip() |
|---|
| 72 | n/a | if line and line[0] != '#': |
|---|
| 73 | n/a | break # Leave it alone |
|---|
| 74 | n/a | else: |
|---|
| 75 | n/a | if symbol != "eval": |
|---|
| 76 | n/a | source = "pass" # Replace it with a 'pass' statement |
|---|
| 77 | n/a | |
|---|
| 78 | n/a | err = err1 = err2 = None |
|---|
| 79 | n/a | code = code1 = code2 = None |
|---|
| 80 | n/a | |
|---|
| 81 | n/a | try: |
|---|
| 82 | n/a | code = compiler(source, filename, symbol) |
|---|
| 83 | n/a | except SyntaxError as err: |
|---|
| 84 | n/a | pass |
|---|
| 85 | n/a | |
|---|
| 86 | n/a | try: |
|---|
| 87 | n/a | code1 = compiler(source + "\n", filename, symbol) |
|---|
| 88 | n/a | except SyntaxError as e: |
|---|
| 89 | n/a | err1 = e |
|---|
| 90 | n/a | |
|---|
| 91 | n/a | try: |
|---|
| 92 | n/a | code2 = compiler(source + "\n\n", filename, symbol) |
|---|
| 93 | n/a | except SyntaxError as e: |
|---|
| 94 | n/a | err2 = e |
|---|
| 95 | n/a | |
|---|
| 96 | n/a | if code: |
|---|
| 97 | n/a | return code |
|---|
| 98 | n/a | if not code1 and repr(err1) == repr(err2): |
|---|
| 99 | n/a | raise err1 |
|---|
| 100 | n/a | |
|---|
| 101 | n/a | def _compile(source, filename, symbol): |
|---|
| 102 | n/a | return compile(source, filename, symbol, PyCF_DONT_IMPLY_DEDENT) |
|---|
| 103 | n/a | |
|---|
| 104 | n/a | def compile_command(source, filename="<input>", symbol="single"): |
|---|
| 105 | n/a | r"""Compile a command and determine whether it is incomplete. |
|---|
| 106 | n/a | |
|---|
| 107 | n/a | Arguments: |
|---|
| 108 | n/a | |
|---|
| 109 | n/a | source -- the source string; may contain \n characters |
|---|
| 110 | n/a | filename -- optional filename from which source was read; default |
|---|
| 111 | n/a | "<input>" |
|---|
| 112 | n/a | symbol -- optional grammar start symbol; "single" (default) or "eval" |
|---|
| 113 | n/a | |
|---|
| 114 | n/a | Return value / exceptions raised: |
|---|
| 115 | n/a | |
|---|
| 116 | n/a | - Return a code object if the command is complete and valid |
|---|
| 117 | n/a | - Return None if the command is incomplete |
|---|
| 118 | n/a | - Raise SyntaxError, ValueError or OverflowError if the command is a |
|---|
| 119 | n/a | syntax error (OverflowError and ValueError can be produced by |
|---|
| 120 | n/a | malformed literals). |
|---|
| 121 | n/a | """ |
|---|
| 122 | n/a | return _maybe_compile(_compile, source, filename, symbol) |
|---|
| 123 | n/a | |
|---|
| 124 | n/a | class Compile: |
|---|
| 125 | n/a | """Instances of this class behave much like the built-in compile |
|---|
| 126 | n/a | function, but if one is used to compile text containing a future |
|---|
| 127 | n/a | statement, it "remembers" and compiles all subsequent program texts |
|---|
| 128 | n/a | with the statement in force.""" |
|---|
| 129 | n/a | def __init__(self): |
|---|
| 130 | n/a | self.flags = PyCF_DONT_IMPLY_DEDENT |
|---|
| 131 | n/a | |
|---|
| 132 | n/a | def __call__(self, source, filename, symbol): |
|---|
| 133 | n/a | codeob = compile(source, filename, symbol, self.flags, 1) |
|---|
| 134 | n/a | for feature in _features: |
|---|
| 135 | n/a | if codeob.co_flags & feature.compiler_flag: |
|---|
| 136 | n/a | self.flags |= feature.compiler_flag |
|---|
| 137 | n/a | return codeob |
|---|
| 138 | n/a | |
|---|
| 139 | n/a | class CommandCompiler: |
|---|
| 140 | n/a | """Instances of this class have __call__ methods identical in |
|---|
| 141 | n/a | signature to compile_command; the difference is that if the |
|---|
| 142 | n/a | instance compiles program text containing a __future__ statement, |
|---|
| 143 | n/a | the instance 'remembers' and compiles all subsequent program texts |
|---|
| 144 | n/a | with the statement in force.""" |
|---|
| 145 | n/a | |
|---|
| 146 | n/a | def __init__(self,): |
|---|
| 147 | n/a | self.compiler = Compile() |
|---|
| 148 | n/a | |
|---|
| 149 | n/a | def __call__(self, source, filename="<input>", symbol="single"): |
|---|
| 150 | n/a | r"""Compile a command and determine whether it is incomplete. |
|---|
| 151 | n/a | |
|---|
| 152 | n/a | Arguments: |
|---|
| 153 | n/a | |
|---|
| 154 | n/a | source -- the source string; may contain \n characters |
|---|
| 155 | n/a | filename -- optional filename from which source was read; |
|---|
| 156 | n/a | default "<input>" |
|---|
| 157 | n/a | symbol -- optional grammar start symbol; "single" (default) or |
|---|
| 158 | n/a | "eval" |
|---|
| 159 | n/a | |
|---|
| 160 | n/a | Return value / exceptions raised: |
|---|
| 161 | n/a | |
|---|
| 162 | n/a | - Return a code object if the command is complete and valid |
|---|
| 163 | n/a | - Return None if the command is incomplete |
|---|
| 164 | n/a | - Raise SyntaxError, ValueError or OverflowError if the command is a |
|---|
| 165 | n/a | syntax error (OverflowError and ValueError can be produced by |
|---|
| 166 | n/a | malformed literals). |
|---|
| 167 | n/a | """ |
|---|
| 168 | n/a | return _maybe_compile(self.compiler, source, filename, symbol) |
|---|