# HG changeset patch # User Matt Harbison # Date 2023-01-06 16:38:13 # 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 --git a/mercurial/debugcommands.py b/mercurial/debugcommands.py --- a/mercurial/debugcommands.py +++ b/mercurial/debugcommands.py @@ -3800,6 +3800,21 @@ def debugshell(ui, repo): '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)