codekingpro/portable-devtools
114k
1r"""Utilities to compile possibly incomplete Python source code.2 3This module provides two interfaces, broadly similar to the builtin4function compile(), which take program text, a filename and a 'mode'5and:6 7- Return code object if the command is complete and valid8- Return None if the command is incomplete9- Raise SyntaxError, ValueError or OverflowError if the command is a10 syntax error (OverflowError and ValueError can be produced by11 malformed literals).12 13The two interfaces are:14 15compile_command(source, filename, symbol):16 17 Compiles a single command in the manner described above.18 19CommandCompiler():20 21 Instances of this class have __call__ methods identical in22 signature to compile_command; the difference is that if the23 instance compiles program text containing a __future__ statement,24 the instance 'remembers' and compiles all subsequent program texts25 with the statement in force.26 27The module also provides another class:28 29Compile():30 31 Instances of this class act like the built-in function compile,32 but with 'memory' in the sense described above.33"""34 35import __future__36import warnings37 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.h44# Caveat emptor: These flags are undocumented on purpose and depending45# on their effect outside the standard library is **unsupported**.46PyCF_DONT_IMPLY_DEDENT = 0x20047PyCF_ONLY_AST = 0x40048PyCF_ALLOW_INCOMPLETE_INPUT = 0x400049 50def _maybe_compile(compiler, source, filename, symbol, flags):51 # Check for source consisting of only blank lines and comments.52 for line in source.split("\n"):53 line = line.strip()54 if line and line[0] != '#':55 break # Leave it alone.56 else:57 if symbol != "eval":58 source = "pass" # Replace it with a 'pass' statement59 60 # Disable compiler warnings when checking for incomplete input.61 with warnings.catch_warnings():62 warnings.simplefilter("ignore", (SyntaxWarning, DeprecationWarning))63 try:64 compiler(source, filename, symbol, flags=flags)65 except SyntaxError: # Let other compile() errors propagate.66 try:67 compiler(source + "\n", filename, symbol, flags=flags)68 return None69 except _IncompleteInputError as e:70 return None71 except SyntaxError as e:72 pass73 # fallthrough74 75 return compiler(source, filename, symbol, incomplete_input=False)76 77def _compile(source, filename, symbol, incomplete_input=True, *, flags=0):78 if incomplete_input:79 flags |= PyCF_ALLOW_INCOMPLETE_INPUT80 flags |= PyCF_DONT_IMPLY_DEDENT81 return compile(source, filename, symbol, flags)82 83def compile_command(source, filename="<input>", symbol="single", flags=0):84 r"""Compile a command and determine whether it is incomplete.85 86 Arguments:87 88 source -- the source string; may contain \n characters89 filename -- optional filename from which source was read; default90 "<input>"91 symbol -- optional grammar start symbol; "single" (default), "exec"92 or "eval"93 94 Return value / exceptions raised:95 96 - Return a code object if the command is complete and valid97 - Return None if the command is incomplete98 - Raise SyntaxError, ValueError or OverflowError if the command is a99 syntax error (OverflowError and ValueError can be produced by100 malformed literals).101 """102 return _maybe_compile(_compile, source, filename, symbol, flags)103 104class Compile:105 """Instances of this class behave much like the built-in compile106 function, but if one is used to compile text containing a future107 statement, it "remembers" and compiles all subsequent program texts108 with the statement in force."""109 def __init__(self):110 self.flags = PyCF_DONT_IMPLY_DEDENT | PyCF_ALLOW_INCOMPLETE_INPUT111 112 def __call__(self, source, filename, symbol, flags=0, **kwargs):113 flags |= self.flags114 if kwargs.get('incomplete_input', True) is False:115 flags &= ~PyCF_DONT_IMPLY_DEDENT116 flags &= ~PyCF_ALLOW_INCOMPLETE_INPUT117 codeob = compile(source, filename, symbol, flags, True)118 if flags & PyCF_ONLY_AST:119 return codeob # this is an ast.Module in this case120 for feature in _features:121 if codeob.co_flags & feature.compiler_flag:122 self.flags |= feature.compiler_flag123 return codeob124 125class CommandCompiler:126 """Instances of this class have __call__ methods identical in127 signature to compile_command; the difference is that if the128 instance compiles program text containing a __future__ statement,129 the instance 'remembers' and compiles all subsequent program texts130 with the statement in force."""131 132 def __init__(self,):133 self.compiler = Compile()134 135 def __call__(self, source, filename="<input>", symbol="single"):136 r"""Compile a command and determine whether it is incomplete.137 138 Arguments:139 140 source -- the source string; may contain \n characters141 filename -- optional filename from which source was read;142 default "<input>"143 symbol -- optional grammar start symbol; "single" (default) or144 "eval"145 146 Return value / exceptions raised:147 148 - Return a code object if the command is complete and valid149 - Return None if the command is incomplete150 - Raise SyntaxError, ValueError or OverflowError if the command is a151 syntax error (OverflowError and ValueError can be produced by152 malformed literals).153 """154 return _maybe_compile(self.compiler, source, filename, symbol, flags=self.compiler.flags)155 