annotate Lib/codeop.py @ 17322:bd110eadc19a legacy-trunk

One-liner critical fix from Jim Fulton: Eric's string-method crusade got the order backwards in a line (for .find()).
author Tim Peters <tim.peters@gmail.com>
date Thu, 05 Apr 2001 22:26:23 +0000
parents 69f162dcd8b4
children a5710578c2e0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9440
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 """Utility to compile possibly incomplete Python source code."""
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 import sys
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 import traceback
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
5
16070
b5345e99596b added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents: 9440
diff changeset
6 __all__ = ["compile_command"]
b5345e99596b added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents: 9440
diff changeset
7
9440
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 def compile_command(source, filename="<input>", symbol="single"):
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 r"""Compile a command and determine whether it is incomplete.
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
10
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 Arguments:
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
12
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 source -- the source string; may contain \n characters
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 filename -- optional filename from which source was read; default "<input>"
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 symbol -- optional grammar start symbol; "single" (default) or "eval"
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
16
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 Return value / exceptions raised:
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
18
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 - Return a code object if the command is complete and valid
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 - Return None if the command is incomplete
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 - Raise SyntaxError or OverflowError if the command is a syntax error
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 (OverflowError if the error is in a numeric constant)
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
23
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 Approach:
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
25
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 First, check if the source consists entirely of blank lines and
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 comments; if so, replace it with 'pass', because the built-in
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 parser doesn't always do the right thing for these.
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
29
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 Compile three times: as is, with \n, and with \n\n appended. If
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 it compiles as is, it's complete. If it compiles with one \n
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 appended, we expect more. If it doesn't compile either way, we
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 compare the error we get when compiling with \n or \n\n appended.
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 If the errors are the same, the code is broken. But if the errors
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 are different, we expect more. Not intuitive; not even guaranteed
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 to hold in future releases; but this matches the compiler's
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 behavior from Python 1.4 through 1.5.2, at least.
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
38
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 Caveat:
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
40
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 It is possible (but not likely) that the parser stops parsing
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 with a successful outcome before reaching the end of the source;
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 in this case, trailing symbols may be ignored instead of causing an
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 error. For example, a backslash followed by two newlines may be
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 followed by arbitrary garbage. This will be fixed once the API
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 for the parser is better.
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
47
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 """
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
49
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 # Check for source consisting of only blank lines and comments
16495
69f162dcd8b4 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16070
diff changeset
51 for line in source.split("\n"):
69f162dcd8b4 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16070
diff changeset
52 line = line.strip()
9440
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 if line and line[0] != '#':
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 break # Leave it alone
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 else:
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 source = "pass" # Replace it with a 'pass' statement
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
57
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 err = err1 = err2 = None
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 code = code1 = code2 = None
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
60
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 try:
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 code = compile(source, filename, symbol)
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 except SyntaxError, err:
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 pass
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
65
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 try:
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 code1 = compile(source + "\n", filename, symbol)
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 except SyntaxError, err1:
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 pass
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
70
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 try:
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 code2 = compile(source + "\n\n", filename, symbol)
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 except SyntaxError, err2:
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 pass
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
75
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 if code:
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 return code
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 try:
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 e1 = err1.__dict__
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 except AttributeError:
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 e1 = err1
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 try:
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 e2 = err2.__dict__
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 except AttributeError:
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 e2 = err2
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 if not code1 and e1 == e2:
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 raise SyntaxError, err1