python3 shell模式熱加載究極實現方式

之前寫了一個熱加載的python shell模式,但是不能支持多行,於是我就直接去看了python code模塊的源碼,然後扒下來,自己加上文件檢測的函數,就成了一個可以熱加載的python shell模式

創建一個 文件

"""Utilities needed to emulate Python's interactive interpreter.


# Inspired by similar code by Jeff Epler and Fredrik Lundh.

import sys, os, importlib
import traceback
from codeop import CommandCompiler, compile_command

__all__ = ["InteractiveInterpreter", "InteractiveConsole", "interact",

class InteractiveInterpreter:
    """Base class for InteractiveConsole.

    This class deals with parsing and interpreter state (the user's
    namespace); it doesn't deal with input buffering or prompting or
    input file naming (the filename is always passed in explicitly).


    def __init__(self, locals=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.compile = CommandCompiler()

    def runsource(self, source, filename="<input>", symbol="single"):
        """Compile and run some source in the interpreter.

        Arguments are as for compile_command().

        One several things can happen:

        1) The input is incorrect; compile_command() raised an
        exception (SyntaxError or OverflowError).  A syntax traceback
        will be printed by calling the showsyntaxerror() method.

        2) The input is incomplete, and more input is required;
        compile_command() returned None.  Nothing happens.

        3) The input is complete; compile_command() returned a code
        object.  The code is executed by calling self.runcode() (which
        also handles run-time exceptions, except for SystemExit).

        The return value is True in case 2, False in the other cases (unless
        an exception is raised).  The return value can be used to
        decide whether to use sys.ps1 or sys.ps2 to prompt the next

            code = self.compile(source, filename, symbol)
        except (OverflowError, SyntaxError, ValueError):
            # Case 1
            return False

        if code is None:
            # Case 2
            return True

        # Case 3
        return False

    def runcode(self, code):
        """Execute a code object.

        When an exception occurs, self.showtraceback() is called to
        display a traceback.  All exceptions are caught except
        SystemExit, which is reraised.

        A note about KeyboardInterrupt: this exception may occur
        elsewhere in this code, and may not always be caught.  The
        caller should be prepared to deal with it.

            exec(code, self.locals)
        except SystemExit:

    def showsyntaxerror(self, filename=None):
        """Display the syntax error that just occurred.

        This doesn't display a stack trace because there isn't one.

        If a filename is given, it is stuffed in the exception instead
        of what was there before (because Python's parser always uses
        "<string>" when reading from a string).

        The output is written by self.write(), below.

        type, value, tb = sys.exc_info()
        sys.last_type = type
        sys.last_value = value
        sys.last_traceback = tb
        if filename and type is SyntaxError:
            # Work hard to stuff the correct filename in the exception
                msg, (dummy_filename, lineno, offset, line) = value.args
            except ValueError:
                # Not the format we expect; leave it alone
                # Stuff in the right filename
                value = SyntaxError(msg, (filename, lineno, offset, line))
                sys.last_value = value
        if sys.excepthook is sys.__excepthook__:
            lines = traceback.format_exception_only(type, value)
            # If someone has set sys.excepthook, we let that take precedence
            # over self.write
            sys.excepthook(type, value, tb)

    def showtraceback(self):
        """Display the exception that just occurred.

        We remove the first stack item because it is our own code.

        The output is written by self.write(), below.

        sys.last_type, sys.last_value, last_tb = ei = sys.exc_info()
        sys.last_traceback = last_tb
            lines = traceback.format_exception(ei[0], ei[1], last_tb.tb_next)
            if sys.excepthook is sys.__excepthook__:
                # If someone has set sys.excepthook, we let that take precedence
                # over self.write
                sys.excepthook(ei[0], ei[1], last_tb)
            last_tb = ei = None

    def write(self, data):
        """Write a string.

        The base implementation writes to sys.stderr; a subclass may
        replace this with a different implementation.


class InteractiveConsole(InteractiveInterpreter):
    """Closely emulate the behavior of the interactive Python interpreter.

    This class builds on InteractiveInterpreter and adds prompting
    using the familiar sys.ps1 and sys.ps2, and input buffering.


    def __init__(self, locals=None, filename="<console>"):

        The optional locals argument will be passed to the
        InteractiveInterpreter base class.

        The optional filename argument should specify the (file)name
        of the input stream; it will show up in tracebacks.

        InteractiveInterpreter.__init__(self, locals)
        self.filename = filename

    # 自己加的函數
    def iteration_module_files(self):
        # 獲取環境
        for module in sys.modules.values():
            filename = getattr(module, '__file__', None)
            if filename:
                # 減少判斷只檢測本目錄下的模塊
                if not os.getcwd() in filename:
                if filename[-4:] in ('.pyo', '.pyc'):
                    filename = filename[:-1]
                # 將模塊也帶過去,因爲後面需要重載被修改文件的模塊
                yield (filename, module)

    # 自己加的函數
    def is_files_modify(self, mtimes, module_list):
        # 檢測模塊文件是否修改
        for filename, module in self.iteration_module_files():
                # 文件修改時間
                mtime = os.stat(filename).st_mtime
            except IOError:
            # 從dict中根據文件名取出上一次文件修改時間
            old_time = mtimes.get(filename, None)
            # 如果沒有上一次的修改時間,加入dict
            if old_time is None:
                mtimes[filename] = mtime
            # 對比修改時間
            elif mtime > old_time:
                # 將修改了時間的模塊加入列表
                # 如果不把新的時間賦值給文件,那麼會一直進入重載
                mtimes[filename] = mtime
        return module_list

    def resetbuffer(self):
        """Reset the input buffer."""
        self.buffer = []

    def interact(self, banner=None, exitmsg=None):
        """Closely emulate the interactive Python console.

        The optional banner argument specifies the banner to print
        before the first interaction; by default it prints a banner
        similar to the one printed by the real Python interpreter,
        followed by the current class name in parentheses (so as not
        to confuse this with the real interpreter -- since it's so

        The optional exitmsg argument specifies the exit message
        printed when exiting. Pass the empty string to suppress
        printing an exit message. If exitmsg is not given or None,
        a default message is printed.

        except AttributeError:
            sys.ps1 = ">>> "
        except AttributeError:
            sys.ps2 = "... "
        cprt = 'Type "help", "copyright", "credits" or "license" for more information.'
        if banner is None:
            self.write("Python %s on %s\n%s\n(%s)\n" %
                       (sys.version, sys.platform, cprt,
        elif banner:
            self.write("%s\n" % str(banner))
        more = 0
        # mtimes,module_list 自己家的變量
        mtimes = {}
        module_list = set()
        while 1:
            # ------ start ------
            # 自己加的判斷
            self.is_files_modify(mtimes, module_list)
            # 如果有文件修改,就重載修改過的模塊
            if module_list:
                for module in module_list:
                    # 記得清空模塊列表
                    module_list = set()
            # ------ end ----------
                if more:
                    prompt = sys.ps2
                    prompt = sys.ps1
                    line = self.raw_input(prompt)
                except EOFError:
                    more = self.push(line)
            except KeyboardInterrupt:
                more = 0
        if exitmsg is None:
            self.write('now exiting %s...\n' % self.__class__.__name__)
        elif exitmsg != '':
            self.write('%s\n' % exitmsg)

    def push(self, line):
        """Push a line to the interpreter.

        The line should not have a trailing newline; it may have
        internal newlines.  The line is appended to a buffer and the
        interpreter's runsource() method is called with the
        concatenated contents of the buffer as source.  If this
        indicates that the command was executed or invalid, the buffer
        is reset; otherwise, the command is incomplete, and the buffer
        is left as it was after the line was appended.  The return
        value is 1 if more input is required, 0 if the line was dealt
        with in some way (this is the same as runsource()).

        source = "\n".join(self.buffer)
        more = self.runsource(source, self.filename)
        if not more:
        return more

    def raw_input(self, prompt=""):
        """Write a prompt and read a line.

        The returned line does not include the trailing newline.
        When the user enters the EOF key sequence, EOFError is raised.

        The base implementation uses the built-in function
        input(); a subclass may replace this with a different

        return input(prompt)

def interact(banner=None, readfunc=None, local=None, exitmsg=None):
    """Closely emulate the interactive Python interpreter.

    This is a backwards compatible interface to the InteractiveConsole
    class.  When readfunc is not specified, it attempts to import the
    readline module to enable GNU readline if it is available.

    Arguments (all optional, all default to None):

    banner -- passed to InteractiveConsole.interact()
    readfunc -- if not None, replaces InteractiveConsole.raw_input()
    local -- passed to InteractiveInterpreter.__init__()
    exitmsg -- passed to InteractiveConsole.interact()

    console = InteractiveConsole(local)
    if readfunc is not None:
        console.raw_input = readfunc
            import readline
        except ImportError:
    console.interact(banner, exitmsg)

if __name__ == "__main__":
    import argparse

    parser = argparse.ArgumentParser()
    parser.add_argument('-q', action='store_true',
                       help="don't print version and copyright messages")
    args = parser.parse_args()
    if args.q or sys.flags.quiet:
        banner = ''
        banner = None


創建一個 文件

import py_code as code



創建 demo01.py文件

import demo02

class Demo1(object):
    def x1():
        print('this is Demo1.x1')



class Demo2(object):
    def x2():
        print('this is Demo2.x2')



還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.