While working on the “run” function on PyEM (similar to
pipenv run and
poetry run) I accidentally found that the command history on Windows sometimes does not work.
Here’s a minimal reproducible example:
>>> import os, subprocess, sys >>> sys.executable # A Python 3.8 venv created for testing. 'C:\\Users\\uranusjr\\Downloads\\foo\\Scripts\\python.exe' >>> os.getpid() 6872 >>> # ["os.getpid()" if you press up here] >>> subprocess.run([os.path.join(sys.base_prefix, 'python.exe'), '-q']) >>> import os # Now we are in the inner interpreter. >>> os.getpid() 13304 >>> # [Can still get "os.getpid()" by pressing up] >>> ^Z CompletedProcess(args=['C:\\Users\\uranusjr\\AppData\\Local\\Programs\\Python\\Python38\\python.exe', '-q'], returncode=0) >>> # Now we're back to the outer interpreter >>> subprocess.run([sys.executable, '-q']) >>> import os # New inner interpreter. >>> os.getpwd() 3020 >>> # [Now pressing up does nothing!]
I tried some combinations, with the following observations:
- This happens regardless of the outer interpreter. The outer interpreter is a venvlauncher in the example, but I can reproduce with a base interpreter or venv befoew bpo-34977 implementation as well.
- This only happens when the inner interpreter (the one run via subprocess) is a redirector. Both py.exe or venvlauncher has the same problem. An actual interpreter run via subprocess can still get history correctly.
I am unfamiliar with either how the command history is set up on Windows (the documentation is scarce), or how the redirector forwards calls using Windows API. Is the redirector not correctly forwarding inputs? (But left and right arrow keys work fine.) Is this due to how history is set up that the intermediate redirect confuses it? I am honestly quite stumped by this.