Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e7fefbf

Browse files
committed
Fix bugs:
457466: popenx() argument mangling hangs python 226766: popen('python -c"...."') tends to hang Fixes argument quoting in w9xpopen.exe for Windows 9x. w9xpopen.exe also never attempts to display a MessageBox when not executed interactively. Added test_popen() test. This test currently just executes "python -c ..." as a child process, and checks that the expected arguments were all recieved correctly by the child process. This test succeeds for me on Win9x, win2k and Linux, and I hope it does for other popen supported platforms too :)
1 parent b0aaec5 commit e7fefbf

4 files changed

Lines changed: 98 additions & 5 deletions

File tree

Lib/test/output/test_popen

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
test_popen
2+
Test popen:
3+
popen seemed to process the command-line correctly

Lib/test/test_popen.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#! /usr/bin/env python
2+
"""Basic tests for os.popen()
3+
4+
Particularly useful for platforms that fake popen.
5+
"""
6+
7+
import os
8+
import sys
9+
from test_support import TestSkipped
10+
from os import popen
11+
12+
# Test that command-lines get down as we expect.
13+
# To do this we execute:
14+
# python -c "import sys;print sys.argv" {rest_of_commandline}
15+
# This results in Python being spawned and printing the sys.argv list.
16+
# We can then eval() the result of this, and see what each argv was.
17+
def _do_test_commandline(cmdline, expected):
18+
cmd = 'python -c "import sys;print sys.argv" %s' % (cmdline,)
19+
data = popen(cmd).read()
20+
got = eval(data)[1:] # strip off argv[0]
21+
if got != expected:
22+
print "Error in popen commandline handling."
23+
print " executed '%s', expected '%r', but got '%r'" \
24+
% (cmdline, expected, got)
25+
26+
def _test_commandline():
27+
_do_test_commandline("foo bar", ["foo", "bar"])
28+
_do_test_commandline('foo "spam and eggs" "silly walk"', ["foo", "spam and eggs", "silly walk"])
29+
_do_test_commandline('foo "a \\"quoted\\" arg" bar', ["foo", 'a "quoted" arg', "bar"])
30+
print "popen seemed to process the command-line correctly"
31+
32+
def main():
33+
print "Test popen:"
34+
_test_commandline()
35+
36+
main()

Modules/posixmodule.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3285,9 +3285,15 @@ _PyPopenCreateProcess(char *cmdstring,
32853285

32863286
s2 = (char *)_alloca(x);
32873287
ZeroMemory(s2, x);
3288+
/* To maintain correct argument passing semantics,
3289+
we pass the command-line as it stands, and allow
3290+
quoting to be applied. w9xpopen.exe will then
3291+
use its argv vector, and re-quote the necessary
3292+
args for the ultimate child process.
3293+
*/
32883294
PyOS_snprintf(
32893295
s2, x,
3290-
"%s \"%s%s%s\"",
3296+
"\"%s\" %s%s%s",
32913297
modulepath,
32923298
s1,
32933299
s3,

PC/w9xpopen.c

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616

1717
#define WINDOWS_LEAN_AND_MEAN
1818
#include <windows.h>
19+
#include <stdio.h>
1920

2021
const char *usage =
21-
"This program is used by Python's os.pipe function to\n"
22+
"This program is used by Python's os.popen function to\n"
2223
"to work around a limitation in Windows 95/98. It is\n"
2324
"not designed to be used as stand-alone program.";
2425

@@ -28,11 +29,56 @@ int main(int argc, char *argv[])
2829
STARTUPINFO si;
2930
PROCESS_INFORMATION pi;
3031
DWORD exit_code=0;
32+
int cmdlen = 0;
33+
int i;
34+
char *cmdline, *cmdlinefill;
3135

32-
if (argc != 2) {
33-
MessageBox(NULL, usage, argv[0], MB_OK);
36+
if (argc < 2) {
37+
if (GetFileType(GetStdHandle(STD_INPUT_HANDLE))==FILE_TYPE_CHAR)
38+
/* Attached to a console, and therefore not executed by Python
39+
Display a message box for the inquisitive user
40+
*/
41+
MessageBox(NULL, usage, argv[0], MB_OK);
42+
else {
43+
/* Eeek - executed by Python, but args are screwed!
44+
Write an error message to stdout so there is at
45+
least some clue for the end user when it appears
46+
in their output.
47+
A message box would be hidden and blocks the app.
48+
*/
49+
fprintf(stdout, "Internal popen error - no args specified\n%s\n", usage);
50+
}
3451
return 1;
3552
}
53+
/* Build up the command-line from the args.
54+
Args with a space are quoted, existing quotes are escaped.
55+
To keep things simple calculating the buffer size, we assume
56+
every character is a quote - ie, we allocate double what we need
57+
in the worst case. As this is only double the command line passed
58+
to us, there is a good chance this is reasonably small, so the total
59+
allocation will almost always be < 512 bytes.
60+
*/
61+
for (i=1;i<argc;i++)
62+
cmdlen += strlen(argv[i])*2 + 3; /* one space, maybe 2 quotes */
63+
cmdline = cmdlinefill = (char *)malloc(cmdlen+1);
64+
if (cmdline == NULL)
65+
return -1;
66+
for (i=1;i<argc;i++) {
67+
const char *arglook;
68+
int bQuote = strchr(argv[i], ' ') != NULL;
69+
if (bQuote)
70+
*cmdlinefill++ = '"';
71+
/* escape quotes */
72+
for (arglook=argv[i];*arglook;arglook++) {
73+
if (*arglook=='"')
74+
*cmdlinefill++ = '\\';
75+
*cmdlinefill++ = *arglook;
76+
}
77+
if (bQuote)
78+
*cmdlinefill++ = '"';
79+
*cmdlinefill++ = ' ';
80+
}
81+
*cmdlinefill = '\0';
3682

3783
/* Make child process use this app's standard files. */
3884
ZeroMemory(&si, sizeof si);
@@ -43,13 +89,15 @@ int main(int argc, char *argv[])
4389
si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
4490

4591
bRet = CreateProcess(
46-
NULL, argv[1],
92+
NULL, cmdline,
4793
NULL, NULL,
4894
TRUE, 0,
4995
NULL, NULL,
5096
&si, &pi
5197
);
5298

99+
free(cmdline);
100+
53101
if (bRet) {
54102
if (WaitForSingleObject(pi.hProcess, INFINITE) != WAIT_FAILED) {
55103
GetExitCodeProcess(pi.hProcess, &exit_code);

0 commit comments

Comments
 (0)