@@ -474,44 +474,75 @@ def test_writes_before_communicate(self):
474474 def test_universal_newlines (self ):
475475 p = subprocess .Popen ([sys .executable , "-c" ,
476476 'import sys,os;' + SETBINARY +
477- 'sys.stdout.write("line1 \\ n" );'
477+ 'sys.stdout.write(sys.stdin.readline() );'
478478 'sys.stdout.flush();'
479479 'sys.stdout.write("line2\\ n");'
480480 'sys.stdout.flush();'
481- 'sys.stdout.write("line3 \\ r \\ n" );'
481+ 'sys.stdout.write(sys.stdin.read() );'
482482 'sys.stdout.flush();'
483- 'sys.stdout.write("line4\\ r ");'
483+ 'sys.stdout.write("line4\\ n ");'
484484 'sys.stdout.flush();'
485- 'sys.stdout.write("\\ nline5 ");'
485+ 'sys.stdout.write("line5 \\ r \\ n ");'
486486 'sys.stdout.flush();'
487- 'sys.stdout.write("\\ nline6");' ],
487+ 'sys.stdout.write("line6\\ r");'
488+ 'sys.stdout.flush();'
489+ 'sys.stdout.write("\\ nline7");'
490+ 'sys.stdout.flush();'
491+ 'sys.stdout.write("\\ nline8");' ],
492+ stdin = subprocess .PIPE ,
488493 stdout = subprocess .PIPE ,
489494 universal_newlines = 1 )
495+ p .stdin .write ("line1\n " )
496+ self .assertEqual (p .stdout .readline (), "line1\n " )
497+ p .stdin .write ("line3\n " )
498+ p .stdin .close ()
490499 self .addCleanup (p .stdout .close )
491- stdout = p .stdout .read ()
492- self .assertEqual (stdout , "line1\n line2\n line3\n line4\n line5\n line6" )
500+ self .assertEqual (p .stdout .readline (),
501+ "line2\n " )
502+ self .assertEqual (p .stdout .read (6 ),
503+ "line3\n " )
504+ self .assertEqual (p .stdout .read (),
505+ "line4\n line5\n line6\n line7\n line8" )
493506
494507 def test_universal_newlines_communicate (self ):
495508 # universal newlines through communicate()
496509 p = subprocess .Popen ([sys .executable , "-c" ,
497510 'import sys,os;' + SETBINARY +
498- 'sys.stdout.write("line1\\ n");'
499- 'sys.stdout.flush();'
500511 'sys.stdout.write("line2\\ n");'
501512 'sys.stdout.flush();'
502- 'sys.stdout.write("line3\\ r\\ n");'
513+ 'sys.stdout.write("line4\\ n");'
514+ 'sys.stdout.flush();'
515+ 'sys.stdout.write("line5\\ r\\ n");'
503516 'sys.stdout.flush();'
504- 'sys.stdout.write("line4 \\ r");'
517+ 'sys.stdout.write("line6 \\ r");'
505518 'sys.stdout.flush();'
506- 'sys.stdout.write("\\ nline5 ");'
519+ 'sys.stdout.write("\\ nline7 ");'
507520 'sys.stdout.flush();'
508- 'sys.stdout.write("\\ nline6");' ],
509- stdout = subprocess .PIPE , stderr = subprocess .PIPE ,
521+ 'sys.stdout.write("\\ nline8");' ],
522+ stderr = subprocess .PIPE ,
523+ stdout = subprocess .PIPE ,
510524 universal_newlines = 1 )
511525 self .addCleanup (p .stdout .close )
512526 self .addCleanup (p .stderr .close )
527+ # BUG: can't give a non-empty stdin because it breaks both the
528+ # select- and poll-based communicate() implementations.
513529 (stdout , stderr ) = p .communicate ()
514- self .assertEqual (stdout , "line1\n line2\n line3\n line4\n line5\n line6" )
530+ self .assertEqual (stdout ,
531+ "line2\n line4\n line5\n line6\n line7\n line8" )
532+
533+ def test_universal_newlines_communicate_stdin (self ):
534+ # universal newlines through communicate(), with only stdin
535+ p = subprocess .Popen ([sys .executable , "-c" ,
536+ 'import sys,os;' + SETBINARY + '''\n if True:
537+ s = sys.stdin.readline()
538+ assert s == "line1\\ n", repr(s)
539+ s = sys.stdin.read()
540+ assert s == "line3\\ n", repr(s)
541+ ''' ],
542+ stdin = subprocess .PIPE ,
543+ universal_newlines = 1 )
544+ (stdout , stderr ) = p .communicate ("line1\n line3\n " )
545+ self .assertEqual (p .returncode , 0 )
515546
516547 def test_no_leaking (self ):
517548 # Make sure we leak no resources
@@ -1584,7 +1615,8 @@ def test_main():
15841615 ProcessTestCaseNoPoll ,
15851616 HelperFunctionTests ,
15861617 CommandsWithSpaces ,
1587- ContextManagerTests )
1618+ ContextManagerTests ,
1619+ )
15881620
15891621 support .run_unittest (* unit_tests )
15901622 support .reap_children ()
0 commit comments