diff options
Diffstat (limited to 'coverage')
-rw-r--r-- | coverage/cmdline.py | 13 | ||||
-rw-r--r-- | coverage/execfile.py | 9 |
2 files changed, 13 insertions, 9 deletions
diff --git a/coverage/cmdline.py b/coverage/cmdline.py index cb1d7a3..ac80310 100644 --- a/coverage/cmdline.py +++ b/coverage/cmdline.py @@ -1,6 +1,6 @@ """Command-line support for Coverage.""" -import optparse, sys, traceback +import optparse, os, sys, traceback from coverage.backward import sorted # pylint: disable=W0622 from coverage.execfile import run_python_file, run_python_module @@ -549,15 +549,21 @@ class CoverageScript(object): def do_execute(self, options, args): """Implementation of 'coverage run'.""" + # Set the first path element properly. + old_path0 = sys.path[0] + # Run the script. self.coverage.start() code_ran = True try: try: if options.module: + sys.path[0] = '' self.run_python_module(args[0], args) else: - self.run_python_file(args[0], args) + filename = args[0] + sys.path[0] = os.path.abspath(os.path.dirname(filename)) + self.run_python_file(filename, args) except NoSource: code_ran = False raise @@ -566,6 +572,9 @@ class CoverageScript(object): if code_ran: self.coverage.save() + # Restore the old path + sys.path[0] = old_path0 + def do_debug(self, args): """Implementation of 'coverage debug'.""" diff --git a/coverage/execfile.py b/coverage/execfile.py index 587c2d3..fbb49b2 100644 --- a/coverage/execfile.py +++ b/coverage/execfile.py @@ -65,6 +65,7 @@ def run_python_module(modulename, args): openfile.close() # Finally, hand the file off to run_python_file for execution. + pathname = os.path.abspath(pathname) args[0] = pathname run_python_file(pathname, args, package=packagename) @@ -87,14 +88,9 @@ def run_python_file(filename, args, package=None): main_mod.__package__ = package main_mod.__builtins__ = BUILTINS - # Set sys.argv and the first path element properly. + # Set sys.argv properly. old_argv = sys.argv - old_path0 = sys.path[0] sys.argv = args - if package: - sys.path[0] = '' - else: - sys.path[0] = os.path.abspath(os.path.dirname(filename)) try: # Open the source file. @@ -135,4 +131,3 @@ def run_python_file(filename, args, package=None): # Restore the old argv and path sys.argv = old_argv - sys.path[0] = old_path0 |