logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

codeop.py (6568B)


  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. Approach:
  11. First, check if the source consists entirely of blank lines and
  12. comments; if so, replace it with 'pass', because the built-in
  13. parser doesn't always do the right thing for these.
  14. Compile three times: as is, with \n, and with \n\n appended. If it
  15. compiles as is, it's complete. If it compiles with one \n appended,
  16. we expect more. If it doesn't compile either way, we compare the
  17. error we get when compiling with \n or \n\n appended. If the errors
  18. are the same, the code is broken. But if the errors are different, we
  19. expect more. Not intuitive; not even guaranteed to hold in future
  20. releases; but this matches the compiler's behavior from Python 1.4
  21. through 2.2, at least.
  22. Caveat:
  23. It is possible (but not likely) that the parser stops parsing with a
  24. successful outcome before reaching the end of the source; in this
  25. case, trailing symbols may be ignored instead of causing an error.
  26. For example, a backslash followed by two newlines may be followed by
  27. arbitrary garbage. This will be fixed once the API for the parser is
  28. better.
  29. The two interfaces are:
  30. compile_command(source, filename, symbol):
  31. Compiles a single command in the manner described above.
  32. CommandCompiler():
  33. Instances of this class have __call__ methods identical in
  34. signature to compile_command; the difference is that if the
  35. instance compiles program text containing a __future__ statement,
  36. the instance 'remembers' and compiles all subsequent program texts
  37. with the statement in force.
  38. The module also provides another class:
  39. Compile():
  40. Instances of this class act like the built-in function compile,
  41. but with 'memory' in the sense described above.
  42. """
  43. import __future__
  44. import warnings
  45. _features = [getattr(__future__, fname)
  46. for fname in __future__.all_feature_names]
  47. __all__ = ["compile_command", "Compile", "CommandCompiler"]
  48. PyCF_DONT_IMPLY_DEDENT = 0x200 # Matches pythonrun.h.
  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. try:
  59. return compiler(source, filename, symbol)
  60. except SyntaxError: # Let other compile() errors propagate.
  61. pass
  62. # Catch syntax warnings after the first compile
  63. # to emit warnings (SyntaxWarning, DeprecationWarning) at most once.
  64. with warnings.catch_warnings():
  65. warnings.simplefilter("error")
  66. code1 = err1 = err2 = None
  67. try:
  68. code1 = compiler(source + "\n", filename, symbol)
  69. except SyntaxError as e:
  70. err1 = e
  71. try:
  72. code2 = compiler(source + "\n\n", filename, symbol)
  73. except SyntaxError as e:
  74. err2 = e
  75. try:
  76. if not code1 and _is_syntax_error(err1, err2):
  77. raise err1
  78. else:
  79. return None
  80. finally:
  81. err1 = err2 = None
  82. def _is_syntax_error(err1, err2):
  83. rep1 = repr(err1)
  84. rep2 = repr(err2)
  85. if "was never closed" in rep1 and "was never closed" in rep2:
  86. return False
  87. if rep1 == rep2:
  88. return True
  89. return False
  90. def _compile(source, filename, symbol):
  91. return compile(source, filename, symbol, PyCF_DONT_IMPLY_DEDENT)
  92. def compile_command(source, filename="<input>", symbol="single"):
  93. r"""Compile a command and determine whether it is incomplete.
  94. Arguments:
  95. source -- the source string; may contain \n characters
  96. filename -- optional filename from which source was read; default
  97. "<input>"
  98. symbol -- optional grammar start symbol; "single" (default), "exec"
  99. or "eval"
  100. Return value / exceptions raised:
  101. - Return a code object if the command is complete and valid
  102. - Return None if the command is incomplete
  103. - Raise SyntaxError, ValueError or OverflowError if the command is a
  104. syntax error (OverflowError and ValueError can be produced by
  105. malformed literals).
  106. """
  107. return _maybe_compile(_compile, source, filename, symbol)
  108. class Compile:
  109. """Instances of this class behave much like the built-in compile
  110. function, but if one is used to compile text containing a future
  111. statement, it "remembers" and compiles all subsequent program texts
  112. with the statement in force."""
  113. def __init__(self):
  114. self.flags = PyCF_DONT_IMPLY_DEDENT
  115. def __call__(self, source, filename, symbol):
  116. codeob = compile(source, filename, symbol, self.flags, True)
  117. for feature in _features:
  118. if codeob.co_flags & feature.compiler_flag:
  119. self.flags |= feature.compiler_flag
  120. return codeob
  121. class CommandCompiler:
  122. """Instances of this class have __call__ methods identical in
  123. signature to compile_command; the difference is that if the
  124. instance compiles program text containing a __future__ statement,
  125. the instance 'remembers' and compiles all subsequent program texts
  126. with the statement in force."""
  127. def __init__(self,):
  128. self.compiler = Compile()
  129. def __call__(self, source, filename="<input>", symbol="single"):
  130. r"""Compile a command and determine whether it is incomplete.
  131. Arguments:
  132. source -- the source string; may contain \n characters
  133. filename -- optional filename from which source was read;
  134. default "<input>"
  135. symbol -- optional grammar start symbol; "single" (default) or
  136. "eval"
  137. Return value / exceptions raised:
  138. - Return a code object if the command is complete and valid
  139. - Return None if the command is incomplete
  140. - Raise SyntaxError, ValueError or OverflowError if the command is a
  141. syntax error (OverflowError and ValueError can be produced by
  142. malformed literals).
  143. """
  144. return _maybe_compile(self.compiler, source, filename, symbol)