()
| 163 | |
| 164 | |
| 165 | def run() -> None: |
| 166 | a = create_parser().parse_args() |
| 167 | |
| 168 | config_file, history_file = get_config_and_history_file(a) |
| 169 | |
| 170 | # Startup path |
| 171 | startup_paths = [] |
| 172 | if "PYTHONSTARTUP" in os.environ: |
| 173 | startup_paths.append(os.environ["PYTHONSTARTUP"]) |
| 174 | |
| 175 | # --interactive |
| 176 | if a.interactive and a.args: |
| 177 | # Note that we shouldn't run PYTHONSTARTUP when -i is given. |
| 178 | startup_paths = [a.args[0]] |
| 179 | sys.argv = a.args |
| 180 | |
| 181 | # Add the current directory to `sys.path`. |
| 182 | if sys.path[0] != "": |
| 183 | sys.path.insert(0, "") |
| 184 | |
| 185 | # When a file has been given, run that, otherwise start the shell. |
| 186 | if a.args and not a.interactive: |
| 187 | sys.argv = a.args |
| 188 | path = a.args[0] |
| 189 | with open(path, "rb") as f: |
| 190 | code = compile(f.read(), path, "exec") |
| 191 | # NOTE: We have to pass a dict as namespace. Omitting this argument |
| 192 | # causes imports to not be found. See issue #326. |
| 193 | # However, an empty dict sets __name__ to 'builtins', which |
| 194 | # breaks `if __name__ == '__main__'` checks. See issue #444. |
| 195 | exec(code, {"__name__": "__main__", "__file__": path}) |
| 196 | |
| 197 | # Run interactive shell. |
| 198 | else: |
| 199 | enable_deprecation_warnings() |
| 200 | |
| 201 | # Apply config file |
| 202 | def configure(repl: PythonRepl) -> None: |
| 203 | if os.path.exists(config_file): |
| 204 | run_config(repl, config_file) |
| 205 | |
| 206 | # Adjust colors if dark/light background flag has been given. |
| 207 | if a.light_bg: |
| 208 | repl.min_brightness = 0.0 |
| 209 | repl.max_brightness = 0.60 |
| 210 | elif a.dark_bg: |
| 211 | repl.min_brightness = 0.60 |
| 212 | repl.max_brightness = 1.0 |
| 213 | |
| 214 | import __main__ |
| 215 | |
| 216 | embed_result = embed( # type: ignore |
| 217 | vi_mode=a.vi, |
| 218 | history_filename=history_file, |
| 219 | configure=configure, |
| 220 | locals=__main__.__dict__, |
| 221 | globals=__main__.__dict__, |
| 222 | startup_paths=startup_paths, |
no test coverage detected