codeop.py 5.8 KB

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