Пример #1
0
    def __init__(self, workspace=None, thread_id=None):
        if workspace is None:
            workspace = dict()

        self.workspace = workspace
        self.compile = CommandCompiler()
        self.thread_id = thread_id
Пример #2
0
 def __init__(self,
              namespace=None,
              io=None,
              ps1="Python> ",
              ps2="..more> ",
              history=None):
     self._ns = namespace or globals()
     self._io = io or ConsoleIO()
     if history:
         self.history = os.path.expandvars(os.path.expanduser(history))
     else:
         self.history = None
     readline.set_history_length(1000)
     if sys.platform == "darwin":
         readline.parse_and_bind("^I rl_complete")
     else:
         readline.parse_and_bind("tab: complete")
     try:
         self.saveps1, self.saveps2 = sys.ps1, sys.ps2
     except AttributeError:
         self.saveps1, self.saveps2 = ">>> ", "... "
     sys.ps1, sys.ps2 = ps1, ps2
     if self.history:
         try:
             readline.read_history_file(self.history)
         except FileNotFoundError:
             pass
     self.compiler = CommandCompiler()
     self._reset()
    def __init__(self, locals=None, encoding=None):
        """Constructor.

        The optional 'locals' argument specifies the dictionary in
        which code will be executed; it defaults to a newly created
        dictionary with key "__name__" set to "__console__" and key
        "__doc__" set to None.

        We include an argument for the outfile to pass to the formatter for it
        to write to.
        """
        if locals is None:
            locals = {"__name__": "__console__", "__doc__": None}
        if encoding is None:
            encoding = getpreferredencoding()
        ReplInterpreter.__init__(self, locals, encoding)

        self.locals = locals
        self.compile = CommandCompiler()

        # typically changed after being instantiated
        # but used when interpreter used corresponding REPL
        def write(err_line):
            """Default stderr handler for tracebacks

            Accepts FmtStrs so interpreters can output them"""
            sys.stderr.write(text_type(err_line))

        self.write = write
        self.outfile = self
Пример #4
0
    def __init__(self, locals, filename="<console>"):

        # Init variables for locals and globals (globals only for debugging)
        self.locals = locals
        self.globals = None

        # Store filename
        self._filename = filename

        # Store ref of locals that is our main
        self._main_locals = locals

        # Flag to ignore sys exit, to allow running some scripts
        # interactively, even if they call sys.exit.
        self.ignore_sys_exit = False

        # Information for debugging. If self._dbFrames, we're in debug mode
        # _dbFrameIndex starts from 1
        self._dbFrames = []
        self._dbFrameIndex = 0
        self._dbFrameName = ""

        # Init datase to store source code that we execute
        self._codeCollection = ExecutedSourceCollection()

        # Init buffer to deal with multi-line command in the shell
        self._buffer = []

        # Init sleep time. 0.001 result in 0% CPU usage at my laptop (Windows),
        # but 8% CPU usage at my older laptop (on Linux).
        self.sleeptime = 0.01  # 100 Hz

        # Create compiler
        if sys.platform.startswith("java"):
            import compiler

            self._compile = compiler.compile  # or 'exec' does not work
        else:
            self._compile = CommandCompiler()

        # Instantiate magician and tracer
        self.magician = Magician()
        self.debugger = Debugger()

        # To keep track of whether to send a new prompt, and whether more
        # code is expected.
        self.more = 0
        self.newPrompt = True

        # Code and script to run on first iteration
        self._codeToRunOnStartup = None
        self._scriptToRunOnStartup = None

        # Remove "THIS" directory from the PYTHONPATH
        # to prevent unwanted imports. Same for pyzokernel dir
        thisPath = os.getcwd()
        for p in [thisPath, os.path.join(thisPath, "pyzokernel")]:
            while p in sys.path:
                sys.path.remove(p)
Пример #5
0
    def __init__(self,
            prompt='>>> ',
            continuation='... ',
            parent=None):
        QTextEdit.__init__(self, parent)
        self.shutting_down = False
        self.compiler = CommandCompiler()
        self.buf = self.old_buf = []
        self.history = History([''], dynamic.get('console_history', []))
        self.prompt_frame = None
        self.allow_output = False
        self.prompt_frame_format = QTextFrameFormat()
        self.prompt_frame_format.setBorder(1)
        self.prompt_frame_format.setBorderStyle(QTextFrameFormat.BorderStyle_Solid)
        self.prompt_len = len(prompt)

        self.doc.setMaximumBlockCount(int(prefs['scrollback']))
        self.lexer = PythonLexer(ensurenl=False)
        self.tb_lexer = PythonTracebackLexer()

        self.context_menu = cm = QMenu(self) # {{{
        cm.theme = ThemeMenu(cm)
        # }}}

        self.formatter = Formatter(prompt, continuation, style=prefs['theme'])
        p = QPalette()
        p.setColor(p.Base, QColor(self.formatter.background_color))
        p.setColor(p.Text, QColor(self.formatter.color))
        self.setPalette(p)

        self.key_dispatcher = { # {{{
                Qt.Key_Enter : self.enter_pressed,
                Qt.Key_Return : self.enter_pressed,
                Qt.Key_Up : self.up_pressed,
                Qt.Key_Down : self.down_pressed,
                Qt.Key_Home : self.home_pressed,
                Qt.Key_End : self.end_pressed,
                Qt.Key_Left : self.left_pressed,
                Qt.Key_Right : self.right_pressed,
                Qt.Key_Backspace : self.backspace_pressed,
                Qt.Key_Delete : self.delete_pressed,
        } # }}}

        motd = textwrap.dedent('''\
        # Python {0}
        # {1} {2}
        '''.format(sys.version.splitlines()[0], __appname__,
            __version__))

        sys.excepthook = self.unhandled_exception

        self.controllers = []
        QTimer.singleShot(0, self.launch_controller)


        with EditBlock(self.cursor):
            self.render_block(motd)
