0

I just downloaded the latest Git Bash (2.36.1 64-bit) and installed Python 10. I'm running Windows 11. Among a couple of other unexpected changes from my earlier setup using a previous version of Git Bash and mostly running Python 3.9 (i.e. I now have to run python -i for the interactive python interpreter, for all 2.x and 3.x versions, instead of just running python), the most frustrating is that ctrl+z followed by Enter no longer quits from the interpreter. Instead I must call exit(). Ex:

user@User MINGW64 ~
$ python -i
Python 3.10.4 (tags/v3.10.4:9d38120, Mar 23 2022, 23:13:41) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> # ctrl+z, Enter pressed here
  File "<stdin>", line 1

    ^
SyntaxError: invalid syntax
>>> exit()

user@User MINGW64 ~
$

The same is true for my other installations of python 3 (3.7, 3.8, 3.9), but python 2.7 still has the expected behavior:

user@User MINGW64 ~
$ $PYTHON\\Python27\\python -i
Python 2.7.18 (v2.7.18:8d21aa21f2, Apr 20 2020, 13:25:05) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> # ctrl+z, Enter pressed here


user@User MINGW64 ~
$

What I've tried

I've done quite a bit of research but can't figure out what may have changed. In the earlier version of Git Bash, I remember that ^Z, ^X, ^C, and ^V characters (and maybe more) would display explicitly in the console when typed. Curiously, pressing ctrl+c followed by Enter comes with the error message below. Did some sort of character encoding change in the newer versions of Git Bash?

user@User MINGW64 ~
$ python
Python 3.10.4 (tags/v3.10.4:9d38120, Mar 23 2022, 23:13:41) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> # ctrl+c, Enter pressed here
Traceback (most recent call last):
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\encodings\cp1252.py", line 14, in decode
    def decode(self,input,errors='strict'):
KeyboardInterrupt

The above exception was the direct cause of the following exception:

KeyboardInterrupt: decoding with 'cp1252' codec failed (KeyboardInterrupt: )
>>>

I am too lazy to constantly type exit(). How can I restore the functionality where pressing ctrl+z, then Enter quits the interpreter for my python 3 versions?

Jackson H
  • 171
  • 10
  • Try `Ctrl-D` instead. – metatoaster Jun 08 '22 at 01:07
  • @metatoaster I found that in the Python docs too, same behavior. Still doesn't recognize it as a character the same way it used to. – Jackson H Jun 08 '22 at 01:22
  • Try using Python under WSL (Windows Subsystem for Linux) - Windows can be a bit of a pain when it comes to CLI usage. – metatoaster Jun 08 '22 at 02:02
  • @metatoaster Do you mean change the python executable being used? Or just not use Git Bash? If it's the latter that sounds like a workaround and not what I'm hoping for. I like Git Bash for other reasons as well. – Jackson H Jun 08 '22 at 02:20
  • Or try [ctrl-break](https://stackoverflow.com/questions/1364173/stopping-python-using-ctrlc) instead. – metatoaster Jun 08 '22 at 02:30
  • @metatoaster Doesn't work either – Jackson H Jun 13 '22 at 13:44
  • 1
    You may wish to [report that as an issue](https://github.com/git-for-windows/git/issues) at the git-for-windows repo as they provide Git-bash. Alternatively, just downgrade back to the previous version that had your expected behavior. Probably you may need to downgrade to Python 3.9 in your current setup to confirm that they indeed have changed the ctrl-z behavior. Seems like [this was a past issue](https://github.com/git-for-windows/git/issues/2216) once upon a time. – metatoaster Jun 13 '22 at 22:40
  • Will do, thanks for your help and research! – Jackson H Jun 14 '22 at 15:45

1 Answers1

0

After stumbling upon this answer, I've found a solution that restores the functionality I wanted. Instead of:

alias python3='.../Python310/python

in which case you must get to the interactive interpreter using python3 -i, and ctrl+Z keystrokes are not recognized, use:

alias python3='winpty .../Python310/python

If it's helpful to anyone, here's how I've set up my ~/.bash_aliases:

alias python2='winpty C:\Users\<user>\AppData\Local\Programs\Python/Python27/python'
alias python3.7='winpty C:\Users\<user>\AppData\Local\Programs\Python/Python37/python'
alias python3.8='winpty C:\Users\<user>\AppData\Local\Programs\Python/Python38-32/python'
alias python3.9='winpty C:\Users\<user>\AppData\Local\Programs\Python/Python39/python'
alias python3.10='winpty C:\Users\<user>\AppData\Local\Programs\Python/Python310/python'

alias python3='python3.10'
alias python='python2'

and these lines must be in ~/bashrc:

if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi

Now you can open the python interpreter without requiring -i (just run python), and ctrl+Z, Enter exits it.

Edit: After reading this answer, I reinstalled Git Bash with "Enable experimental support for pseudo consoles" checked and functionality is restored without needing to run Python through winpty.

Jackson H
  • 171
  • 10