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

Skip to content

Commit 11bd119

Browse files
committed
SF bug #887946, segfault if redirecting directory
Also provide a warning if a directory is passed on the command line. Add minimal command line test. Will backport.
1 parent d157b1d commit 11bd119

4 files changed

Lines changed: 70 additions & 0 deletions

File tree

Lib/test/test_cmd_line.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2+
import test.test_support, unittest
3+
import sys
4+
import popen2
5+
6+
class CmdLineTest(unittest.TestCase):
7+
def start_python(self, cmd_line):
8+
outfp, infp = popen2.popen4('%s %s' % (sys.executable, cmd_line))
9+
infp.close()
10+
data = outfp.read()
11+
outfp.close()
12+
return data
13+
14+
def test_directories(self):
15+
self.assertTrue('is a directory' in self.start_python('.'))
16+
self.assertTrue('is a directory' in self.start_python('< .'))
17+
18+
def verify_valid_flag(self, cmd_line):
19+
data = self.start_python(cmd_line)
20+
self.assertTrue(data.endswith('\n'))
21+
self.assertTrue('Traceback' not in data)
22+
23+
def test_environment(self):
24+
self.verify_valid_flag('-E')
25+
26+
def test_optimize(self):
27+
self.verify_valid_flag('-O')
28+
self.verify_valid_flag('-OO')
29+
30+
def test_q(self):
31+
self.verify_valid_flag('-Qold')
32+
self.verify_valid_flag('-Qnew')
33+
self.verify_valid_flag('-Qwarn')
34+
self.verify_valid_flag('-Qwarnall')
35+
36+
def test_site_flag(self):
37+
self.verify_valid_flag('-S')
38+
39+
def test_usage(self):
40+
self.assertTrue('usage' in self.start_python('-h'))
41+
42+
def test_version(self):
43+
version = 'Python %d.%d' % sys.version_info[:2]
44+
self.assertTrue(self.start_python('-V').startswith(version))
45+
46+
def test_main():
47+
test.test_support.run_unittest(CmdLineTest)
48+
49+
if __name__ == "__main__":
50+
test_main()

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ What's New in Python 2.5 alpha 1?
1212
Core and builtins
1313
-----------------
1414

15+
- SF Bug #887946: fix segfault when redirecting stdin from a directory.
16+
Provide a warning when a directory is passed on the command line.
17+
1518
- Fix segfault with invalid coding.
1619

1720
- SF bug #772896: unknown encoding results in MemoryError.

Modules/main.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,14 @@ Py_Main(int argc, char **argv)
359359
}
360360
}
361361
}
362+
{
363+
/* XXX: does this work on Win/Win64? (see posix_fstat) */
364+
struct stat sb;
365+
if (fstat(fileno(fp), &sb) == 0 &&
366+
S_ISDIR(sb.st_mode)) {
367+
fprintf(stderr, "%s: warning '%s' is a directory\n", argv[0], filename);
368+
}
369+
}
362370
}
363371
}
364372

Python/sysmodule.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,15 @@ _PySys_Init(void)
947947
m = Py_InitModule3("sys", sys_methods, sys_doc);
948948
sysdict = PyModule_GetDict(m);
949949

950+
{
951+
/* XXX: does this work on Win/Win64? (see posix_fstat) */
952+
struct stat sb;
953+
if (fstat(fileno(stdin), &sb) == 0 &&
954+
S_ISDIR(sb.st_mode)) {
955+
Py_FatalError("<stdin> is a directory");
956+
}
957+
}
958+
950959
/* Closing the standard FILE* if sys.std* goes aways causes problems
951960
* for embedded Python usages. Closing them when somebody explicitly
952961
* invokes .close() might be possible, but the FAQ promises they get

0 commit comments

Comments
 (0)