ยปCore Development>Code coverage>Lib/codeop.py

Python code coverage for Lib/codeop.py

#countcontent
1n/ar"""Utilities to compile possibly incomplete Python source code.
2n/a
3n/aThis module provides two interfaces, broadly similar to the builtin
4n/afunction compile(), which take program text, a filename and a 'mode'
5n/aand:
6n/a
7n/a- Return code object if the command is complete and valid
8n/a- Return None if the command is incomplete
9n/a- Raise SyntaxError, ValueError or OverflowError if the command is a
10n/a syntax error (OverflowError and ValueError can be produced by
11n/a malformed literals).
12n/a
13n/aApproach:
14n/a
15n/aFirst, check if the source consists entirely of blank lines and
16n/acomments; if so, replace it with 'pass', because the built-in
17n/aparser doesn't always do the right thing for these.
18n/a
19n/aCompile three times: as is, with \n, and with \n\n appended. If it
20n/acompiles as is, it's complete. If it compiles with one \n appended,
21n/awe expect more. If it doesn't compile either way, we compare the
22n/aerror we get when compiling with \n or \n\n appended. If the errors
23n/aare the same, the code is broken. But if the errors are different, we
24n/aexpect more. Not intuitive; not even guaranteed to hold in future
25n/areleases; but this matches the compiler's behavior from Python 1.4
26n/athrough 2.2, at least.
27n/a
28n/aCaveat:
29n/a
30n/aIt is possible (but not likely) that the parser stops parsing with a
31n/asuccessful outcome before reaching the end of the source; in this
32n/acase, trailing symbols may be ignored instead of causing an error.
33n/aFor example, a backslash followed by two newlines may be followed by
34n/aarbitrary garbage. This will be fixed once the API for the parser is
35n/abetter.
36n/a
37n/aThe two interfaces are:
38n/a
39n/acompile_command(source, filename, symbol):
40n/a
41n/a Compiles a single command in the manner described above.
42n/a
43n/aCommandCompiler():
44n/a
45n/a Instances of this class have __call__ methods identical in
46n/a signature to compile_command; the difference is that if the
47n/a instance compiles program text containing a __future__ statement,
48n/a the instance 'remembers' and compiles all subsequent program texts
49n/a with the statement in force.
50n/a
51n/aThe module also provides another class:
52n/a
53n/aCompile():
54n/a
55n/a Instances of this class act like the built-in function compile,
56n/a but with 'memory' in the sense described above.
57n/a"""
58n/a
59n/aimport __future__
60n/a
61n/a_features = [getattr(__future__, fname)
62n/a for fname in __future__.all_feature_names]
63n/a
64n/a__all__ = ["compile_command", "Compile", "CommandCompiler"]
65n/a
66n/aPyCF_DONT_IMPLY_DEDENT = 0x200 # Matches pythonrun.h
67n/a
68n/adef _maybe_compile(compiler, source, filename, symbol):
69n/a # Check for source consisting of only blank lines and comments
70n/a for line in source.split("\n"):
71n/a line = line.strip()
72n/a if line and line[0] != '#':
73n/a break # Leave it alone
74n/a else:
75n/a if symbol != "eval":
76n/a source = "pass" # Replace it with a 'pass' statement
77n/a
78n/a err = err1 = err2 = None
79n/a code = code1 = code2 = None
80n/a
81n/a try:
82n/a code = compiler(source, filename, symbol)
83n/a except SyntaxError as err:
84n/a pass
85n/a
86n/a try:
87n/a code1 = compiler(source + "\n", filename, symbol)
88n/a except SyntaxError as e:
89n/a err1 = e
90n/a
91n/a try:
92n/a code2 = compiler(source + "\n\n", filename, symbol)
93n/a except SyntaxError as e:
94n/a err2 = e
95n/a
96n/a if code:
97n/a return code
98n/a if not code1 and repr(err1) == repr(err2):
99n/a raise err1
100n/a
101n/adef _compile(source, filename, symbol):
102n/a return compile(source, filename, symbol, PyCF_DONT_IMPLY_DEDENT)
103n/a
104n/adef compile_command(source, filename="<input>", symbol="single"):
105n/a r"""Compile a command and determine whether it is incomplete.
106n/a
107n/a Arguments:
108n/a
109n/a source -- the source string; may contain \n characters
110n/a filename -- optional filename from which source was read; default
111n/a "<input>"
112n/a symbol -- optional grammar start symbol; "single" (default) or "eval"
113n/a
114n/a Return value / exceptions raised:
115n/a
116n/a - Return a code object if the command is complete and valid
117n/a - Return None if the command is incomplete
118n/a - Raise SyntaxError, ValueError or OverflowError if the command is a
119n/a syntax error (OverflowError and ValueError can be produced by
120n/a malformed literals).
121n/a """
122n/a return _maybe_compile(_compile, source, filename, symbol)
123n/a
124n/aclass Compile:
125n/a """Instances of this class behave much like the built-in compile
126n/a function, but if one is used to compile text containing a future
127n/a statement, it "remembers" and compiles all subsequent program texts
128n/a with the statement in force."""
129n/a def __init__(self):
130n/a self.flags = PyCF_DONT_IMPLY_DEDENT
131n/a
132n/a def __call__(self, source, filename, symbol):
133n/a codeob = compile(source, filename, symbol, self.flags, 1)
134n/a for feature in _features:
135n/a if codeob.co_flags & feature.compiler_flag:
136n/a self.flags |= feature.compiler_flag
137n/a return codeob
138n/a
139n/aclass CommandCompiler:
140n/a """Instances of this class have __call__ methods identical in
141n/a signature to compile_command; the difference is that if the
142n/a instance compiles program text containing a __future__ statement,
143n/a the instance 'remembers' and compiles all subsequent program texts
144n/a with the statement in force."""
145n/a
146n/a def __init__(self,):
147n/a self.compiler = Compile()
148n/a
149n/a def __call__(self, source, filename="<input>", symbol="single"):
150n/a r"""Compile a command and determine whether it is incomplete.
151n/a
152n/a Arguments:
153n/a
154n/a source -- the source string; may contain \n characters
155n/a filename -- optional filename from which source was read;
156n/a default "<input>"
157n/a symbol -- optional grammar start symbol; "single" (default) or
158n/a "eval"
159n/a
160n/a Return value / exceptions raised:
161n/a
162n/a - Return a code object if the command is complete and valid
163n/a - Return None if the command is incomplete
164n/a - Raise SyntaxError, ValueError or OverflowError if the command is a
165n/a syntax error (OverflowError and ValueError can be produced by
166n/a malformed literals).
167n/a """
168n/a return _maybe_compile(self.compiler, source, filename, symbol)