PageRenderTime 48ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/python/helpers/pydev/_pydevd_bundle/pydevconsole_code_for_ironpython.py

http://github.com/JetBrains/intellij-community
Python | 513 lines | 492 code | 1 blank | 20 comment | 0 complexity | aa13976f230223a2242e62b827167b23 MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0, MPL-2.0-no-copyleft-exception, MIT, EPL-1.0, AGPL-1.0
  1. """Utilities needed to emulate Python's interactive interpreter.
  2. """
  3. # Inspired by similar code by Jeff Epler and Fredrik Lundh.
  4. import sys
  5. import traceback
  6. #START --------------------------- from codeop import CommandCompiler, compile_command
  7. #START --------------------------- from codeop import CommandCompiler, compile_command
  8. #START --------------------------- from codeop import CommandCompiler, compile_command
  9. #START --------------------------- from codeop import CommandCompiler, compile_command
  10. #START --------------------------- from codeop import CommandCompiler, compile_command
  11. r"""Utilities to compile possibly incomplete Python source code.
  12. This module provides two interfaces, broadly similar to the builtin
  13. function compile(), which take program text, a filename and a 'mode'
  14. and:
  15. - Return code object if the command is complete and valid
  16. - Return None if the command is incomplete
  17. - Raise SyntaxError, ValueError or OverflowError if the command is a
  18. syntax error (OverflowError and ValueError can be produced by
  19. malformed literals).
  20. Approach:
  21. First, check if the source consists entirely of blank lines and
  22. comments; if so, replace it with 'pass', because the built-in
  23. parser doesn't always do the right thing for these.
  24. Compile three times: as is, with \n, and with \n\n appended. If it
  25. compiles as is, it's complete. If it compiles with one \n appended,
  26. we expect more. If it doesn't compile either way, we compare the
  27. error we get when compiling with \n or \n\n appended. If the errors
  28. are the same, the code is broken. But if the errors are different, we
  29. expect more. Not intuitive; not even guaranteed to hold in future
  30. releases; but this matches the compiler's behavior from Python 1.4
  31. through 2.2, at least.
  32. Caveat:
  33. It is possible (but not likely) that the parser stops parsing with a
  34. successful outcome before reaching the end of the source; in this
  35. case, trailing symbols may be ignored instead of causing an error.
  36. For example, a backslash followed by two newlines may be followed by
  37. arbitrary garbage. This will be fixed once the API for the parser is
  38. better.
  39. The two interfaces are:
  40. compile_command(source, filename, symbol):
  41. Compiles a single command in the manner described above.
  42. CommandCompiler():
  43. Instances of this class have __call__ methods identical in
  44. signature to compile_command; the difference is that if the
  45. instance compiles program text containing a __future__ statement,
  46. the instance 'remembers' and compiles all subsequent program texts
  47. with the statement in force.
  48. The module also provides another class:
  49. Compile():
  50. Instances of this class act like the built-in function compile,
  51. but with 'memory' in the sense described above.
  52. """
  53. import __future__
  54. _features = [getattr(__future__, fname)
  55. for fname in __future__.all_feature_names]
  56. __all__ = ["compile_command", "Compile", "CommandCompiler"]
  57. PyCF_DONT_IMPLY_DEDENT = 0x200 # Matches pythonrun.h
  58. def _maybe_compile(compiler, source, filename, symbol):
  59. # Check for source consisting of only blank lines and comments
  60. for line in source.split("\n"):
  61. line = line.strip()
  62. if line and line[0] != '#':
  63. break # Leave it alone
  64. else:
  65. if symbol != "eval":
  66. source = "pass" # Replace it with a 'pass' statement
  67. err = err1 = err2 = None
  68. code = code1 = code2 = None
  69. try:
  70. code = compiler(source, filename, symbol)
  71. except SyntaxError:
  72. pass
  73. try:
  74. code1 = compiler(source + "\n", filename, symbol)
  75. except SyntaxError as err1:
  76. pass
  77. try:
  78. code2 = compiler(source + "\n\n", filename, symbol)
  79. except SyntaxError as err2:
  80. pass
  81. if code:
  82. return code
  83. if not code1 and repr(err1) == repr(err2):
  84. raise SyntaxError(err1)
  85. def _compile(source, filename, symbol):
  86. return compile(source, filename, symbol, PyCF_DONT_IMPLY_DEDENT)
  87. def compile_command(source, filename="<input>", symbol="single"):
  88. r"""Compile a command and determine whether it is incomplete.
  89. Arguments:
  90. source -- the source string; may contain \n characters
  91. filename -- optional filename from which source was read; default
  92. "<input>"
  93. symbol -- optional grammar start symbol; "single" (default) or "eval"
  94. Return value / exceptions raised:
  95. - Return a code object if the command is complete and valid
  96. - Return None if the command is incomplete
  97. - Raise SyntaxError, ValueError or OverflowError if the command is a
  98. syntax error (OverflowError and ValueError can be produced by
  99. malformed literals).
  100. """
  101. return _maybe_compile(_compile, source, filename, symbol)
  102. class Compile:
  103. """Instances of this class behave much like the built-in compile
  104. function, but if one is used to compile text containing a future
  105. statement, it "remembers" and compiles all subsequent program texts
  106. with the statement in force."""
  107. def __init__(self):
  108. self.flags = PyCF_DONT_IMPLY_DEDENT
  109. def __call__(self, source, filename, symbol):
  110. codeob = compile(source, filename, symbol, self.flags, 1)
  111. for feature in _features:
  112. if codeob.co_flags & feature.compiler_flag:
  113. self.flags |= feature.compiler_flag
  114. return codeob
  115. class CommandCompiler:
  116. """Instances of this class have __call__ methods identical in
  117. signature to compile_command; the difference is that if the
  118. instance compiles program text containing a __future__ statement,
  119. the instance 'remembers' and compiles all subsequent program texts
  120. with the statement in force."""
  121. def __init__(self,):
  122. self.compiler = Compile()
  123. def __call__(self, source, filename="<input>", symbol="single"):
  124. r"""Compile a command and determine whether it is incomplete.
  125. Arguments:
  126. source -- the source string; may contain \n characters
  127. filename -- optional filename from which source was read;
  128. default "<input>"
  129. symbol -- optional grammar start symbol; "single" (default) or
  130. "eval"
  131. Return value / exceptions raised:
  132. - Return a code object if the command is complete and valid
  133. - Return None if the command is incomplete
  134. - Raise SyntaxError, ValueError or OverflowError if the command is a
  135. syntax error (OverflowError and ValueError can be produced by
  136. malformed literals).
  137. """
  138. return _maybe_compile(self.compiler, source, filename, symbol)
  139. #END --------------------------- from codeop import CommandCompiler, compile_command
  140. #END --------------------------- from codeop import CommandCompiler, compile_command
  141. #END --------------------------- from codeop import CommandCompiler, compile_command
  142. #END --------------------------- from codeop import CommandCompiler, compile_command
  143. #END --------------------------- from codeop import CommandCompiler, compile_command
  144. __all__ = ["InteractiveInterpreter", "InteractiveConsole", "interact",
  145. "compile_command"]
  146. def softspace(file, newvalue):
  147. oldvalue = 0
  148. try:
  149. oldvalue = file.softspace
  150. except AttributeError:
  151. pass
  152. try:
  153. file.softspace = newvalue
  154. except (AttributeError, TypeError):
  155. # "attribute-less object" or "read-only attributes"
  156. pass
  157. return oldvalue
  158. class InteractiveInterpreter:
  159. """Base class for InteractiveConsole.
  160. This class deals with parsing and interpreter state (the user's
  161. namespace); it doesn't deal with input buffering or prompting or
  162. input file naming (the filename is always passed in explicitly).
  163. """
  164. def __init__(self, locals=None):
  165. """Constructor.
  166. The optional 'locals' argument specifies the dictionary in
  167. which code will be executed; it defaults to a newly created
  168. dictionary with key "__name__" set to "__console__" and key
  169. "__doc__" set to None.
  170. """
  171. if locals is None:
  172. locals = {"__name__": "__console__", "__doc__": None}
  173. self.locals = locals
  174. self.compile = CommandCompiler()
  175. def runsource(self, source, filename="<input>", symbol="single"):
  176. """Compile and run some source in the interpreter.
  177. Arguments are as for compile_command().
  178. One several things can happen:
  179. 1) The input is incorrect; compile_command() raised an
  180. exception (SyntaxError or OverflowError). A syntax traceback
  181. will be printed by calling the showsyntaxerror() method.
  182. 2) The input is incomplete, and more input is required;
  183. compile_command() returned None. Nothing happens.
  184. 3) The input is complete; compile_command() returned a code
  185. object. The code is executed by calling self.runcode() (which
  186. also handles run-time exceptions, except for SystemExit).
  187. The return value is True in case 2, False in the other cases (unless
  188. an exception is raised). The return value can be used to
  189. decide whether to use sys.ps1 or sys.ps2 to prompt the next
  190. line.
  191. """
  192. try:
  193. code = self.compile(source, filename, symbol)
  194. except (OverflowError, SyntaxError, ValueError):
  195. # Case 1
  196. self.showsyntaxerror(filename)
  197. return False
  198. if code is None:
  199. # Case 2
  200. return True
  201. # Case 3
  202. self.runcode(code)
  203. return False
  204. def runcode(self, code):
  205. """Execute a code object.
  206. When an exception occurs, self.showtraceback() is called to
  207. display a traceback. All exceptions are caught except
  208. SystemExit, which is reraised.
  209. A note about KeyboardInterrupt: this exception may occur
  210. elsewhere in this code, and may not always be caught. The
  211. caller should be prepared to deal with it.
  212. """
  213. try:
  214. exec code in self.locals
  215. except SystemExit:
  216. raise
  217. except:
  218. self.showtraceback()
  219. else:
  220. if softspace(sys.stdout, 0):
  221. sys.stdout.write('\n')
  222. def showsyntaxerror(self, filename=None):
  223. """Display the syntax error that just occurred.
  224. This doesn't display a stack trace because there isn't one.
  225. If a filename is given, it is stuffed in the exception instead
  226. of what was there before (because Python's parser always uses
  227. "<string>" when reading from a string).
  228. The output is written by self.write(), below.
  229. """
  230. type, value, sys.last_traceback = sys.exc_info()
  231. sys.last_type = type
  232. sys.last_value = value
  233. if filename and type is SyntaxError:
  234. # Work hard to stuff the correct filename in the exception
  235. try:
  236. msg, (dummy_filename, lineno, offset, line) = value
  237. except:
  238. # Not the format we expect; leave it alone
  239. pass
  240. else:
  241. # Stuff in the right filename
  242. value = SyntaxError(msg, (filename, lineno, offset, line))
  243. sys.last_value = value
  244. list = traceback.format_exception_only(type, value)
  245. map(self.write, list)
  246. def showtraceback(self):
  247. """Display the exception that just occurred.
  248. We remove the first stack item because it is our own code.
  249. The output is written by self.write(), below.
  250. """
  251. try:
  252. type, value, tb = sys.exc_info()
  253. sys.last_type = type
  254. sys.last_value = value
  255. sys.last_traceback = tb
  256. tblist = traceback.extract_tb(tb)
  257. del tblist[:1]
  258. list = traceback.format_list(tblist)
  259. if list:
  260. list.insert(0, "Traceback (most recent call last):\n")
  261. list[len(list):] = traceback.format_exception_only(type, value)
  262. finally:
  263. tblist = tb = None
  264. map(self.write, list)
  265. def write(self, data):
  266. """Write a string.
  267. The base implementation writes to sys.stderr; a subclass may
  268. replace this with a different implementation.
  269. """
  270. sys.stderr.write(data)
  271. class InteractiveConsole(InteractiveInterpreter):
  272. """Closely emulate the behavior of the interactive Python interpreter.
  273. This class builds on InteractiveInterpreter and adds prompting
  274. using the familiar sys.ps1 and sys.ps2, and input buffering.
  275. """
  276. def __init__(self, locals=None, filename="<console>"):
  277. """Constructor.
  278. The optional locals argument will be passed to the
  279. InteractiveInterpreter base class.
  280. The optional filename argument should specify the (file)name
  281. of the input stream; it will show up in tracebacks.
  282. """
  283. InteractiveInterpreter.__init__(self, locals)
  284. self.filename = filename
  285. self.resetbuffer()
  286. def resetbuffer(self):
  287. """Reset the input buffer."""
  288. self.buffer = []
  289. def interact(self, banner=None):
  290. """Closely emulate the interactive Python console.
  291. The optional banner argument specify the banner to print
  292. before the first interaction; by default it prints a banner
  293. similar to the one printed by the real Python interpreter,
  294. followed by the current class name in parentheses (so as not
  295. to confuse this with the real interpreter -- since it's so
  296. close!).
  297. """
  298. try:
  299. sys.ps1 #@UndefinedVariable
  300. except AttributeError:
  301. sys.ps1 = ">>> "
  302. try:
  303. sys.ps2 #@UndefinedVariable
  304. except AttributeError:
  305. sys.ps2 = "... "
  306. cprt = 'Type "help", "copyright", "credits" or "license" for more information.'
  307. if banner is None:
  308. self.write("Python %s on %s\n%s\n(%s)\n" %
  309. (sys.version, sys.platform, cprt,
  310. self.__class__.__name__))
  311. else:
  312. self.write("%s\n" % str(banner))
  313. more = 0
  314. while 1:
  315. try:
  316. if more:
  317. prompt = sys.ps2 #@UndefinedVariable
  318. else:
  319. prompt = sys.ps1 #@UndefinedVariable
  320. try:
  321. line = self.raw_input(prompt)
  322. # Can be None if sys.stdin was redefined
  323. encoding = getattr(sys.stdin, "encoding", None)
  324. if encoding and not isinstance(line, unicode):
  325. line = line.decode(encoding)
  326. except EOFError:
  327. self.write("\n")
  328. break
  329. else:
  330. more = self.push(line)
  331. except KeyboardInterrupt:
  332. self.write("\nKeyboardInterrupt\n")
  333. self.resetbuffer()
  334. more = 0
  335. def push(self, line):
  336. """Push a line to the interpreter.
  337. The line should not have a trailing newline; it may have
  338. internal newlines. The line is appended to a buffer and the
  339. interpreter's runsource() method is called with the
  340. concatenated contents of the buffer as source. If this
  341. indicates that the command was executed or invalid, the buffer
  342. is reset; otherwise, the command is incomplete, and the buffer
  343. is left as it was after the line was appended. The return
  344. value is 1 if more input is required, 0 if the line was dealt
  345. with in some way (this is the same as runsource()).
  346. """
  347. self.buffer.append(line)
  348. source = "\n".join(self.buffer)
  349. more = self.runsource(source, self.filename)
  350. if not more:
  351. self.resetbuffer()
  352. return more
  353. def raw_input(self, prompt=""):
  354. """Write a prompt and read a line.
  355. The returned line does not include the trailing newline.
  356. When the user enters the EOF key sequence, EOFError is raised.
  357. The base implementation uses the built-in function
  358. raw_input(); a subclass may replace this with a different
  359. implementation.
  360. """
  361. return raw_input(prompt)
  362. def interact(banner=None, readfunc=None, local=None):
  363. """Closely emulate the interactive Python interpreter.
  364. This is a backwards compatible interface to the InteractiveConsole
  365. class. When readfunc is not specified, it attempts to import the
  366. readline module to enable GNU readline if it is available.
  367. Arguments (all optional, all default to None):
  368. banner -- passed to InteractiveConsole.interact()
  369. readfunc -- if not None, replaces InteractiveConsole.raw_input()
  370. local -- passed to InteractiveInterpreter.__init__()
  371. """
  372. console = InteractiveConsole(local)
  373. if readfunc is not None:
  374. console.raw_input = readfunc
  375. else:
  376. try:
  377. import readline
  378. except ImportError:
  379. pass
  380. console.interact(banner)
  381. if __name__ == '__main__':
  382. import pdb
  383. pdb.run("interact()\n")