blob: 7a08610239c3571ee5207eb2f4515ddda3b75e66 [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
Walter Dörwald4df30682003-11-20 13:38:01 +00004function compile(), which take program text, a filename and a 'mode'
Tim Peters6cd6a822001-08-17 22:11:27 +00005and:
Skip Montanaroe99d5ea2001-01-20 19:54:20 +00006
Walter Dörwald4df30682003-11-20 13:38:01 +00007- 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 Peters6cd6a822001-08-17 22:11:27 +000010 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__
Cheryl Sabella052d3fc2020-06-04 19:40:24 -040060import warnings
Tim Peters6cd6a822001-08-17 22:11:27 +000061
62_features = [getattr(__future__, fname)
63 for fname in __future__.all_feature_names]
64
65__all__ = ["compile_command", "Compile", "CommandCompiler"]
66
Guido van Rossum4b499dd32003-02-13 22:07:59 +000067PyCF_DONT_IMPLY_DEDENT = 0x200 # Matches pythonrun.h
68
Tim Peters6cd6a822001-08-17 22:11:27 +000069def _maybe_compile(compiler, source, filename, symbol):
Guido van Rossumc41c1a91998-10-22 21:56:15 +000070 # Check for source consisting of only blank lines and comments
Eric S. Raymond6b71e742001-02-09 08:56:30 +000071 for line in source.split("\n"):
72 line = line.strip()
Guido van Rossumc41c1a91998-10-22 21:56:15 +000073 if line and line[0] != '#':
74 break # Leave it alone
75 else:
Guido van Rossum993bc3a2003-05-16 01:24:30 +000076 if symbol != "eval":
77 source = "pass" # Replace it with a 'pass' statement
Guido van Rossumc41c1a91998-10-22 21:56:15 +000078
79 err = err1 = err2 = None
80 code = code1 = code2 = None
81
82 try:
Tim Peters6cd6a822001-08-17 22:11:27 +000083 code = compiler(source, filename, symbol)
Pablo Galindo293dd232019-11-19 21:34:03 +000084 except SyntaxError:
Guido van Rossumc41c1a91998-10-22 21:56:15 +000085 pass
86
Victor Stinner369a1cb2020-08-12 14:53:28 +020087 # Catch syntax warnings after the first compile
Terry Jan Reedyc818b152020-08-13 13:18:49 -040088 # to emit warnings (SyntaxWarning, DeprecationWarning) at most once.
Cheryl Sabella052d3fc2020-06-04 19:40:24 -040089 with warnings.catch_warnings():
Terry Jan Reedyc818b152020-08-13 13:18:49 -040090 warnings.simplefilter("error")
Victor Stinner369a1cb2020-08-12 14:53:28 +020091
Cheryl Sabella052d3fc2020-06-04 19:40:24 -040092 try:
93 code1 = compiler(source + "\n", filename, symbol)
94 except SyntaxError as e:
95 err1 = e
Guido van Rossumc41c1a91998-10-22 21:56:15 +000096
Cheryl Sabella052d3fc2020-06-04 19:40:24 -040097 try:
98 code2 = compiler(source + "\n\n", filename, symbol)
99 except SyntaxError as e:
100 err2 = e
Guido van Rossumc41c1a91998-10-22 21:56:15 +0000101
Mario Corcherob64334c2019-12-06 14:27:38 +0000102 try:
103 if code:
104 return code
Pablo Galindodbb22812021-02-09 20:07:38 +0000105 if not code1 and _is_syntax_error(err1, err2):
Mario Corcherob64334c2019-12-06 14:27:38 +0000106 raise err1
107 finally:
108 err1 = err2 = None
Tim Peters6cd6a822001-08-17 22:11:27 +0000109
Pablo Galindodbb22812021-02-09 20:07:38 +0000110def _is_syntax_error(err1, err2):
111 rep1 = repr(err1)
112 rep2 = repr(err2)
113 if "was never closed" in rep1 and "was never closed" in rep2:
114 return False
115 if rep1 == rep2:
116 return True
117 return False
118
Guido van Rossum4b499dd32003-02-13 22:07:59 +0000119def _compile(source, filename, symbol):
120 return compile(source, filename, symbol, PyCF_DONT_IMPLY_DEDENT)
121
Tim Peters6cd6a822001-08-17 22:11:27 +0000122def compile_command(source, filename="<input>", symbol="single"):
123 r"""Compile a command and determine whether it is incomplete.
124
125 Arguments:
126
127 source -- the source string; may contain \n characters
128 filename -- optional filename from which source was read; default
129 "<input>"
Joannah Nanjekye7ba1f752020-05-14 21:59:46 -0300130 symbol -- optional grammar start symbol; "single" (default), "exec"
131 or "eval"
Tim Peters6cd6a822001-08-17 22:11:27 +0000132
133 Return value / exceptions raised:
134
135 - Return a code object if the command is complete and valid
136 - Return None if the command is incomplete
137 - Raise SyntaxError, ValueError or OverflowError if the command is a
138 syntax error (OverflowError and ValueError can be produced by
139 malformed literals).
140 """
Guido van Rossum4b499dd32003-02-13 22:07:59 +0000141 return _maybe_compile(_compile, source, filename, symbol)
Tim Peters6cd6a822001-08-17 22:11:27 +0000142
143class Compile:
144 """Instances of this class behave much like the built-in compile
145 function, but if one is used to compile text containing a future
146 statement, it "remembers" and compiles all subsequent program texts
147 with the statement in force."""
148 def __init__(self):
Guido van Rossum4b499dd32003-02-13 22:07:59 +0000149 self.flags = PyCF_DONT_IMPLY_DEDENT
Tim Peters6cd6a822001-08-17 22:11:27 +0000150
151 def __call__(self, source, filename, symbol):
Serhiy Storchaka1f21eaa2019-09-01 12:16:51 +0300152 codeob = compile(source, filename, symbol, self.flags, True)
Tim Peters6cd6a822001-08-17 22:11:27 +0000153 for feature in _features:
154 if codeob.co_flags & feature.compiler_flag:
155 self.flags |= feature.compiler_flag
156 return codeob
157
158class CommandCompiler:
159 """Instances of this class have __call__ methods identical in
160 signature to compile_command; the difference is that if the
161 instance compiles program text containing a __future__ statement,
162 the instance 'remembers' and compiles all subsequent program texts
163 with the statement in force."""
164
165 def __init__(self,):
166 self.compiler = Compile()
167
168 def __call__(self, source, filename="<input>", symbol="single"):
169 r"""Compile a command and determine whether it is incomplete.
170
171 Arguments:
172
173 source -- the source string; may contain \n characters
174 filename -- optional filename from which source was read;
175 default "<input>"
176 symbol -- optional grammar start symbol; "single" (default) or
177 "eval"
178
179 Return value / exceptions raised:
180
181 - Return a code object if the command is complete and valid
182 - Return None if the command is incomplete
183 - Raise SyntaxError, ValueError or OverflowError if the command is a
184 syntax error (OverflowError and ValueError can be produced by
185 malformed literals).
186 """
187 return _maybe_compile(self.compiler, source, filename, symbol)