Пример #6
0
    def __init__(self, locals=None):
        """Constructor.

        The optional 'locals' argument specifies the dictionary in
        which code will be executed; it defaults to a newly created
        dictionary with key "__name__" set to "__console__" and key
        "__doc__" set to None.

        """
        if locals is None:
            locals = {"__name__": "__console__", "__doc__": None}
        self.locals = locals
        self.compile = CommandCompiler()
Пример #7
0
    def __init__(self, locals, filename="<console>"):

        # Init variables for locals and globals (globals only for debugging)
        self.locals = locals
        self.globals = None

        # Store filename
        self._filename = filename

        # Store ref of locals that is our main
        self._main_locals = locals

        # Information for debugging. If self._dbFrames, we're in debug mode
        # _dbFrameIndex starts from 1
        self._dbFrames = []
        self._dbFrameIndex = 0
        self._dbFrameName = ''

        # Init datase to store source code that we execute
        self._codeCollection = ExecutedSourceCollection()

        # Init buffer to deal with multi-line command in the shell
        self._buffer = []

        # Init sleep time. 0.001 result in 0% CPU usage at my laptop (Windows),
        # but 8% CPU usage at my older laptop (on Linux).
        self.sleeptime = 0.01  # 100 Hz

        # Create compiler
        self._compile = CommandCompiler()

        # Instantiate magician
        self.magician = Magician()

        # Define prompts
        try:
            sys.ps1
        except AttributeError:
            sys.ps1 = ">>> "
        try:
            sys.ps2
        except AttributeError:
            sys.ps2 = "... "

        # Remove "THIS" directory from the PYTHONPATH
        # to prevent unwanted imports. Same for iepkernel dir
        thisPath = os.getcwd()
        for p in [thisPath, os.path.join(thisPath, 'iepkernel')]:
            while p in sys.path:
                sys.path.remove(p)
Пример #8
0
    def __init__(self, locals=None, username=None):
        """
        The optional 'locals' argument specifies the dictionary in
        which code will be executed; it defaults to a newly created
        dictionary with key "__name__" set to "__console__" and key
        "__doc__" set to None.

        """

        if locals is None:
            locals = {"__name__": "__console__", "__doc__": None}
        self.locals = locals
        self.username = username
        if username is not None:
            self.locals.update(config[username]['symbols'])
            #print _("Hello %s ! "% username)
            print('')
        self.compile = CommandCompiler()
Пример #9
0
    def __init__(self, locals=None):
        """Constructor.

        The optional 'locals' argument specifies the dictionary in
        which code will be executed; it defaults to a newly created
        dictionary with key "__name__" set to "__console__" and key
        "__doc__" set to None.

        We include an argument for the outfile to pass to the formatter for it to write to.

        """
        if locals is None:
            locals = {"__name__": "__console__", "__doc__": None}
        self.locals = locals
        self.compile = CommandCompiler()

        # typically changed after being instantiated
        self.write = lambda stuff: sys.stderr.write(stuff)
        self.outfile = self
Пример #10
0
    def __init__(self, locals=None):
        """Constructor.

        The optional 'locals' argument specifies the dictionary in
        which code will be executed; it defaults to a newly created
        dictionary with key "__name__" set to "__console__" and key
        "__doc__" set to None.

        """
        tlc = {"__name__": "__py4Nezha__", "__doc__": None}
        tlc['__whoami__'] = 'Nezha'
        tlc['__myfriend__'] = 'Aobing'
        tlc['__myparents__'] = ('Yinshiniang', 'Lijing')
        tlc['__father__'] = 'Jiaozi'

        if locals is None:
            locals = tlc
        self.locals = locals
        self.compile = CommandCompiler()
Пример #11
0
 def __init__(self, locals=None):
     if locals is None:
         locals = {'__name__': '__console__', '__doc__': None}
     self.locals = locals
     self.compile = CommandCompiler()
     return
Пример #12
0
 def __init__(self, filename):
     self.filename = filename
     self.compile = CommandCompiler()
Пример #13
0
from io import StringIO
from contextlib import contextmanager
from codeop import CommandCompiler

@contextmanager
def stdout_io(stdout=None):
    old = sys.stdout
    if stdout is None:
        stdout = StringIO()

    sys.stdout = stdout
    yield stdout
    sys.stdout = old

_compile = CommandCompiler()


def multiline_input(single_prompt=">>> ", additional_prompt="... "):
    header = single_prompt
    commands = []

    while True:
        source = input(header)
        commands.append(source)

        try:
            all_commands = "\n".join(commands)
            code = _compile(all_commands, "<dummy>", "single")

        except (OverflowError, SyntaxError, ValueError):
Пример #14
0
 def __init__(self,):
     CommandCompiler.__init__(self)
     self.compiler = MacroCompile()
Пример #15
0
 def __init__(self, locals=None):
     if locals is None:
         locals = {"__name__": "__console__", "__doc__": None}
     self.locals = locals
     self.compile = CommandCompiler()
Пример #16
0
"""Utilities needed to emulate Python's interactive interpreter.
Пример #17
0
 def __init__(self, ):
     CommandCompiler.__init__(self)
     self.compiler = MacroCompile()
Пример #18
0
 def __init__(self, *args, **kwargs):
     CommandCompiler.__init__(self, *args, **kwargs)
     self.compiler = PyrCompile()