| 202 | |
| 203 | |
| 204 | def _reload() -> None: |
| 205 | global _reload_attempted |
| 206 | _reload_attempted = True |
| 207 | for fn in _reload_hooks: |
| 208 | fn() |
| 209 | if sys.platform != "win32": |
| 210 | # Clear the alarm signal set by |
| 211 | # ioloop.set_blocking_log_threshold so it doesn't fire |
| 212 | # after the exec. |
| 213 | signal.setitimer(signal.ITIMER_REAL, 0, 0) |
| 214 | # sys.path fixes: see comments at top of file. If __main__.__spec__ |
| 215 | # exists, we were invoked with -m and the effective path is about to |
| 216 | # change on re-exec. Reconstruct the original command line to |
| 217 | # ensure that the new process sees the same path we did. If |
| 218 | # __spec__ is not available (Python < 3.4), check instead if |
| 219 | # sys.path[0] is an empty string and add the current directory to |
| 220 | # $PYTHONPATH. |
| 221 | if _autoreload_is_main: |
| 222 | assert _original_argv is not None |
| 223 | spec = _original_spec |
| 224 | argv = _original_argv |
| 225 | else: |
| 226 | spec = getattr(sys.modules["__main__"], "__spec__", None) |
| 227 | argv = sys.argv |
| 228 | if spec: |
| 229 | argv = ["-m", spec.name] + argv[1:] |
| 230 | else: |
| 231 | path_prefix = "." + os.pathsep |
| 232 | if sys.path[0] == "" and not os.environ.get("PYTHONPATH", "").startswith( |
| 233 | path_prefix |
| 234 | ): |
| 235 | os.environ["PYTHONPATH"] = path_prefix + os.environ.get("PYTHONPATH", "") |
| 236 | if not _has_execv: |
| 237 | subprocess.Popen([sys.executable] + argv) |
| 238 | os._exit(0) |
| 239 | else: |
| 240 | try: |
| 241 | os.execv(sys.executable, [sys.executable] + argv) |
| 242 | except OSError: |
| 243 | # Mac OS X versions prior to 10.6 do not support execv in |
| 244 | # a process that contains multiple threads. Instead of |
| 245 | # re-executing in the current process, start a new one |
| 246 | # and cause the current process to exit. This isn't |
| 247 | # ideal since the new process is detached from the parent |
| 248 | # terminal and thus cannot easily be killed with ctrl-C, |
| 249 | # but it's better than not being able to autoreload at |
| 250 | # all. |
| 251 | # Unfortunately the errno returned in this case does not |
| 252 | # appear to be consistent, so we can't easily check for |
| 253 | # this error specifically. |
| 254 | os.spawnv( |
| 255 | os.P_NOWAIT, sys.executable, [sys.executable] + argv # type: ignore |
| 256 | ) |
| 257 | # At this point the IOLoop has been closed and finally |
| 258 | # blocks will experience errors if we allow the stack to |
| 259 | # unwind, so just exit uncleanly. |
| 260 | os._exit(0) |
| 261 | |