mirror of
				https://github.com/python/cpython.git
				synced 2025-11-03 23:21:29 +00:00 
			
		
		
		
	Issue #10763: subprocess.communicate() closes stdout and stderr if both are
pipes (bug specific to Windows). Improve also the unit test: write a portable unit test.
This commit is contained in:
		
							parent
							
								
									291151b7f4
								
							
						
					
					
						commit
						667d4b577f
					
				
					 3 changed files with 24 additions and 14 deletions
				
			
		| 
						 | 
				
			
			@ -985,6 +985,7 @@ def wait(self):
 | 
			
		|||
 | 
			
		||||
        def _readerthread(self, fh, buffer):
 | 
			
		||||
            buffer.append(fh.read())
 | 
			
		||||
            fh.close()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        def _communicate(self, input):
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -366,22 +366,28 @@ def test_communicate(self):
 | 
			
		|||
        self.assertEqual(stdout, b"banana")
 | 
			
		||||
        self.assertStderrEqual(stderr, b"pineapple")
 | 
			
		||||
 | 
			
		||||
    # This test is Linux specific for simplicity to at least have
 | 
			
		||||
    # some coverage.  It is not a platform specific bug.
 | 
			
		||||
    @unittest.skipUnless(os.path.isdir('/proc/%d/fd' % os.getpid()),
 | 
			
		||||
                         "Linux specific")
 | 
			
		||||
    # Test for the fd leak reported in http://bugs.python.org/issue2791.
 | 
			
		||||
    def test_communicate_pipe_fd_leak(self):
 | 
			
		||||
        fd_directory = '/proc/%d/fd' % os.getpid()
 | 
			
		||||
        num_fds_before_popen = len(os.listdir(fd_directory))
 | 
			
		||||
        p = subprocess.Popen([sys.executable, "-c", "print()"],
 | 
			
		||||
                             stdout=subprocess.PIPE)
 | 
			
		||||
        for stdin_pipe in (False, True):
 | 
			
		||||
            for stdout_pipe in (False, True):
 | 
			
		||||
                for stderr_pipe in (False, True):
 | 
			
		||||
                    options = {}
 | 
			
		||||
                    if stdin_pipe:
 | 
			
		||||
                        options['stdin'] = subprocess.PIPE
 | 
			
		||||
                    if stdout_pipe:
 | 
			
		||||
                        options['stdout'] = subprocess.PIPE
 | 
			
		||||
                    if stderr_pipe:
 | 
			
		||||
                        options['stderr'] = subprocess.PIPE
 | 
			
		||||
                    if not options:
 | 
			
		||||
                        continue
 | 
			
		||||
                    p = subprocess.Popen((sys.executable, "-c", "pass"), **options)
 | 
			
		||||
                    p.communicate()
 | 
			
		||||
        num_fds_after_communicate = len(os.listdir(fd_directory))
 | 
			
		||||
        del p
 | 
			
		||||
        num_fds_after_destruction = len(os.listdir(fd_directory))
 | 
			
		||||
        self.assertEqual(num_fds_before_popen, num_fds_after_destruction)
 | 
			
		||||
        self.assertEqual(num_fds_before_popen, num_fds_after_communicate)
 | 
			
		||||
                    if p.stdin is not None:
 | 
			
		||||
                        self.assertTrue(p.stdin.closed)
 | 
			
		||||
                    if p.stdout is not None:
 | 
			
		||||
                        self.assertTrue(p.stdout.closed)
 | 
			
		||||
                    if p.stderr is not None:
 | 
			
		||||
                        self.assertTrue(p.stderr.closed)
 | 
			
		||||
 | 
			
		||||
    def test_communicate_returns(self):
 | 
			
		||||
        # communicate() should return None if no redirection is active
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -11,6 +11,9 @@ Core and Builtins
 | 
			
		|||
Library
 | 
			
		||||
-------
 | 
			
		||||
 | 
			
		||||
- Issue #10763: subprocess.communicate() closes stdout and stderr if both are
 | 
			
		||||
  pipes (bug specific to Windows).
 | 
			
		||||
 | 
			
		||||
- Issue #1693546: fix email.message RFC 2231 parameter encoding to be in better
 | 
			
		||||
  compliance (no "s around encoded values).
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue