subprocess.Popen() 和 Shell=True 导致错误“命令的语法不正确”
我似乎在 Windows XP x64 机器上发现了 subprocess.Popen()
的问题。如果使用 Shell=True
调用 Popen()
,而不是创建子进程,
The syntax of the command is incorrect
则会将错误打印到命令提示符。
尝试在命令行上使用 Mercurial 时发现了该问题。 'hgsolve' 会因上述错误而崩溃。我已将其范围缩小到 Popen() 的问题,并制作了一个 <10 行脚本来重现该问题。这个问题也存在于 tortoisehg 的多个地方,也是因为它使用 Popen()
和 Shell=True
。
有趣的是,如果我使用 Shell=False 运行测试代码,它可以正常工作(出现预期的行为)。我已经在 Windows x86 版本上测试了代码,并且使用了 Shell=True
和 Shell=True
。 False
它按预期工作。这台特定的机器对 PATH 进行了一些自定义,但没什么严重的。
有谁知道这台 x64 机器有什么不同会导致此错误。在我开始工作之前,机器已经安装/设置,所以我不确定发生了什么变化。到目前为止,我唯一的想法是,由于参数(或相关的东西)的错误转义而发生了一些错误,但是我不确定这种情况可能发生在哪里。
仅供参考,我发布了 Mercurial 对 Popen
的调用之一,以防有人发现可能存在问题的内容
p = subprocess.Popen(cmd, shell=True, bufsize=-1,
close_fds=closefds,
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
universal_newlines=newlines,
env=env)
不幸的是,我现在无法发布测试代码(它正在工作) ,但是我会在拿到手后立即发布更新。
更新,测试代码如下...
import os
import sys
import subprocess
if 'THG_HGTK_SPAWN' in os.environ:
print 'spawn successful'
sys.exit(0)
os.environ['THG_HGTK_SPAWN'] = '1'
subprocess.Popen(args={'python', r'test.py'}, shell=True)
print 'spawning...'
sys.exit(0)
I seem to have discovered a problem with subprocess.Popen()
on a Windows XP x64 machine. If Popen()
is called with Shell=True
, instead of a subprocess being created, the error
The syntax of the command is incorrect
is printed to the command prompt.
The problem was discovered when trying to use Mercurial on the command line. 'hg resolve' would crash with the above error. I've narrowed it down to a problem with Popen(), and have made a <10 line script that reproduces the issue. The problem also exists in tortoisehg in a variety of places, also due to the fact it makes use of Popen()
with Shell=True
.
Interestingly, if I run my test-code with Shell=False, it works fine (expected behaviour occurs). I've tested the code on a Windows x86 build, and with both Shell=True
& False
it works as expected. This specific machine has some customizations to the PATH, however nothing serious.
Does anybody know what could be different about this x64 machine to cause this error. The machine was already installed/setup before I came into the job, so I am unsure what has been changed. The only idea I've had so far is that there's some error occurring due to incorrect escaping of parameters (or something related), however I'm unsure where this could occur.
Just for informational purposes, I've posted one of Mercurial's calls to Popen
, in case anybody spots something which might be the issue
p = subprocess.Popen(cmd, shell=True, bufsize=-1,
close_fds=closefds,
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
universal_newlines=newlines,
env=env)
Unfortunately I can't post the test-code right now (it's at work), however I will post an update the moment I can get my hands on it.
Update, the code for the test is below...
import os
import sys
import subprocess
if 'THG_HGTK_SPAWN' in os.environ:
print 'spawn successful'
sys.exit(0)
os.environ['THG_HGTK_SPAWN'] = '1'
subprocess.Popen(args={'python', r'test.py'}, shell=True)
print 'spawning...'
sys.exit(0)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论