我们从Python开源项目中,提取了以下7个代码示例,用于说明如何使用code.compile_command()。
def _preprocess(self, source, filename, symbol): self.input_lines = ScriptInputProvider() self.input_lines.pushText(source) lines = source.splitlines() scriptlines = [] codeblock = [] line_no = 0 for l in self.input_lines.iterate(): if len(l) > 0 and l[0] == '!': codeblock.append(l[1:]) else: if len(codeblock) > 0: c = code.compile_command("\n".join(codeblock), filename, 'exec') codeblock = [] if c == None: raise Exception("Incomplete command in block ending at line {}".format(line_no)) super().runcode(c) scriptlines.append(l) line_no += 1 return "\n".join(scriptlines)
def key_Return(self, entry, event): text = self.getText() # Figure out if that Return meant "next line" or "execute." try: c = code.compile_command(text) except SyntaxError, e: # This could conceivably piss you off if the client's python # doesn't accept keywords that are known to the manhole's # python. point = buffer.get_iter_at_line_offset(e.lineno, e.offset) buffer.place(point) # TODO: Componentize! self.toplevel.output.append(str(e), "exception") except (OverflowError, ValueError), e: self.toplevel.output.append(str(e), "exception") else: if c is not None: self.sendMessage() # Don't insert Return as a newline in the buffer. self.history.append(text) self.clear() # entry.emit_stop_by_name("key_press_event") return True else: # not a complete code block return False return False
def run_interpreter(stdin, stdout): globals = {} try: str(sys.ps1) except: sys.ps1 = ">>> " source = "" while 1: stdout.write(sys.ps1) line = stdin.readline() if line[:2] == '\377\354': line = "" if not line and not source: break if line[-2:] == '\r\n': line = line[:-2] + '\n' source = source + line try: code = compile_command(source) except SyntaxError, err: source = "" traceback.print_exception(SyntaxError, err, None, file=stdout) continue if not code: continue source = "" try: run_command(code, stdin, stdout, globals) except SystemExit, how: if how: try: how = str(how) except: how = "" stdout.write("Exit %s\n" % how) break stdout.write("\nGoodbye.\n")
def processKey(self, entry, event): # TODO: make key bindings easier to customize. stopSignal = False # ASSUMPTION: Assume Meta == mod4 isMeta = event.state & gtk.GDK.MOD4_MASK if event.keyval == gtk.GDK.Return: isShift = event.state & gtk.GDK.SHIFT_MASK if isShift: self.linemode = True self.insert_defaults('\n') else: stopSignal = True text = self.get_chars(0,-1) if not text: return try: if text[0] == '/': # It's a local-command, don't evaluate it as # Python. c = True else: # This will tell us it's a complete expression. c = code.compile_command(text) except SyntaxError, e: # Ding! self.set_positionLineOffset(e.lineno, e.offset) print "offset", e.offset errmsg = {'traceback': [], 'exception': [str(e) + '\n']} self.toplevel.output.console([('exception', errmsg)]) except OverflowError, e: e = traceback.format_exception_only(OverflowError, e) errmsg = {'traceback': [], 'exception': e} self.toplevel.output.console([('exception', errmsg)]) else: if c is None: self.linemode = True stopSignal = False else: self.sendMessage(entry) self.clear() elif ((event.keyval == gtk.GDK.Up and isCursorOnFirstLine(self)) or (isMeta and event.string == 'p')): self.historyUp() stopSignal = True elif ((event.keyval == gtk.GDK.Down and isCursorOnLastLine(self)) or (isMeta and event.string == 'n')): self.historyDown() stopSignal = True if stopSignal: self.emit_stop_by_name("key_press_event") return True
def process_input(self): self.input_history += [str(self.input.text())] self.input_history_pos = None input = str(self.input.text()) + "\n" self.input.setText("") self.output.textCursor().movePosition(QTextCursor.End) fmt = QTextCharFormat() fmt.setForeground(QBrush(Qt.black)) if len(self.prompt.text()) > 0: self.output.textCursor().insertText(self.prompt.text() + " " + input, fmt) else: self.output.textCursor().insertText(input, fmt) self.output.ensureCursorVisible() if self.input_requested: # Request for data from stdin self.input_requested = False self.input.setEnabled(False) self.input_result = input self.input_event.set() return if self.source is not None: self.source = self.source + input if input != "\n": # Don't end multiline input until a blank line return input = self.source try: result = code.compile_command(input) except: result = False if result is None: if self.source is None: self.source = input else: self.source += input self.prompt.setText("...") return self.source = None self.prompt.setText(">>>") self.thread.code = input self.thread.event.set() self.running = True self.thread.done.wait(0.05) if self.thread.done.is_set(): self.thread.done.clear() self.running = False else: self.input.setEnabled(False)