# HG changeset patch # User Matt Harbison # Date 1673023093 18000 # Node ID 88b81dc2d82be9bebad1eefe2147af3f166fc511 # Parent 07792fd1837fd32f8cee8d835c92cf7b39f57e85 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 diff -r 07792fd1837f -r 88b81dc2d82b 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)