1 r"""Utilities to compile possibly incomplete Python source code.
2
3 This module provides two interfaces, broadly similar to the builtin
4 function compile(), which take program text, a filename and a 'mode'
5 and:
6
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
10 syntax error (OverflowError and ValueError can be produced by
11 malformed literals).
12
13 The two interfaces are:
14
15 compile_command(source, filename, symbol):
16
17 Compiles a single command in the manner described above.
18
19 CommandCompiler():
20
21 Instances of this class have __call__ methods identical in
22 signature to compile_command; the difference is that if the
23 instance compiles program text containing a __future__ statement,
24 the instance 'remembers' and compiles all subsequent program texts
25 with the statement in force.
26
27 The module also provides another class:
28
29 Compile():
30
31 Instances of this class act like the built-in function compile,
32 but with 'memory' in the sense described above.
33 """
34
35 import __future__
36 import warnings
37
38 _features = [getattr(__future__, fname)
39 for fname in __future__.all_feature_names]
40
41 __all__ = ["compile_command", "Compile", "CommandCompiler"]
42
43 # The following flags match the values from Include/cpython/compile.h
44 # Caveat emptor: These flags are undocumented on purpose and depending
45 # on their effect outside the standard library is **unsupported**.
46 PyCF_DONT_IMPLY_DEDENT = 0x200
47 PyCF_ALLOW_INCOMPLETE_INPUT = 0x4000
48
49 def _maybe_compile(compiler, source, filename, symbol):
50 # Check for source consisting of only blank lines and comments.
51 for line in source.split("\n"):
52 line = line.strip()
53 if line and line[0] != '#':
54 break # Leave it alone.
55 else:
56 if symbol != "eval":
57 source = "pass" # Replace it with a 'pass' statement
58
59 # Disable compiler warnings when checking for incomplete input.
60 with warnings.catch_warnings():
61 warnings.simplefilter("ignore", (SyntaxWarning, DeprecationWarning))
62 try:
63 compiler(source, filename, symbol)
64 except SyntaxError: # Let other compile() errors propagate.
65 try:
66 compiler(source + "\n", filename, symbol)
67 return None
68 except SyntaxError as e:
69 if "incomplete input" in str(e):
70 return None
71 # fallthrough
72
73 return compiler(source, filename, symbol, incomplete_input=False)
74
75 def _is_syntax_error(err1, err2):
76 rep1 = repr(err1)
77 rep2 = repr(err2)
78 if "was never closed" in rep1 and "was never closed" in rep2:
79 return False
80 if rep1 == rep2:
81 return True
82 return False
83
84 def _compile(source, filename, symbol, incomplete_input=True):
85 flags = 0
86 if incomplete_input:
87 flags |= PyCF_ALLOW_INCOMPLETE_INPUT
88 flags |= PyCF_DONT_IMPLY_DEDENT
89 return compile(source, filename, symbol, flags)
90
91 def compile_command(source, filename="<input>", symbol="single"):
92 r"""Compile a command and determine whether it is incomplete.
93
94 Arguments:
95
96 source -- the source string; may contain \n characters
97 filename -- optional filename from which source was read; default
98 "<input>"
99 symbol -- optional grammar start symbol; "single" (default), "exec"
100 or "eval"
101
102 Return value / exceptions raised:
103
104 - Return a code object if the command is complete and valid
105 - Return None if the command is incomplete
106 - Raise SyntaxError, ValueError or OverflowError if the command is a
107 syntax error (OverflowError and ValueError can be produced by
108 malformed literals).
109 """
110 return _maybe_compile(_compile, source, filename, symbol)
111
112 class ESC[4;38;5;81mCompile:
113 """Instances of this class behave much like the built-in compile
114 function, but if one is used to compile text containing a future
115 statement, it "remembers" and compiles all subsequent program texts
116 with the statement in force."""
117 def __init__(self):
118 self.flags = PyCF_DONT_IMPLY_DEDENT | PyCF_ALLOW_INCOMPLETE_INPUT
119
120 def __call__(self, source, filename, symbol, **kwargs):
121 flags = self.flags
122 if kwargs.get('incomplete_input', True) is False:
123 flags &= ~PyCF_DONT_IMPLY_DEDENT
124 flags &= ~PyCF_ALLOW_INCOMPLETE_INPUT
125 codeob = compile(source, filename, symbol, flags, True)
126 for feature in _features:
127 if codeob.co_flags & feature.compiler_flag:
128 self.flags |= feature.compiler_flag
129 return codeob
130
131 class ESC[4;38;5;81mCommandCompiler:
132 """Instances of this class have __call__ methods identical in
133 signature to compile_command; the difference is that if the
134 instance compiles program text containing a __future__ statement,
135 the instance 'remembers' and compiles all subsequent program texts
136 with the statement in force."""
137
138 def __init__(self,):
139 self.compiler = Compile()
140
141 def __call__(self, source, filename="<input>", symbol="single"):
142 r"""Compile a command and determine whether it is incomplete.
143
144 Arguments:
145
146 source -- the source string; may contain \n characters
147 filename -- optional filename from which source was read;
148 default "<input>"
149 symbol -- optional grammar start symbol; "single" (default) or
150 "eval"
151
152 Return value / exceptions raised:
153
154 - Return a code object if the command is complete and valid
155 - Return None if the command is incomplete
156 - Raise SyntaxError, ValueError or OverflowError if the command is a
157 syntax error (OverflowError and ValueError can be produced by
158 malformed literals).
159 """
160 return _maybe_compile(self.compiler, source, filename, symbol)