Mercurial > cpython-lru_cache-weakref
annotate Lib/codeop.py @ 13163:a09e8635534d legacy-trunk
Don't try to guess the name of a .def file -- if one is supplied, use it,
otherwise just generate an '/export:' option.
| author | Greg Ward <gward@python.net> |
|---|---|
| date | Thu, 29 Jun 2000 23:09:20 +0000 |
| parents | f8ddd2340398 |
| children | b5345e99596b |
| 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 string |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5 import traceback |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6 |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7 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
|
8 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
|
9 |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
10 Arguments: |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
11 |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
12 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
|
13 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
|
14 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
|
15 |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
16 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
|
17 |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
18 - 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
|
19 - 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
|
20 - 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
|
21 (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
|
22 |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
23 Approach: |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
24 |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
25 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
|
26 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
|
27 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
|
28 |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
29 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
|
30 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
|
31 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
|
32 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
|
33 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
|
34 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
|
35 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
|
36 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
|
37 |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
38 Caveat: |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
39 |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
40 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
|
41 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
|
42 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
|
43 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
|
44 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
|
45 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
|
46 |
|
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 # Check for source consisting of only blank lines and comments |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
50 for line in string.split(source, "\n"): |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
51 line = string.strip(line) |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
52 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
|
53 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
|
54 else: |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
55 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
|
56 |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
57 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
|
58 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
|
59 |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
60 try: |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
61 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
|
62 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
|
63 pass |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
64 |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
65 try: |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
66 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
|
67 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
|
68 pass |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
69 |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
70 try: |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
71 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
|
72 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
|
73 pass |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
74 |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
75 if code: |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
76 return code |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
77 try: |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
78 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
|
79 except AttributeError: |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
80 e1 = err1 |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
81 try: |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
82 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
|
83 except AttributeError: |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
84 e2 = err2 |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
85 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
|
86 raise SyntaxError, err1 |
