DebugClients/Python3/coverage/execfile.py

Tue, 17 Jun 2014 21:09:24 +0200

author
T.Rzepka <Tobias.Rzepka@gmail.com>
date
Tue, 17 Jun 2014 21:09:24 +0200
branch
5_4_x
changeset 3641
8cfe4af1af36
parent 29
391dc0bc4ae5
child 3495
fac17a82b431
permissions
-rw-r--r--

Fixed stop at sys.breakpoint() in run mode under Python3 and on first run (Py2 and 3).
(grafted from 2bf828881e86be11e4996264a0e580390ac0cc45)

"""Execute files of Python code."""

import imp, os, sys

from .backward import exec_function
from .misc import NoSource


try:
    # In Py 2.x, the builtins were in __builtin__
    BUILTINS = sys.modules['__builtin__']
except KeyError:
    # In Py 3.x, they're in builtins
    BUILTINS = sys.modules['builtins']


def run_python_file(filename, args):
    """Run a python file as if it were the main program on the command line.

    `filename` is the path to the file to execute, it need not be a .py file.
    `args` is the argument array to present as sys.argv, including the first
    element representing the file being executed.

    """
    # Create a module to serve as __main__
    old_main_mod = sys.modules['__main__']
    main_mod = imp.new_module('__main__')
    sys.modules['__main__'] = main_mod
    main_mod.__file__ = filename
    main_mod.__builtins__ = BUILTINS

    # Set sys.argv and the first path element properly.
    old_argv = sys.argv
    old_path0 = sys.path[0]
    sys.argv = args
    sys.path[0] = os.path.dirname(filename)

    try:
        try:
            source = open(filename, 'rU').read()
        except IOError:
            raise NoSource("No file to run: %r" % filename)
        exec_function(source, filename, main_mod.__dict__)
    finally:
        # Restore the old __main__
        sys.modules['__main__'] = old_main_mod

        # Restore the old argv and path
        sys.argv = old_argv
        sys.path[0] = old_path0

eric ide

mercurial