Execute code located at the specified filesystem location Returns the resulting top level namespace dictionary The file path may refer directly to a Python script (i.e. one that could be directly executed with execfile) or else it may refer to a zipfile or directory containing a to
(path_name, init_globals=None, run_name=None)
| 285 | |
| 286 | |
| 287 | def run_path(path_name, init_globals=None, run_name=None): |
| 288 | """Execute code located at the specified filesystem location |
| 289 | |
| 290 | Returns the resulting top level namespace dictionary |
| 291 | |
| 292 | The file path may refer directly to a Python script (i.e. |
| 293 | one that could be directly executed with execfile) or else |
| 294 | it may refer to a zipfile or directory containing a top |
| 295 | level __main__.py script. |
| 296 | """ |
| 297 | if run_name is None: |
| 298 | run_name = "<run_path>" |
| 299 | pkg_name = run_name.rpartition(".")[0] |
| 300 | importer = pkgutil_get_importer(path_name) |
| 301 | # Trying to avoid importing imp so as to not consume the deprecation warning. |
| 302 | is_NullImporter = False |
| 303 | if type(importer).__module__ == "imp": |
| 304 | if type(importer).__name__ == "NullImporter": |
| 305 | is_NullImporter = True |
| 306 | if isinstance(importer, type(None)) or is_NullImporter: |
| 307 | # Not a valid sys.path entry, so run the code directly |
| 308 | # execfile() doesn't help as we want to allow compiled files |
| 309 | code, fname = _get_code_from_file(run_name, path_name) |
| 310 | return _run_module_code(code, init_globals, run_name, pkg_name=pkg_name, script_name=fname) |
| 311 | else: |
| 312 | # Finder is defined for path, so add it to |
| 313 | # the start of sys.path |
| 314 | sys.path.insert(0, path_name) |
| 315 | try: |
| 316 | # Here's where things are a little different from the run_module |
| 317 | # case. There, we only had to replace the module in sys while the |
| 318 | # code was running and doing so was somewhat optional. Here, we |
| 319 | # have no choice and we have to remove it even while we read the |
| 320 | # code. If we don't do this, a __loader__ attribute in the |
| 321 | # existing __main__ module may prevent location of the new module. |
| 322 | mod_name, mod_spec, code = _get_main_module_details() |
| 323 | with _TempModule(run_name) as temp_module, _ModifiedArgv0(path_name): |
| 324 | mod_globals = temp_module.module.__dict__ |
| 325 | return _run_code(code, mod_globals, init_globals, run_name, mod_spec, pkg_name).copy() |
| 326 | finally: |
| 327 | try: |
| 328 | sys.path.remove(path_name) |
| 329 | except ValueError: |
| 330 | pass |
| 331 | |
| 332 | |
| 333 | if __name__ == "__main__": |
nothing calls this directly
no test coverage detected