annotate Lib/codeop.py @ 25838:8540887bcec6 2.2

Backport of 2.183: Fix memory leak in add_subclass() found by NealN with valgrind.
author Guido van Rossum <guido@python.org>
date Fri, 18 Oct 2002 13:42:21 +0000
parents d5acff178ad5
children e9f9a196fdef be7683a6f521
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
19355
d5acff178ad5 A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents: 17339
diff changeset
66 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
67 # 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
68 for line in source.split("\n"):
69f162dcd8b4 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16070
diff changeset
69 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
70 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
71 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
72 else:
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 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
74
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 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
76 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
77
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 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
79 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
80 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
81 pass
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
82
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 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
84 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
85 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
86 pass
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
87
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 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
89 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
90 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
91 pass
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
92
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 if code:
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 return code
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 try:
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 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
97 except AttributeError:
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
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 try:
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 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
101 except AttributeError:
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
f8ddd2340398 Moved compile_command() here from code.py, so JPython can provide its
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 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
104 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
105
d5acff178ad5 A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents: 17339
diff changeset
106 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
107 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
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 Arguments:
d5acff178ad5 A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents: 17339
diff changeset
110
d5acff178ad5 A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents: 17339
diff changeset
111 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
112 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
113 "<input>"
d5acff178ad5 A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents: 17339
diff changeset
114 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
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 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
117
d5acff178ad5 A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents: 17339
diff changeset
118 - 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
119 - 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
120 - 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
121 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
122 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
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 return _maybe_compile(compile, 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
125
d5acff178ad5 A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents: 17339
diff changeset
126 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
127 """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
128 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
129 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
130 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
131 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
132 self.flags = 0
d5acff178ad5 A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents: 17339
diff changeset
133
d5acff178ad5 A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents: 17339
diff changeset
134 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
135 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
136 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
137 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
138 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
139 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
140
d5acff178ad5 A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents: 17339
diff changeset
141 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
142 """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
143 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
144 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
145 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
146 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
147
d5acff178ad5 A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents: 17339
diff changeset
148 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
149 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
150
d5acff178ad5 A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents: 17339
diff changeset
151 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
152 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
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 Arguments:
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 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
157 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
158 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
159 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
160 "eval"
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 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
163
d5acff178ad5 A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents: 17339
diff changeset
164 - 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
165 - 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
166 - 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
167 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
168 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
169 """
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 _maybe_compile(self.compiler, source, filename, symbol)