diff options
Diffstat (limited to 'python/helpers/pydev/pydev_console_utils.py')
-rw-r--r-- | python/helpers/pydev/pydev_console_utils.py | 165 |
1 files changed, 81 insertions, 84 deletions
diff --git a/python/helpers/pydev/pydev_console_utils.py b/python/helpers/pydev/pydev_console_utils.py index 571ae871b3ab..bd7b7de073d2 100644 --- a/python/helpers/pydev/pydev_console_utils.py +++ b/python/helpers/pydev/pydev_console_utils.py @@ -1,36 +1,12 @@ -from pydev_imports import xmlrpclib +from pydev_imports import xmlrpclib, _queue, Exec import sys - -import traceback - from pydevd_constants import USE_LIB_COPY from pydevd_constants import IS_JYTHON - -try: - if USE_LIB_COPY: - import _pydev_Queue as _queue - else: - import Queue as _queue -except: - import queue as _queue - -try: - from pydevd_exec import Exec -except: - from pydevd_exec2 import Exec - -try: - if USE_LIB_COPY: - import _pydev_thread as thread - else: - import thread -except: - import _thread as thread - +from _pydev_imps import _pydev_thread as thread import pydevd_xml import pydevd_vars - -from pydevd_utils import * +from pydevd_utils import * # @UnusedWildImport +import traceback #======================================================================================================================= # Null @@ -137,7 +113,7 @@ class CodeFragment: def __init__(self, text, is_single_line=True): self.text = text self.is_single_line = is_single_line - + def append(self, code_fragment): self.text = self.text + "\n" + code_fragment.text if not code_fragment.is_single_line: @@ -173,9 +149,12 @@ class BaseInterpreterInterface: self.buffer = code_fragment else: self.buffer.append(code_fragment) - + return self.needMoreForCode(self.buffer.text) + def createStdIn(self): + return StdIn(self, self.host, self.client_port) + def addExec(self, code_fragment): original_in = sys.stdin try: @@ -194,7 +173,7 @@ class BaseInterpreterInterface: more = False try: - sys.stdin = StdIn(self, self.host, self.client_port) + sys.stdin = self.createStdIn() try: if help is not None: #This will enable the help() function to work. @@ -209,8 +188,6 @@ class BaseInterpreterInterface: self._input_error_printed = True sys.stderr.write('\nError when trying to update pydoc.help.input\n') sys.stderr.write('(help() may not work -- please report this as a bug in the pydev bugtracker).\n\n') - import traceback - traceback.print_exc() try: @@ -241,8 +218,6 @@ class BaseInterpreterInterface: except SystemExit: raise except: - import traceback; - traceback.print_exc() return more @@ -251,7 +226,7 @@ class BaseInterpreterInterface: def doAddExec(self, codeFragment): ''' Subclasses should override. - + @return: more (True if more input is needed to complete the statement and False if the statement is complete). ''' raise NotImplementedError() @@ -260,7 +235,7 @@ class BaseInterpreterInterface: def getNamespace(self): ''' Subclasses should override. - + @return: dict with namespace. ''' raise NotImplementedError() @@ -312,14 +287,14 @@ class BaseInterpreterInterface: pass try: - #if no attempt succeeded, try to return repr()... + #if no attempt succeeded, try to return repr()... return repr(obj) except: try: - #otherwise the class + #otherwise the class return str(obj.__class__) except: - #if all fails, go to an empty string + #if all fails, go to an empty string return '' except: traceback.print_exc() @@ -353,6 +328,7 @@ class BaseInterpreterInterface: def interrupt(self): + self.buffer = None # Also clear the buffer when it's interrupted. try: if self.interruptable: if hasattr(thread, 'interrupt_main'): #Jython doesn't have it @@ -371,11 +347,13 @@ class BaseInterpreterInterface: self.interruptable = True def get_server(self): - if self.host is not None: + if getattr(self, 'host', None) is not None: return xmlrpclib.Server('http://%s:%s' % (self.host, self.client_port)) else: return None + server = property(get_server) + def finishExec(self, more): self.interruptable = False @@ -409,7 +387,12 @@ class BaseInterpreterInterface: return xml def changeVariable(self, attr, value): - Exec('%s=%s' % (attr, value), self.getNamespace(), self.getNamespace()) + def do_change_variable(): + Exec('%s=%s' % (attr, value), self.getNamespace(), self.getNamespace()) + + # Important: it has to be really enabled in the main thread, so, schedule + # it to run in the main thread. + self.exec_queue.put(do_change_variable) def _findFrame(self, thread_id, frame_id): ''' @@ -431,39 +414,48 @@ class BaseInterpreterInterface: Used to show console with variables connection. Mainly, monkey-patches things in the debugger structure so that the debugger protocol works. ''' - try: - # Try to import the packages needed to attach the debugger - import pydevd - import pydevd_vars - import threading - except: - # This happens on Jython embedded in host eclipse - import traceback;traceback.print_exc() - return ('pydevd is not available, cannot connect',) + def do_connect_to_debugger(): + try: + # Try to import the packages needed to attach the debugger + import pydevd + if USE_LIB_COPY: + import _pydev_threading as threading + else: + import threading - import pydev_localhost - threading.currentThread().__pydevd_id__ = "console_main" + except: + # This happens on Jython embedded in host eclipse + traceback.print_exc() + sys.stderr.write('pydevd is not available, cannot connect\n',) - self.orig_findFrame = pydevd_vars.findFrame - pydevd_vars.findFrame = self._findFrame + import pydev_localhost + threading.currentThread().__pydevd_id__ = "console_main" - self.debugger = pydevd.PyDB() - try: - self.debugger.connect(pydev_localhost.get_localhost(), debuggerPort) - self.debugger.prepareToRun() - import pydevd_tracing - pydevd_tracing.SetTrace(None) - except: - import traceback;traceback.print_exc() - return ('Failed to connect to target debugger.') + self.orig_findFrame = pydevd_vars.findFrame + pydevd_vars.findFrame = self._findFrame - # Register to process commands when idle - self.debugrunning = False - try: - self.server.setDebugHook(self.debugger.processInternalCommands) - except: - import traceback;traceback.print_exc() - return ('Version of Python does not support debuggable Interactive Console.') + self.debugger = pydevd.PyDB() + try: + self.debugger.connect(pydev_localhost.get_localhost(), debuggerPort) + self.debugger.prepareToRun() + import pydevd_tracing + pydevd_tracing.SetTrace(None) + except: + traceback.print_exc() + sys.stderr.write('Failed to connect to target debugger.\n') + + # Register to process commands when idle + self.debugrunning = False + try: + import pydevconsole + pydevconsole.set_debug_hook(self.debugger.processInternalCommands) + except: + traceback.print_exc() + sys.stderr.write('Version of Python does not support debuggable Interactive Console.\n') + + # Important: it has to be really enabled in the main thread, so, schedule + # it to run in the main thread. + self.exec_queue.put(do_connect_to_debugger) return ('connect complete',) @@ -476,19 +468,24 @@ class BaseInterpreterInterface: As with IPython, enabling multiple GUIs isn't an error, but only the last one's main loop runs and it may not work ''' - from pydev_versioncheck import versionok_for_gui - if versionok_for_gui(): - try: - from pydev_ipython.inputhook import enable_gui - enable_gui(guiname) - except: - sys.stderr.write("Failed to enable GUI event loop integration for '%s'\n" % guiname) - import traceback;traceback.print_exc() - elif guiname not in ['none', '', None]: - # Only print a warning if the guiname was going to do something - sys.stderr.write("PyDev console: Python version does not support GUI event loop integration for '%s'\n" % guiname) - # Return value does not matter, so return back what was sent - return guiname + def do_enable_gui(): + from pydev_versioncheck import versionok_for_gui + if versionok_for_gui(): + try: + from pydev_ipython.inputhook import enable_gui + enable_gui(guiname) + except: + sys.stderr.write("Failed to enable GUI event loop integration for '%s'\n" % guiname) + traceback.print_exc() + elif guiname not in ['none', '', None]: + # Only print a warning if the guiname was going to do something + sys.stderr.write("PyDev console: Python version does not support GUI event loop integration for '%s'\n" % guiname) + # Return value does not matter, so return back what was sent + return guiname + + # Important: it has to be really enabled in the main thread, so, schedule + # it to run in the main thread. + self.exec_queue.put(do_enable_gui) #======================================================================================================================= # FakeFrame |