(file, globals=None, locals=None, is_module=False)
| 14 | |
| 15 | |
| 16 | def run_file(file, globals=None, locals=None, is_module=False): |
| 17 | module_name = None |
| 18 | entry_point_fn = None |
| 19 | if is_module: |
| 20 | file, _, entry_point_fn = file.partition(":") |
| 21 | module_name = file |
| 22 | filename = get_fullname(file) |
| 23 | if filename is None: |
| 24 | sys.stderr.write("No module named %s\n" % file) |
| 25 | return |
| 26 | else: |
| 27 | file = filename |
| 28 | |
| 29 | if os.path.isdir(file): |
| 30 | new_target = os.path.join(file, "__main__.py") |
| 31 | if os.path.isfile(new_target): |
| 32 | file = new_target |
| 33 | |
| 34 | if globals is None: |
| 35 | m = save_main_module(file, "pydev_run_in_console") |
| 36 | |
| 37 | globals = m.__dict__ |
| 38 | try: |
| 39 | globals["__builtins__"] = __builtins__ |
| 40 | except NameError: |
| 41 | pass # Not there on Jython... |
| 42 | |
| 43 | if locals is None: |
| 44 | locals = globals |
| 45 | |
| 46 | if not is_module: |
| 47 | sys.path.insert(0, os.path.split(file)[0]) |
| 48 | |
| 49 | print("Running %s" % file) |
| 50 | try: |
| 51 | if not is_module: |
| 52 | pydev_imports.execfile(file, globals, locals) # execute the script |
| 53 | else: |
| 54 | # treat ':' as a seperator between module and entry point function |
| 55 | # if there is no entry point we run we same as with -m switch. Otherwise we perform |
| 56 | # an import and execute the entry point |
| 57 | if entry_point_fn: |
| 58 | mod = __import__(module_name, level=0, fromlist=[entry_point_fn], globals=globals, locals=locals) |
| 59 | func = getattr(mod, entry_point_fn) |
| 60 | func() |
| 61 | else: |
| 62 | # Run with the -m switch |
| 63 | from _pydevd_bundle import pydevd_runpy |
| 64 | |
| 65 | pydevd_runpy._run_module_as_main(module_name) |
| 66 | except: |
| 67 | traceback.print_exc() |
| 68 | |
| 69 | return globals |
| 70 | |
| 71 | |
| 72 | def skip_successful_exit(*args): |
no test coverage detected