New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Windows bug picking up stdin from a pipe #61096
Comments
Hi, This is a bug that seems to exist on python 2.7, python 3.3 on Windows versions XP, Vista, 7 and 8 and has been around for some years, presumably also in other python versions. It is only recently I have managed to better isolate it although not completely. My Windows PC is set up with some doskey macros that are loaded at each python32 testsubproc.py < mytextfile.txt will not work. The textfile mytextfile.txt will be ignored. If subprocess is invoked with shell=False the textfile does get piped into stdin. This is however not an option as our porgrams are distributed on other platforms. It would also give an inconsistent experience on the same OS (Windows). If mymacros.cmd is called interactively from the command prompt the problem does not exist and stdin can be piped into the python script with no problem. I have attached a working script below which includes a comment section with a small C++ program and the registry key in question to demonstrate the problem. I would be grateful if there are people who know how to get to the bottom of this bug or even fix it. Many thanks, Robert |
@robert we're sorry about the delay in getting back to you. Sorry Windows gurus this is over my head :( |
Doskey is a command-line interface for a subset of the Win32 console API. Macros are implemented as console input aliases by calling AddConsoleAlias. http://msdn.microsoft.com/en-us/library/ms681935 You can load macros from a text file that defines aliases for multiple EXEs:
Create this file from your current macros:
You can also use ctypes to add an alias. #!python3
import ctypes
import subprocess
AddConsoleAliasW = ctypes.windll.kernel32.AddConsoleAliasW
AddConsoleAliasW.argtypes = [ctypes.c_wchar_p] * 3
# $db => import builtins;dir(builtins)
AddConsoleAliasW("$db",
"import builtins;dir(builtins)",
"python.exe")
#subprocess.call("doskey")
subprocess.call("stdintest.exe", shell=True) The "bug" occurs if I uncomment the line to run doskey. When stdin is a file, calling sys.stdin.tell() before and after running doskey shows that it's reading standard input, for whatever reason. That's its prerogative, not a bug. Calling sys.stdin.seek(0) after running doskey works, but that's not a possibility when doskey is run via the cmd shell's AutoRun. By the way, redirection to a file is not a pipe. It actually works for a pipe such as
The good news is that you only need to load doskey macros once for a given console window. You can use an AutoRun script that tests and sets an environment variable:
This should take care of cases where you're redirecting standard input to a file, assuming the shell wasn't started with the /d option to skip AutoRun. On Posix systems /bin/sh -c doesn't execute .profile, .bashrc, etc. So maybe on Windows Popen should use %ComSpec% /d /c. You'd still have this problem with os.system, however. |
Eryk's diagnosis sounds like this is not a bug in Python or something that Python needs to or even should fix. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: