debugshell: allow TortoiseHg builds to exit with the usual `quit()` command
authorMatt Harbison <matt_harbison@yahoo.com>
Fri, 06 Jan 2023 11:38:13 -0500
changeset 49896 88b81dc2d82b
parent 49895 07792fd1837f
child 49897 a78dfb1ad60e
debugshell: allow TortoiseHg builds to exit with the usual `quit()` command I've long been annoyed that `quit()` only randomly worked to exit the interpreter. When that happens, Ctrl+C doesn't work either (it simply prints "KeyboardInterrupt"), so then you have to `import sys` and `sys.exit()`. But it turns out that the behavior isn't random and it depended on which `hg.exe` was picked up on PATH first, because py2exe disables site initialization. I wasn't able to persuade the maintainer to allow an opt-in to initialization[1], but this works around it so that the behavior is now consistent however `hg.exe` is built. TortoiseHg 6.3.3 will be the first build that includes the site package, so handle the ImportError. [1] https://github.com/py2exe/py2exe/issues/154
mercurial/debugcommands.py
--- a/mercurial/debugcommands.py	Wed Jan 11 19:53:58 2023 +0000
+++ b/mercurial/debugcommands.py	Fri Jan 06 11:38:13 2023 -0500
@@ -3800,6 +3800,21 @@
         'repo': repo,
     }
 
+    # py2exe disables initialization of the site module, which is responsible
+    # for arranging for ``quit()`` to exit the interpreter.  Manually initialize
+    # the stuff that site normally does here, so that the interpreter can be
+    # quit in a consistent manner, whether run with pyoxidizer, exewrapper.c,
+    # py.exe, or py2exe.
+    if getattr(sys, "frozen", None) == 'console_exe':
+        try:
+            import site
+
+            site.setcopyright()
+            site.sethelper()
+            site.setquit()
+        except ImportError:
+            site = None  # Keep PyCharm happy
+
     code.interact(local=imported_objects)