Return the executable. This contains a workaround for Windows if the executable is reported to not have the .exe extension which can cause bugs on reloading.
()
| 222 | |
| 223 | |
| 224 | def get_child_arguments(): |
| 225 | """ |
| 226 | Return the executable. This contains a workaround for Windows if the |
| 227 | executable is reported to not have the .exe extension which can cause bugs |
| 228 | on reloading. |
| 229 | """ |
| 230 | import __main__ |
| 231 | |
| 232 | py_script = Path(sys.argv[0]) |
| 233 | exe_entrypoint = py_script.with_suffix(".exe") |
| 234 | |
| 235 | args = [sys.executable] + ["-W%s" % o for o in sys.warnoptions] |
| 236 | if sys.implementation.name in ("cpython", "pypy"): |
| 237 | args.extend( |
| 238 | f"-X{key}" if value is True else f"-X{key}={value}" |
| 239 | for key, value in sys._xoptions.items() |
| 240 | ) |
| 241 | # __spec__ is set when the server was started with the `-m` option, |
| 242 | # see https://docs.python.org/3/reference/import.html#main-spec |
| 243 | # __spec__ may not exist, e.g. when running in a Conda env. |
| 244 | if getattr(__main__, "__spec__", None) is not None and not exe_entrypoint.exists(): |
| 245 | spec = __main__.__spec__ |
| 246 | if (spec.name == "__main__" or spec.name.endswith(".__main__")) and spec.parent: |
| 247 | name = spec.parent |
| 248 | else: |
| 249 | name = spec.name |
| 250 | args += ["-m", name] |
| 251 | args += sys.argv[1:] |
| 252 | elif not py_script.exists(): |
| 253 | # sys.argv[0] may not exist for several reasons on Windows. |
| 254 | # It may exist with a .exe extension or have a -script.py suffix. |
| 255 | if exe_entrypoint.exists(): |
| 256 | # Should be executed directly, ignoring sys.executable. |
| 257 | return [exe_entrypoint, *sys.argv[1:]] |
| 258 | script_entrypoint = py_script.with_name("%s-script.py" % py_script.name) |
| 259 | if script_entrypoint.exists(): |
| 260 | # Should be executed as usual. |
| 261 | return [*args, script_entrypoint, *sys.argv[1:]] |
| 262 | raise RuntimeError("Script %s does not exist." % py_script) |
| 263 | else: |
| 264 | args += sys.argv |
| 265 | return args |
| 266 | |
| 267 | |
| 268 | def trigger_reload(filename): |
no test coverage detected