(user_ns=None)
| 8 | |
| 9 | |
| 10 | def run(user_ns=None): |
| 11 | a = create_parser().parse_args() |
| 12 | |
| 13 | config_file, history_file = get_config_and_history_file(a) |
| 14 | |
| 15 | # If IPython is not available, show message and exit here with error status |
| 16 | # code. |
| 17 | try: |
| 18 | import IPython |
| 19 | except ImportError: |
| 20 | print("IPython not found. Please install IPython (pip install ipython).") |
| 21 | sys.exit(1) |
| 22 | else: |
| 23 | from ptpython.ipython import embed |
| 24 | from ptpython.repl import enable_deprecation_warnings, run_config |
| 25 | |
| 26 | # Add the current directory to `sys.path`. |
| 27 | if sys.path[0] != "": |
| 28 | sys.path.insert(0, "") |
| 29 | |
| 30 | # When a file has been given, run that, otherwise start the shell. |
| 31 | if a.args and not a.interactive: |
| 32 | sys.argv = a.args |
| 33 | path = a.args[0] |
| 34 | with open(path, "rb") as f: |
| 35 | code = compile(f.read(), path, "exec") |
| 36 | exec(code, {"__name__": "__main__", "__file__": path}) |
| 37 | else: |
| 38 | enable_deprecation_warnings() |
| 39 | |
| 40 | # Create an empty namespace for this interactive shell. (If we don't do |
| 41 | # that, all the variables from this function will become available in |
| 42 | # the IPython shell.) |
| 43 | if user_ns is None: |
| 44 | user_ns = {} |
| 45 | |
| 46 | # Startup path |
| 47 | startup_paths = [] |
| 48 | if "PYTHONSTARTUP" in os.environ: |
| 49 | startup_paths.append(os.environ["PYTHONSTARTUP"]) |
| 50 | |
| 51 | # --interactive |
| 52 | if a.interactive: |
| 53 | startup_paths.append(a.args[0]) |
| 54 | sys.argv = a.args |
| 55 | |
| 56 | # exec scripts from startup paths |
| 57 | for path in startup_paths: |
| 58 | if os.path.exists(path): |
| 59 | with open(path, "rb") as f: |
| 60 | code = compile(f.read(), path, "exec") |
| 61 | exec(code, user_ns, user_ns) |
| 62 | else: |
| 63 | print(f"File not found: {path}\n\n") |
| 64 | sys.exit(1) |
| 65 | |
| 66 | # Apply config file |
| 67 | def configure(repl): |
no test coverage detected