Mercurial > cpython-lru_cache-weakref
annotate Lib/codeop.py @ 17581:c1937faabb23 2.1
Added support for optional explanation parameter to the \versionadded
macro.
Refactored do_cmd_versionadded() and do_cmd_versionchanged() to do most
of the work in a helper function, with the do_cmd_*() wrappers just supplying
a portion of the replacement text.
| author | Fred Drake <fdrake@acm.org> |
|---|---|
| date | Wed, 18 Apr 2001 17:29:14 +0000 |
| parents | a5710578c2e0 |
| children | d5acff178ad5 |
| 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 |
|
16070
b5345e99596b
added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents:
9440
diff
changeset
|
3 __all__ = ["compile_command"] |
|
b5345e99596b
added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents:
9440
diff
changeset
|
4 |
|
9440
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5 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
|
6 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
|
7 |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8 Arguments: |
|
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 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
|
11 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
|
12 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
|
13 |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
14 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
|
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 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
|
17 - 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
|
18 - 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
|
19 (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
|
20 |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
21 Approach: |
|
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 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
|
24 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
|
25 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
|
26 |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
27 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
|
28 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
|
29 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
|
30 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
|
31 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
|
32 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
|
33 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
|
34 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
|
35 |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
36 Caveat: |
|
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 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
|
39 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
|
40 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
|
41 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
|
42 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
|
43 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
|
44 |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
45 """ |
|
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 # 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
|
48 for line in source.split("\n"): |
|
69f162dcd8b4
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16070
diff
changeset
|
49 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
|
50 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
|
51 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
|
52 else: |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
53 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
|
54 |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
55 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
|
56 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
|
57 |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
58 try: |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
59 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
|
60 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
|
61 pass |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
62 |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
63 try: |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
64 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
|
65 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
|
66 pass |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
67 |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
68 try: |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
69 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
|
70 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
|
71 pass |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
72 |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
73 if code: |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
74 return code |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
75 try: |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
76 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
|
77 except AttributeError: |
|
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 |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
79 try: |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
80 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
|
81 except AttributeError: |
|
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 |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
83 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
|
84 raise SyntaxError, err1 |
