| 65 | bpython.embed(self.get_namespace(**options)) |
| 66 | |
| 67 | def python(self, options): |
| 68 | import code |
| 69 | |
| 70 | # Set up a dictionary to serve as the environment for the shell. |
| 71 | imported_objects = self.get_namespace(**options) |
| 72 | |
| 73 | # We want to honor both $PYTHONSTARTUP and .pythonrc.py, so follow |
| 74 | # system conventions and get $PYTHONSTARTUP first then .pythonrc.py. |
| 75 | if not options["no_startup"]: |
| 76 | for pythonrc in OrderedSet( |
| 77 | [os.environ.get("PYTHONSTARTUP"), os.path.expanduser("~/.pythonrc.py")] |
| 78 | ): |
| 79 | if not pythonrc: |
| 80 | continue |
| 81 | if not os.path.isfile(pythonrc): |
| 82 | continue |
| 83 | with open(pythonrc) as handle: |
| 84 | pythonrc_code = handle.read() |
| 85 | # Match the behavior of the cpython shell where an error in |
| 86 | # PYTHONSTARTUP prints an exception and continues. |
| 87 | try: |
| 88 | exec(compile(pythonrc_code, pythonrc, "exec"), imported_objects) |
| 89 | except Exception: |
| 90 | traceback.print_exc() |
| 91 | |
| 92 | # By default, this will set up readline to do tab completion and to |
| 93 | # read and write history to the .python_history file, but this can be |
| 94 | # overridden by $PYTHONSTARTUP or ~/.pythonrc.py. |
| 95 | try: |
| 96 | hook = sys.__interactivehook__ |
| 97 | except AttributeError: |
| 98 | # Match the behavior of the cpython shell where a missing |
| 99 | # sys.__interactivehook__ is ignored. |
| 100 | pass |
| 101 | else: |
| 102 | try: |
| 103 | hook() |
| 104 | except Exception: |
| 105 | # Match the behavior of the cpython shell where an error in |
| 106 | # sys.__interactivehook__ prints a warning and the exception |
| 107 | # and continues. |
| 108 | print("Failed calling sys.__interactivehook__") |
| 109 | traceback.print_exc() |
| 110 | |
| 111 | # Set up tab completion for objects imported by $PYTHONSTARTUP or |
| 112 | # ~/.pythonrc.py. |
| 113 | try: |
| 114 | import readline |
| 115 | import rlcompleter |
| 116 | |
| 117 | readline.set_completer(rlcompleter.Completer(imported_objects).complete) |
| 118 | except ImportError: |
| 119 | pass |
| 120 | |
| 121 | # Start the interactive interpreter. |
| 122 | code.interact(local=imported_objects) |
| 123 | |
| 124 | def get_auto_imports(self): |