annotate Lib/codeop.py @ 28646:193b8aec00ce legacy-trunk

Changes from Jonathan Riehl to allow his pgen extension (PEP 269) to work. This includes some more code that used to be part of pgen in the main parser; I'm okay with that. I'll see if the Windows build needs work next.
author Guido van Rossum <guido@python.org>
date Thu, 17 Apr 2003 14:55:42 +0000
parents be7683a6f521
children cff6b8f422d1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
d5acff178ad5 A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents: 17339
diff changeset
4 function compile(), that take progam text, a filename and a 'mode'
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
19355
d5acff178ad5 A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents: 17339
diff changeset
7 - 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
8 - 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
9 - 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
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:
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 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
76
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 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
78 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
79
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 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
81 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
82 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
83 pass
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
84
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 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
86 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
87 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
88 pass
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
89
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 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
91 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
92 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
93 pass
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
94
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 if code:
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 return code
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 try:
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 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
99 except AttributeError:
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 e1 = err1
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 try:
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 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
103 except AttributeError:
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 e2 = err2
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 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
106 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
107
27878
be7683a6f521 - Finally fixed the bug in compile() and exec where a string ending
Guido van Rossum <guido@python.org>
parents: 19355
diff changeset
108 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
109 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
110
19355
d5acff178ad5 A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents: 17339
diff changeset
111 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
112 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
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 Arguments:
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 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
117 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
118 "<input>"
d5acff178ad5 A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents: 17339
diff changeset
119 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
120
d5acff178ad5 A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents: 17339
diff changeset
121 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
122
d5acff178ad5 A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents: 17339
diff changeset
123 - 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
124 - 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
125 - 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
126 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
127 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
128 """
27878
be7683a6f521 - Finally fixed the bug in compile() and exec where a string ending
Guido van Rossum <guido@python.org>
parents: 19355
diff changeset
129 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
130
d5acff178ad5 A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents: 17339
diff changeset
131 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
132 """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
133 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
134 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
135 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
136 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
137 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
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 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
140 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
141 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
142 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
143 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
144 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
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 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
147 """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
148 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
149 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
150 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
151 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
152
d5acff178ad5 A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents: 17339
diff changeset
153 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
154 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
155
d5acff178ad5 A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents: 17339
diff changeset
156 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
157 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
158
d5acff178ad5 A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents: 17339
diff changeset
159 Arguments:
d5acff178ad5 A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents: 17339
diff changeset
160
d5acff178ad5 A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents: 17339
diff changeset
161 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
162 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
163 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
164 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
165 "eval"
d5acff178ad5 A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents: 17339
diff changeset
166
d5acff178ad5 A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents: 17339
diff changeset
167 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
168
d5acff178ad5 A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents: 17339
diff changeset
169 - 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
170 - 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
171 - 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
172 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
173 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
174 """
d5acff178ad5 A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents: 17339
diff changeset
175 return _maybe_compile(self.compiler, source, filename, symbol)