codeop.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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)
  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):
  66. return compile(source, filename, symbol, PyCF_DONT_IMPLY_DEDENT | PyCF_ALLOW_INCOMPLETE_INPUT)
  67. def compile_command(source, filename="<input>", symbol="single"):
  68. r"""Compile a command and determine whether it is incomplete.
  69. Arguments:
  70. source -- the source string; may contain \n characters
  71. filename -- optional filename from which source was read; default
  72. "<input>"
  73. symbol -- optional grammar start symbol; "single" (default), "exec"
  74. or "eval"
  75. Return value / exceptions raised:
  76. - Return a code object if the command is complete and valid
  77. - Return None if the command is incomplete
  78. - Raise SyntaxError, ValueError or OverflowError if the command is a
  79. syntax error (OverflowError and ValueError can be produced by
  80. malformed literals).
  81. """
  82. return _maybe_compile(_compile, source, filename, symbol)
  83. class Compile:
  84. """Instances of this class behave much like the built-in compile
  85. function, but if one is used to compile text containing a future
  86. statement, it "remembers" and compiles all subsequent program texts
  87. with the statement in force."""
  88. def __init__(self):
  89. self.flags = PyCF_DONT_IMPLY_DEDENT | PyCF_ALLOW_INCOMPLETE_INPUT
  90. def __call__(self, source, filename, symbol):
  91. codeob = compile(source, filename, symbol, self.flags, True)
  92. for feature in _features:
  93. if codeob.co_flags & feature.compiler_flag:
  94. self.flags |= feature.compiler_flag
  95. return codeob
  96. class CommandCompiler:
  97. """Instances of this class have __call__ methods identical in
  98. signature to compile_command; the difference is that if the
  99. instance compiles program text containing a __future__ statement,
  100. the instance 'remembers' and compiles all subsequent program texts
  101. with the statement in force."""
  102. def __init__(self,):
  103. self.compiler = Compile()
  104. def __call__(self, source, filename="<input>", symbol="single"):
  105. r"""Compile a command and determine whether it is incomplete.
  106. Arguments:
  107. source -- the source string; may contain \n characters
  108. filename -- optional filename from which source was read;
  109. default "<input>"
  110. symbol -- optional grammar start symbol; "single" (default) or
  111. "eval"
  112. Return value / exceptions raised:
  113. - Return a code object if the command is complete and valid
  114. - Return None if the command is incomplete
  115. - Raise SyntaxError, ValueError or OverflowError if the command is a
  116. syntax error (OverflowError and ValueError can be produced by
  117. malformed literals).
  118. """
  119. return _maybe_compile(self.compiler, source, filename, symbol)