| 
									
										
										
										
											1999-03-11 13:26:23 +00:00
										 |  |  | #! /usr/bin/env python | 
					
						
							|  |  |  | """Test script for popen2.py
 | 
					
						
							|  |  |  |    Christian Tismer | 
					
						
							|  |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-08-28 17:20:05 +00:00
										 |  |  | import os | 
					
						
							| 
									
										
										
										
											2001-01-30 18:35:32 +00:00
										 |  |  | import sys | 
					
						
							| 
									
										
										
										
											2002-07-23 19:04:11 +00:00
										 |  |  | from test.test_support import TestSkipped | 
					
						
							| 
									
										
										
										
											2000-08-28 17:20:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-03-11 13:26:23 +00:00
										 |  |  | # popen2 contains its own testing routine | 
					
						
							|  |  |  | # which is especially useful to see if open files | 
					
						
							| 
									
										
										
										
											2000-07-27 07:42:43 +00:00
										 |  |  | # like stdin can be read successfully by a forked | 
					
						
							| 
									
										
										
										
											1999-03-11 13:26:23 +00:00
										 |  |  | # subprocess. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def main(): | 
					
						
							| 
									
										
										
										
											2000-08-28 17:20:05 +00:00
										 |  |  |     print "Test popen2 module:" | 
					
						
							| 
									
										
										
										
											2002-06-11 06:22:31 +00:00
										 |  |  |     if (sys.platform[:4] == 'beos' or sys.platform[:6] == 'atheos') \ | 
					
						
							|  |  |  |            and __name__ != '__main__': | 
					
						
							| 
									
										
										
										
											2001-01-30 18:35:32 +00:00
										 |  |  |         #  Locks get messed up or something.  Generally we're supposed | 
					
						
							|  |  |  |         #  to avoid mixing "posix" fork & exec with native threads, and | 
					
						
							|  |  |  |         #  they may be right about that after all. | 
					
						
							| 
									
										
										
										
											2002-06-11 06:22:31 +00:00
										 |  |  |         raise TestSkipped, "popen2() doesn't work during import on " + sys.platform | 
					
						
							| 
									
										
										
										
											2000-07-27 07:42:43 +00:00
										 |  |  |     try: | 
					
						
							|  |  |  |         from os import popen | 
					
						
							|  |  |  |     except ImportError: | 
					
						
							|  |  |  |         # if we don't have os.popen, check that | 
					
						
							|  |  |  |         # we have os.fork.  if not, skip the test | 
					
						
							|  |  |  |         # (by raising an ImportError) | 
					
						
							|  |  |  |         from os import fork | 
					
						
							| 
									
										
										
										
											1999-03-11 13:26:23 +00:00
										 |  |  |     import popen2 | 
					
						
							|  |  |  |     popen2._test() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-08-28 17:20:05 +00:00
										 |  |  | def _test(): | 
					
						
							|  |  |  |     # same test as popen2._test(), but using the os.popen*() API | 
					
						
							|  |  |  |     print "Testing os module:" | 
					
						
							|  |  |  |     import popen2 | 
					
						
							|  |  |  |     cmd  = "cat" | 
					
						
							| 
									
										
										
										
											2000-09-01 20:38:55 +00:00
										 |  |  |     teststr = "ab cd\n" | 
					
						
							| 
									
										
										
										
											2000-08-28 17:20:05 +00:00
										 |  |  |     if os.name == "nt": | 
					
						
							|  |  |  |         cmd = "more" | 
					
						
							| 
									
										
										
										
											2000-09-01 20:38:55 +00:00
										 |  |  |     # "more" doesn't act the same way across Windows flavors, | 
					
						
							|  |  |  |     # sometimes adding an extra newline at the start or the | 
					
						
							|  |  |  |     # end.  So we strip whitespace off both ends for comparison. | 
					
						
							|  |  |  |     expected = teststr.strip() | 
					
						
							| 
									
										
										
										
											2000-08-28 17:20:05 +00:00
										 |  |  |     print "testing popen2..." | 
					
						
							|  |  |  |     w, r = os.popen2(cmd) | 
					
						
							|  |  |  |     w.write(teststr) | 
					
						
							|  |  |  |     w.close() | 
					
						
							| 
									
										
										
										
											2000-09-01 20:38:55 +00:00
										 |  |  |     got = r.read() | 
					
						
							|  |  |  |     if got.strip() != expected: | 
					
						
							| 
									
										
										
										
											2004-02-12 17:35:32 +00:00
										 |  |  |         raise ValueError("wrote %r read %r" % (teststr, got)) | 
					
						
							| 
									
										
										
										
											2000-08-28 17:20:05 +00:00
										 |  |  |     print "testing popen3..." | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         w, r, e = os.popen3([cmd]) | 
					
						
							|  |  |  |     except: | 
					
						
							|  |  |  |         w, r, e = os.popen3(cmd) | 
					
						
							|  |  |  |     w.write(teststr) | 
					
						
							|  |  |  |     w.close() | 
					
						
							| 
									
										
										
										
											2000-09-01 20:38:55 +00:00
										 |  |  |     got = r.read() | 
					
						
							|  |  |  |     if got.strip() != expected: | 
					
						
							| 
									
										
										
										
											2004-02-12 17:35:32 +00:00
										 |  |  |         raise ValueError("wrote %r read %r" % (teststr, got)) | 
					
						
							| 
									
										
										
										
											2000-09-01 20:38:55 +00:00
										 |  |  |     got = e.read() | 
					
						
							|  |  |  |     if got: | 
					
						
							| 
									
										
										
										
											2004-02-12 17:35:32 +00:00
										 |  |  |         raise ValueError("unexected %r on stderr" % (got,)) | 
					
						
							| 
									
										
										
										
											2000-08-28 17:20:05 +00:00
										 |  |  |     for inst in popen2._active[:]: | 
					
						
							|  |  |  |         inst.wait() | 
					
						
							| 
									
										
										
										
											2000-09-01 20:38:55 +00:00
										 |  |  |     if popen2._active: | 
					
						
							|  |  |  |         raise ValueError("_active not empty") | 
					
						
							| 
									
										
										
										
											2000-08-28 17:20:05 +00:00
										 |  |  |     print "All OK" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | main() | 
					
						
							|  |  |  | _test() |