def runsource(self, source, filename='<input>', symbol='single'): if self._is_native: return InteractiveShell.runsource(self, source, filename, symbol) source=source.encode(self.stdin_encoding) if source[:1] in [' ', '\t']: source = 'if 1:\n%s' % source try: code = self.compile(source, filename, symbol) except (OverflowError, SyntaxError, ValueError, TypeError, MemoryError): # Case 1 self.showsyntaxerror(filename) return None if code is None: # Case 2 return True # Case 3 #print "\n Code to translate:\n" + source + "\n" if source.startswith('_ip.'): # This is IPython Magick stuff. Run in native interpreter: return InteractiveShell.runsource(self, source, filename, symbol) # Translate try: jscode, imppy, impjs = self._js_int.translate(source) self.code_to_run = jscode except Exception, e: print_exc() return None
def runsource(self, source, filename="<input>", symbol="single"): """Compile and run some source in the interpreter. Modified version of code.py's runsource(), to handle threading issues. See the original for full docstring details.""" # If Ctrl-C was typed, we reset the flag and return right away if shellglobals.KBINT: shellglobals.KBINT = False return False if self._kill: # can't queue new code if we are being killed return True try: code = self.compile(source, filename, symbol) except (OverflowError, SyntaxError, ValueError): # Case 1 self.showsyntaxerror(filename) return False if code is None: # Case 2 return True # shortcut - if we are in worker thread, or the worker thread is not running, # execute directly (to allow recursion and prevent deadlock if code is run early # in IPython construction) if (not self.reactor_started or (self.worker_ident is None and not self.first_run) or self.worker_ident == thread.get_ident() or shellglobals.run_in_frontend(source)): InteractiveShell.runcode(self,code) return # Case 3 # Store code in queue, so the execution thread can handle it. self.first_run = False completed_ev, received_ev = threading.Event(), threading.Event() self.code_queue.put((code,completed_ev, received_ev)) reactor.callLater(0.0,self.runcode) received_ev.wait(5) if not received_ev.isSet(): # the mainloop is dead, start executing code directly print "Warning: Timeout for mainloop thread exceeded" print "switching to nonthreaded mode (until mainloop wakes up again)" self.worker_ident = None else: completed_ev.wait() return False
def runcode(self): """Execute a code object. Multithreaded wrapper around IPython's runcode().""" global CODE_RUN # Exceptions need to be raised differently depending on which thread is # active CODE_RUN = True # lock thread-protected stuff got_lock = self.thread_ready.acquire(False) if self._kill: print >>Term.cout, 'Closing threads...', Term.cout.flush() for tokill in self.on_kill: tokill() print >>Term.cout, 'Done.' # Install sigint handler. We do it every time to ensure that if user # code modifies it, we restore our own handling. try: signal(SIGINT,sigint_handler) except SystemError: # This happens under Windows, which seems to have all sorts # of problems with signal handling. Oh well... pass # Flush queue of pending code by calling the run methood of the parent # class with all items which may be in the queue. while 1: try: code_to_run = self.code_queue.get_nowait() except Queue.Empty: break if got_lock: self.thread_ready.notify() InteractiveShell.runcode(self,code_to_run) else: break # We're done with thread-protected variables if got_lock: self.thread_ready.release() # We're done... CODE_RUN = False # This MUST return true for gtk threading to work return True
def __init__(self, name, usage=None, rc=Struct(opts=None, args=None), user_ns=None, **kw): user_ns, b2 = self._matplotlib_config(name) InteractiveShell.__init__(self, name, usage, rc, user_ns, banner2=b2, **kw)
def prefilter_PQ(self,line,continuation): """Alternate prefilter for input of PhysicalQuantityInteractive objects. This assumes that the function PhysicalQuantityInteractive() has been imported.""" from re import match from IPython.iplib import InteractiveShell # This regexp is what does the real work unit_split = match(r'\s*(\w+)\s*=\s*(-?\d*\.?\d*[eE]?-?\d*)\s+([a-zA-Z].*)', line) # If special input was ecnountered, process it: if unit_split: var,val,units = unit_split.groups() if var and val and units: units = units.replace('^','**') # Now a valid line needs to be constructed for IPython to process: line = var +" = PhysicalQuantityInteractive(" + val + ", '" + \ units + "')" #print 'New line:',line # dbg # In the end, always call the default IPython _prefilter() function. Note # that self must be passed explicitly, b/c we're calling the unbound class # method (since this method will overwrite the instance prefilter()) return InteractiveShell._prefilter(self,line,continuation)
def prefilter_PQ(self, line, continuation): """Alternate prefilter for input of PhysicalQuantityInteractive objects. This assumes that the function PhysicalQuantityInteractive() has been imported.""" from re import match from IPython.iplib import InteractiveShell # This regexp is what does the real work unit_split = match( r'\s*(\w+)\s*=\s*(-?\d*\.?\d*[eE]?-?\d*)\s+([a-zA-Z].*)', line) # If special input was ecnountered, process it: if unit_split: var, val, units = unit_split.groups() if var and val and units: units = units.replace('^', '**') # Now a valid line needs to be constructed for IPython to process: line = var +" = PhysicalQuantityInteractive(" + val + ", '" + \ units + "')" #print 'New line:',line # dbg # In the end, always call the default IPython _prefilter() function. Note # that self must be passed explicitly, b/c we're calling the unbound class # method (since this method will overwrite the instance prefilter()) return InteractiveShell._prefilter(self, line, continuation)
def prefilter_paste(self, line, continuation): """Alternate prefilter for input of pasted code from an interpreter. """ from re import match from IPython.iplib import InteractiveShell if match(r'^>>> |^\.\.\. ', line): # In the end, always call the default IPython _prefilter() function. # Note that self must be passed explicitly, b/c we're calling the # unbound class method (since this method will overwrite the instance # prefilter()) return InteractiveShell._prefilter(self, line[4:], continuation) elif line.strip() == '...': return InteractiveShell._prefilter(self, '', continuation) else: return InteractiveShell._prefilter(self, line, continuation)
def runcode(self): """Execute a code object. Multithreaded wrapper around IPython's runcode().""" # lock thread-protected stuff got_lock = self.thread_ready.acquire(False) # Install sigint handler try: signal.signal(signal.SIGINT, sigint_handler) except SystemError: # This happens under Windows, which seems to have all sorts # of problems with signal handling. Oh well... pass if self._kill: print >> Term.cout, 'Closing threads...', Term.cout.flush() for tokill in self.on_kill: tokill() print >> Term.cout, 'Done.' # Flush queue of pending code by calling the run methood of the parent # class with all items which may be in the queue. while 1: try: code_to_run = self.code_queue.get_nowait() except Queue.Empty: break if got_lock: self.thread_ready.notify() InteractiveShell.runcode(self, code_to_run) else: break # We're done with thread-protected variables if got_lock: self.thread_ready.release() # This MUST return true for gtk threading to work return True
def __init__(self,name,usage=None,rc=Struct(opts=None,args=None), user_ns=None,user_global_ns=None,banner2='',**kw): """Similar to the normal InteractiveShell, but with threading control""" InteractiveShell.__init__(self,name,usage,rc,user_ns, user_global_ns,banner2) # A queue to hold the code to be executed. self.code_queue = Queue.Queue() # Stuff to do at closing time self._kill = None on_kill = kw.get('on_kill', []) # Check that all things to kill are callable: for t in on_kill: if not callable(t): raise TypeError,'on_kill must be a list of callables' self.on_kill = on_kill # thread identity of the "worker thread" (that may execute code directly) self.worker_ident = None
def __init__(self,name,usage=None,rc=Struct(opts=None,args=None), user_ns=None,user_global_ns=None,banner2='',**kw): """Similar to the normal InteractiveShell, but with threading control""" InteractiveShell.__init__(self,name,usage,rc,user_ns, user_global_ns,banner2) # Locking control variable. self.thread_ready = threading.Condition(threading.RLock()) # A queue to hold the code to be executed. A scalar variable is NOT # enough, because uses like macros cause reentrancy. self.code_queue = Queue.Queue() # Stuff to do at closing time self._kill = False on_kill = kw.get('on_kill') if on_kill is None: on_kill = [] # Check that all things to kill are callable: for t in on_kill: if not callable(t): raise TypeError,'on_kill must be a list of callables' self.on_kill = on_kill
def sage_prefilter(self, block, continuation): """ Sage's prefilter for input. Given a string block (usually a line), return the preparsed version of it. INPUT: - block -- string (usually a single line, but not always) - continuation -- whether or not this line is a continuation. """ try: block2 = '' first = True B = block.split('\n') for i in range(len(B)): L = B[i] M = do_prefilter_paste(L, continuation or (not first)) first = False # The L[:len(L)-len(L.lstrip())] business here preserves # the whitespace at the beginning of L. if block2 != '': block2 += '\n' lstrip = L.lstrip() if lstrip[:5] == 'sage:' or lstrip[:3] == '>>>' or i==0: block2 += M else: block2 += L[:len(L)-len(lstrip)] + M except None: print "WARNING: An error occurred in the Sage parser while" print "parsing the following block:" print block print "Please report this as a bug (include the output of typing '%hist')." block2 = block return InteractiveShell._prefilter(self, block2, continuation)
def sage_prefilter(self, block, continuation): """ Sage's prefilter for input. Given a string block (usually a line), return the preparsed version of it. INPUT: - block -- string (usually a single line, but not always) - continuation -- whether or not this line is a continuation. """ try: block2 = '' first = True B = block.split('\n') for i in range(len(B)): L = B[i] M = do_prefilter_paste(L, continuation or (not first)) first = False # The L[:len(L)-len(L.lstrip())] business here preserves # the whitespace at the beginning of L. if block2 != '': block2 += '\n' lstrip = L.lstrip() if lstrip[:5] == 'sage:' or lstrip[:3] == '>>>' or i == 0: block2 += M else: block2 += L[:len(L) - len(lstrip)] + M except None: print "WARNING: An error occurred in the Sage parser while" print "parsing the following block:" print block print "Please report this as a bug (include the output of typing '%hist')." block2 = block return InteractiveShell._prefilter(self, block2, continuation)
def __init__(self, name, **kw): self.parentclass = InteractiveShell AUTOInteractive.__init__(self, kw["user_ns"], None) InteractiveShell.__init__(self, name, **kw)
def handle_normal(self, line): if line.line.startswith('$'): self.callback.process_message(line.line[1:]) self.callback.check_response_queue() return '' return InteractiveShell.handle_normal(self, line)
def runcode(self): """Execute a code object. Multithreaded wrapper around IPython's runcode().""" global CODE_RUN # we are in worker thread, stash out the id for runsource() self.worker_ident = thread.get_ident() if self._kill: print >>Term.cout, 'Closing threads...', Term.cout.flush() for tokill in self.on_kill: tokill() print >>Term.cout, 'Done.' # allow kill() to return self._kill.set() return True # Install sigint handler. We do it every time to ensure that if user # code modifies it, we restore our own handling. try: signal(SIGINT,sigint_handler) except SystemError: # This happens under Windows, which seems to have all sorts # of problems with signal handling. Oh well... pass # Flush queue of pending code by calling the run methood of the parent # class with all items which may be in the queue. code_to_run = None while 1: try: code_to_run, completed_ev, received_ev = self.code_queue.get_nowait() except Queue.Empty: break received_ev.set() # Exceptions need to be raised differently depending on which # thread is active. This convoluted try/except is only there to # protect against asynchronous exceptions, to ensure that a KBINT # at the wrong time doesn't deadlock everything. The global # CODE_TO_RUN is set to true/false as close as possible to the # runcode() call, so that the KBINT handler is correctly informed. try: try: CODE_RUN = True InteractiveShell.runcode(self,code_to_run) except KeyboardInterrupt: print "Keyboard interrupted in mainloop" while not self.code_queue.empty(): code, ev1,ev2 = self.code_queue.get_nowait() ev1.set() ev2.set() break finally: CODE_RUN = False # allow runsource() return from wait completed_ev.set() # This MUST return true for gtk threading to work return True
def __init__(self,name,usage=None,rc=Struct(opts=None,args=None), user_ns=None,user_global_ns=None,**kw): user_ns,user_global_ns,b2 = self._matplotlib_config(name,user_ns,user_global_ns) InteractiveShell.__init__(self,name,usage,rc,user_ns,user_global_ns, banner2=b2,**kw)
def runcode(self): """Execute a code object. Multithreaded wrapper around IPython's runcode().""" global CODE_RUN # lock thread-protected stuff got_lock = self.thread_ready.acquire() if self._kill: print >>Term.cout, 'Closing threads...', Term.cout.flush() for tokill in self.on_kill: tokill() print >>Term.cout, 'Done.' # Install sigint handler. We do it every time to ensure that if user # code modifies it, we restore our own handling. try: signal(SIGINT,sigint_handler) except SystemError: # This happens under Windows, which seems to have all sorts # of problems with signal handling. Oh well... pass # Flush queue of pending code by calling the run methood of the parent # class with all items which may be in the queue. code_to_run = None while 1: try: code_to_run = self.code_queue.get_nowait() except Queue.Empty: break # Exceptions need to be raised differently depending on which # thread is active. This convoluted try/except is only there to # protect against asynchronous exceptions, to ensure that a KBINT # at the wrong time doesn't deadlock everything. The global # CODE_TO_RUN is set to true/false as close as possible to the # runcode() call, so that the KBINT handler is correctly informed. try: try: CODE_RUN = True InteractiveShell.runcode(self,code_to_run) except KeyboardInterrupt: print "Keyboard interrupted in mainloop" while not self.code_queue.empty(): self.code_queue.get_nowait() break finally: if got_lock: CODE_RUN = False # We're done with thread-protected variables if code_to_run is not None: self.thread_ready.notify() self.thread_ready.release() # We're done... CODE_RUN = False # This MUST return true for gtk threading to work return True
def prefilter(self, line, continue_prompt): if not continue_prompt: line = self.processShorthand(line) line = InteractiveShell.prefilter(self, line, continue_prompt) return line
def runcode(self): """Execute a code object. Multithreaded wrapper around IPython's runcode().""" global CODE_RUN # lock thread-protected stuff got_lock = self.thread_ready.acquire() if self._kill: print >> Term.cout, 'Closing threads...', Term.cout.flush() for tokill in self.on_kill: tokill() print >> Term.cout, 'Done.' # Install sigint handler. We do it every time to ensure that if user # code modifies it, we restore our own handling. try: signal(SIGINT, sigint_handler) except SystemError: # This happens under Windows, which seems to have all sorts # of problems with signal handling. Oh well... pass # Flush queue of pending code by calling the run methood of the parent # class with all items which may be in the queue. code_to_run = None while 1: try: code_to_run = self.code_queue.get_nowait() except Queue.Empty: break # Exceptions need to be raised differently depending on which # thread is active. This convoluted try/except is only there to # protect against asynchronous exceptions, to ensure that a KBINT # at the wrong time doesn't deadlock everything. The global # CODE_TO_RUN is set to true/false as close as possible to the # runcode() call, so that the KBINT handler is correctly informed. try: try: CODE_RUN = True InteractiveShell.runcode(self, code_to_run) except KeyboardInterrupt: print "Keyboard interrupted in mainloop" while not self.code_queue.empty(): self.code_queue.get_nowait() break finally: if got_lock: CODE_RUN = False # We're done with thread-protected variables if code_to_run is not None: self.thread_ready.notify() self.thread_ready.release() # We're done... CODE_RUN = False # This MUST return true for gtk threading to work return True