Mercurial > cpython-lru_cache-weakref
annotate Lib/codeop.py @ 49955:878d40468c4e legacy-trunk
Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
| author | Vinay Sajip <vinay_sajip@yahoo.co.uk> |
|---|---|
| date | Wed, 03 Dec 2008 23:22:58 +0000 |
| parents | 4c0823f0a0a1 |
| children | 6b18a095c2e7 |
| rev | line source |
|---|---|
|
19355
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
1 r"""Utilities to compile possibly incomplete Python source code. |
|
9440
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2 |
|
19355
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
3 This module provides two interfaces, broadly similar to the builtin |
| 30984 | 4 function compile(), which take program text, a filename and a 'mode' |
|
19355
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
5 and: |
|
16070
b5345e99596b
added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents:
9440
diff
changeset
|
6 |
| 30984 | 7 - 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 | |
|
19355
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
10 syntax error (OverflowError and ValueError can be produced by |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
11 malformed literals). |
|
9440
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
12 |
|
19355
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
13 Approach: |
|
9440
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
14 |
|
19355
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
15 First, check if the source consists entirely of blank lines and |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
16 comments; if so, replace it with 'pass', because the built-in |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
17 parser doesn't always do the right thing for these. |
|
9440
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
18 |
|
19355
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
19 Compile three times: as is, with \n, and with \n\n appended. If it |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
20 compiles as is, it's complete. If it compiles with one \n appended, |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
21 we expect more. If it doesn't compile either way, we compare the |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
22 error we get when compiling with \n or \n\n appended. If the errors |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
23 are the same, the code is broken. But if the errors are different, we |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
24 expect more. Not intuitive; not even guaranteed to hold in future |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
25 releases; but this matches the compiler's behavior from Python 1.4 |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
26 through 2.2, at least. |
|
9440
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
27 |
|
19355
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
28 Caveat: |
|
9440
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
29 |
|
19355
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
30 It is possible (but not likely) that the parser stops parsing with a |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
31 successful outcome before reaching the end of the source; in this |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
32 case, trailing symbols may be ignored instead of causing an error. |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
33 For example, a backslash followed by two newlines may be followed by |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
34 arbitrary garbage. This will be fixed once the API for the parser is |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
35 better. |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
36 |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
37 The two interfaces are: |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
38 |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
39 compile_command(source, filename, symbol): |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
40 |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
41 Compiles a single command in the manner described above. |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
42 |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
43 CommandCompiler(): |
|
9440
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
44 |
|
19355
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
45 Instances of this class have __call__ methods identical in |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
46 signature to compile_command; the difference is that if the |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
47 instance compiles program text containing a __future__ statement, |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
48 the instance 'remembers' and compiles all subsequent program texts |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
49 with the statement in force. |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
50 |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
51 The module also provides another class: |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
52 |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
53 Compile(): |
|
9440
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
54 |
|
19355
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
55 Instances of this class act like the built-in function compile, |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
56 but with 'memory' in the sense described above. |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
57 """ |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
58 |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
59 import __future__ |
|
9440
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
60 |
|
19355
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
61 _features = [getattr(__future__, fname) |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
62 for fname in __future__.all_feature_names] |
|
9440
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
63 |
|
19355
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
64 __all__ = ["compile_command", "Compile", "CommandCompiler"] |
|
9440
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
65 |
|
27878
be7683a6f521
- Finally fixed the bug in compile() and exec where a string ending
Guido van Rossum <guido@python.org>
parents:
19355
diff
changeset
|
66 PyCF_DONT_IMPLY_DEDENT = 0x200 # Matches pythonrun.h |
|
be7683a6f521
- Finally fixed the bug in compile() and exec where a string ending
Guido van Rossum <guido@python.org>
parents:
19355
diff
changeset
|
67 |
|
19355
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
68 def _maybe_compile(compiler, source, filename, symbol): |
|
9440
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
69 # 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
|
70 for line in source.split("\n"): |
|
69f162dcd8b4
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16070
diff
changeset
|
71 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
|
72 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
|
73 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
|
74 else: |
|
29123
cff6b8f422d1
Don't replace an empty line with "pass" when symbol == "eval", where
Guido van Rossum <guido@python.org>
parents:
27878
diff
changeset
|
75 if symbol != "eval": |
|
cff6b8f422d1
Don't replace an empty line with "pass" when symbol == "eval", where
Guido van Rossum <guido@python.org>
parents:
27878
diff
changeset
|
76 source = "pass" # Replace it with a 'pass' statement |
|
9440
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
77 |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
78 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
|
79 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
|
80 |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
81 try: |
|
19355
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
82 code = compiler(source, filename, symbol) |
|
9440
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
83 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
|
84 pass |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
85 |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
86 try: |
|
19355
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
87 code1 = compiler(source + "\n", filename, symbol) |
|
9440
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
88 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
|
89 pass |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
90 |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
91 try: |
|
19355
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
92 code2 = compiler(source + "\n\n", filename, symbol) |
|
9440
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
93 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
|
94 pass |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
95 |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
96 if code: |
|
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
97 return code |
|
38431
4c0823f0a0a1
Conversion of exceptions over from faked-up classes to new-style C types.
Richard Jones <richard@commonground.com.au>
parents:
30984
diff
changeset
|
98 if not code1 and repr(err1) == repr(err2): |
|
9440
f8ddd2340398
Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
99 raise SyntaxError, err1 |
|
19355
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
100 |
|
27878
be7683a6f521
- Finally fixed the bug in compile() and exec where a string ending
Guido van Rossum <guido@python.org>
parents:
19355
diff
changeset
|
101 def _compile(source, filename, symbol): |
|
be7683a6f521
- Finally fixed the bug in compile() and exec where a string ending
Guido van Rossum <guido@python.org>
parents:
19355
diff
changeset
|
102 return compile(source, filename, symbol, PyCF_DONT_IMPLY_DEDENT) |
|
be7683a6f521
- Finally fixed the bug in compile() and exec where a string ending
Guido van Rossum <guido@python.org>
parents:
19355
diff
changeset
|
103 |
|
19355
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
104 def compile_command(source, filename="<input>", symbol="single"): |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
105 r"""Compile a command and determine whether it is incomplete. |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
106 |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
107 Arguments: |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
108 |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
109 source -- the source string; may contain \n characters |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
110 filename -- optional filename from which source was read; default |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
111 "<input>" |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
112 symbol -- optional grammar start symbol; "single" (default) or "eval" |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
113 |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
114 Return value / exceptions raised: |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
115 |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
116 - Return a code object if the command is complete and valid |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
117 - Return None if the command is incomplete |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
118 - Raise SyntaxError, ValueError or OverflowError if the command is a |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
119 syntax error (OverflowError and ValueError can be produced by |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
120 malformed literals). |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
121 """ |
|
27878
be7683a6f521
- Finally fixed the bug in compile() and exec where a string ending
Guido van Rossum <guido@python.org>
parents:
19355
diff
changeset
|
122 return _maybe_compile(_compile, source, filename, symbol) |
|
19355
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
123 |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
124 class Compile: |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
125 """Instances of this class behave much like the built-in compile |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
126 function, but if one is used to compile text containing a future |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
127 statement, it "remembers" and compiles all subsequent program texts |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
128 with the statement in force.""" |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
129 def __init__(self): |
|
27878
be7683a6f521
- Finally fixed the bug in compile() and exec where a string ending
Guido van Rossum <guido@python.org>
parents:
19355
diff
changeset
|
130 self.flags = PyCF_DONT_IMPLY_DEDENT |
|
19355
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
131 |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
132 def __call__(self, source, filename, symbol): |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
133 codeob = compile(source, filename, symbol, self.flags, 1) |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
134 for feature in _features: |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
135 if codeob.co_flags & feature.compiler_flag: |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
136 self.flags |= feature.compiler_flag |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
137 return codeob |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
138 |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
139 class CommandCompiler: |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
140 """Instances of this class have __call__ methods identical in |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
141 signature to compile_command; the difference is that if the |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
142 instance compiles program text containing a __future__ statement, |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
143 the instance 'remembers' and compiles all subsequent program texts |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
144 with the statement in force.""" |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
145 |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
146 def __init__(self,): |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
147 self.compiler = Compile() |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
148 |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
149 def __call__(self, source, filename="<input>", symbol="single"): |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
150 r"""Compile a command and determine whether it is incomplete. |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
151 |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
152 Arguments: |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
153 |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
154 source -- the source string; may contain \n characters |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
155 filename -- optional filename from which source was read; |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
156 default "<input>" |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
157 symbol -- optional grammar start symbol; "single" (default) or |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
158 "eval" |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
159 |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
160 Return value / exceptions raised: |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
161 |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
162 - Return a code object if the command is complete and valid |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
163 - Return None if the command is incomplete |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
164 - Raise SyntaxError, ValueError or OverflowError if the command is a |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
165 syntax error (OverflowError and ValueError can be produced by |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
166 malformed literals). |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
167 """ |
|
d5acff178ad5
A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents:
17339
diff
changeset
|
168 return _maybe_compile(self.compiler, source, filename, symbol) |
