blob: cc9d5b26f174f978dc8e2885623e848c3c9fc271 [file] [log] [blame]
Tim Peters6cd6a822001-08-17 22:11:27 +00001r"""Utilities to compile possibly incomplete Python source code.
Guido van Rossumc41c1a91998-10-22 21:56:15 +00002
Tim Peters6cd6a822001-08-17 22:11:27 +00003This module provides two interfaces, broadly similar to the builtin
4function compile(), that take progam text, a filename and a 'mode'
5and:
Skip Montanaroe99d5ea2001-01-20 19:54:20 +00006
Tim Peters6cd6a822001-08-17 22:11:27 +00007- Return a 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
10 syntax error (OverflowError and ValueError can be produced by
11 malformed literals).
Guido van Rossumc41c1a91998-10-22 21:56:15 +000012
Tim Peters6cd6a822001-08-17 22:11:27 +000013Approach:
Guido van Rossumc41c1a91998-10-22 21:56:15 +000014
Tim Peters6cd6a822001-08-17 22:11:27 +000015First, check if the source consists entirely of blank lines and
16comments; if so, replace it with 'pass', because the built-in
17parser doesn't always do the right thing for these.
Guido van Rossumc41c1a91998-10-22 21:56:15 +000018
Tim Peters6cd6a822001-08-17 22:11:27 +000019Compile three times: as is, with \n, and with \n\n appended. If it
20compiles as is, it's complete. If it compiles with one \n appended,
21we expect more. If it doesn't compile either way, we compare the
22error we get when compiling with \n or \n\n appended. If the errors
23are the same, the code is broken. But if the errors are different, we
24expect more. Not intuitive; not even guaranteed to hold in future
25releases; but this matches the compiler's behavior from Python 1.4
26through 2.2, at least.
Guido van Rossumc41c1a91998-10-22 21:56:15 +000027
Tim Peters6cd6a822001-08-17 22:11:27 +000028Caveat:
Guido van Rossumc41c1a91998-10-22 21:56:15 +000029
Tim Peters6cd6a822001-08-17 22:11:27 +000030It is possible (but not likely) that the parser stops parsing with a
31successful outcome before reaching the end of the source; in this
32case, trailing symbols may be ignored instead of causing an error.
33For example, a backslash followed by two newlines may be followed by
34arbitrary garbage. This will be fixed once the API for the parser is
35better.
Guido van Rossumc41c1a91998-10-22 21:56:15 +000036
Tim Peters6cd6a822001-08-17 22:11:27 +000037The two interfaces are:
Guido van Rossumc41c1a91998-10-22 21:56:15 +000038
Tim Peters6cd6a822001-08-17 22:11:27 +000039compile_command(source, filename, symbol):
Guido van Rossumc41c1a91998-10-22 21:56:15 +000040
Tim Peters6cd6a822001-08-17 22:11:27 +000041 Compiles a single command in the manner described above.
Guido van Rossumc41c1a91998-10-22 21:56:15 +000042
Tim Peters6cd6a822001-08-17 22:11:27 +000043CommandCompiler():
Guido van Rossumc41c1a91998-10-22 21:56:15 +000044
Tim Peters6cd6a822001-08-17 22:11:27 +000045 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 Rossumc41c1a91998-10-22 21:56:15 +000050
Tim Peters6cd6a822001-08-17 22:11:27 +000051The module also provides another class:
52
53Compile():
54
55 Instances of this class act like the built-in function compile,
56 but with 'memory' in the sense described above.
57"""
58
59import __future__
60
61_features = [getattr(__future__, fname)
62 for fname in __future__.all_feature_names]
63
64__all__ = ["compile_command", "Compile", "CommandCompiler"]
65
Guido van Rossum4b499dd32003-02-13 22:07:59 +000066PyCF_DONT_IMPLY_DEDENT = 0x200 # Matches pythonrun.h
67
Tim Peters6cd6a822001-08-17 22:11:27 +000068def _maybe_compile(compiler, source, filename, symbol):
Guido van Rossumc41c1a91998-10-22 21:56:15 +000069 # Check for source consisting of only blank lines and comments
Eric S. Raymond6b71e742001-02-09 08:56:30 +000070 for line in source.split("\n"):
71 line = line.strip()
Guido van Rossumc41c1a91998-10-22 21:56:15 +000072 if line and line[0] != '#':
73 break # Leave it alone
74 else:
75 source = "pass" # Replace it with a 'pass' statement
76
77 err = err1 = err2 = None
78 code = code1 = code2 = None
79
80 try:
Tim Peters6cd6a822001-08-17 22:11:27 +000081 code = compiler(source, filename, symbol)
Guido van Rossumc41c1a91998-10-22 21:56:15 +000082 except SyntaxError, err:
83 pass
84
85 try:
Tim Peters6cd6a822001-08-17 22:11:27 +000086 code1 = compiler(source + "\n", filename, symbol)
Guido van Rossumc41c1a91998-10-22 21:56:15 +000087 except SyntaxError, err1:
88 pass
89
90 try:
Tim Peters6cd6a822001-08-17 22:11:27 +000091 code2 = compiler(source + "\n\n", filename, symbol)
Guido van Rossumc41c1a91998-10-22 21:56:15 +000092 except SyntaxError, err2:
93 pass
94
95 if code:
96 return code
97 try:
98 e1 = err1.__dict__
99 except AttributeError:
100 e1 = err1
101 try:
102 e2 = err2.__dict__
103 except AttributeError:
104 e2 = err2
105 if not code1 and e1 == e2:
106 raise SyntaxError, err1
Tim Peters6cd6a822001-08-17 22:11:27 +0000107
Guido van Rossum4b499dd32003-02-13 22:07:59 +0000108def _compile(source, filename, symbol):
109 return compile(source, filename, symbol, PyCF_DONT_IMPLY_DEDENT)
110
Tim Peters6cd6a822001-08-17 22:11:27 +0000111def compile_command(source, filename="<input>", symbol="single"):
112 r"""Compile a command and determine whether it is incomplete.
113
114 Arguments:
115
116 source -- the source string; may contain \n characters
117 filename -- optional filename from which source was read; default
118 "<input>"
119 symbol -- optional grammar start symbol; "single" (default) or "eval"
120
121 Return value / exceptions raised:
122
123 - Return a code object if the command is complete and valid
124 - Return None if the command is incomplete
125 - Raise SyntaxError, ValueError or OverflowError if the command is a
126 syntax error (OverflowError and ValueError can be produced by
127 malformed literals).
128 """
Guido van Rossum4b499dd32003-02-13 22:07:59 +0000129 return _maybe_compile(_compile, source, filename, symbol)
Tim Peters6cd6a822001-08-17 22:11:27 +0000130
131class Compile:
132 """Instances of this class behave much like the built-in compile
133 function, but if one is used to compile text containing a future
134 statement, it "remembers" and compiles all subsequent program texts
135 with the statement in force."""
136 def __init__(self):
Guido van Rossum4b499dd32003-02-13 22:07:59 +0000137 self.flags = PyCF_DONT_IMPLY_DEDENT
Tim Peters6cd6a822001-08-17 22:11:27 +0000138
139 def __call__(self, source, filename, symbol):
140 codeob = compile(source, filename, symbol, self.flags, 1)
141 for feature in _features:
142 if codeob.co_flags & feature.compiler_flag:
143 self.flags |= feature.compiler_flag
144 return codeob
145
146class CommandCompiler:
147 """Instances of this class have __call__ methods identical in
148 signature to compile_command; the difference is that if the
149 instance compiles program text containing a __future__ statement,
150 the instance 'remembers' and compiles all subsequent program texts
151 with the statement in force."""
152
153 def __init__(self,):
154 self.compiler = Compile()
155
156 def __call__(self, source, filename="<input>", symbol="single"):
157 r"""Compile a command and determine whether it is incomplete.
158
159 Arguments:
160
161 source -- the source string; may contain \n characters
162 filename -- optional filename from which source was read;
163 default "<input>"
164 symbol -- optional grammar start symbol; "single" (default) or
165 "eval"
166
167 Return value / exceptions raised:
168
169 - Return a code object if the command is complete and valid
170 - Return None if the command is incomplete
171 - Raise SyntaxError, ValueError or OverflowError if the command is a
172 syntax error (OverflowError and ValueError can be produced by
173 malformed literals).
174 """
175 return _maybe_compile(self.compiler, source, filename, symbol)