subprocess.Popen() 和 Shell=True 导致错误“命令的语法不正确”

发布于 2024-09-13 02:37:26 字数 1415 浏览 5 评论 0原文

我似乎在 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=TrueShell=TrueFalse 它按预期工作。这台特定的机器对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文