PyXR

c:\python24\lib \ test \ test_popen.py



0001 #! /usr/bin/env python
0002 """Basic tests for os.popen()
0003 
0004   Particularly useful for platforms that fake popen.
0005 """
0006 
0007 import os
0008 import sys
0009 from test.test_support import TestSkipped
0010 from os import popen
0011 
0012 # Test that command-lines get down as we expect.
0013 # To do this we execute:
0014 #    python -c "import sys;print sys.argv" {rest_of_commandline}
0015 # This results in Python being spawned and printing the sys.argv list.
0016 # We can then eval() the result of this, and see what each argv was.
0017 python = sys.executable
0018 if ' ' in python:
0019     python = '"' + python + '"'     # quote embedded space for cmdline
0020 def _do_test_commandline(cmdline, expected):
0021     cmd = '%s -c "import sys;print sys.argv" %s' % (python, cmdline)
0022     data = popen(cmd).read()
0023     got = eval(data)[1:] # strip off argv[0]
0024     if got != expected:
0025         print "Error in popen commandline handling."
0026         print " executed '%s', expected '%r', but got '%r'" \
0027                                                     % (cmdline, expected, got)
0028 
0029 def _test_commandline():
0030     _do_test_commandline("foo bar", ["foo", "bar"])
0031     _do_test_commandline('foo "spam and eggs" "silly walk"', ["foo", "spam and eggs", "silly walk"])
0032     _do_test_commandline('foo "a \\"quoted\\" arg" bar', ["foo", 'a "quoted" arg', "bar"])
0033     print "popen seemed to process the command-line correctly"
0034 
0035 def main():
0036     print "Test popen:"
0037     _test_commandline()
0038 
0039 main()
0040 

Generated by PyXR 0.9.4
SourceForge.net Logo