mirror of
				https://github.com/python/cpython.git
				synced 2025-11-04 07:31:38 +00:00 
			
		
		
		
	bpo-30225: Fix is_valid_fd() on macOS Tiger (#1443)
is_valid_fd() now uses fstat() instead of dup() on macOS to return 0 on a pipe when the other side of the pipe is closed. fstat() fails with EBADF in that case, whereas dup() succeed.
This commit is contained in:
		
							parent
							
								
									5f161fd86d
								
							
						
					
					
						commit
						1c4670ea0c
					
				
					 1 changed files with 9 additions and 0 deletions
				
			
		| 
						 | 
					@ -1045,6 +1045,14 @@ initsite(void)
 | 
				
			||||||
static int
 | 
					static int
 | 
				
			||||||
is_valid_fd(int fd)
 | 
					is_valid_fd(int fd)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
 | 
					#ifdef __APPLE__
 | 
				
			||||||
 | 
					    /* bpo-30225: On macOS Tiger, when stdout is redirected to a pipe
 | 
				
			||||||
 | 
					       and the other side of the pipe is closed, dup(1) succeed, whereas
 | 
				
			||||||
 | 
					       fstat(1, &st) fails with EBADF. Prefer fstat() over dup() to detect
 | 
				
			||||||
 | 
					       such error. */
 | 
				
			||||||
 | 
					    struct stat st;
 | 
				
			||||||
 | 
					    return (fstat(fd, &st) == 0);
 | 
				
			||||||
 | 
					#else
 | 
				
			||||||
    int fd2;
 | 
					    int fd2;
 | 
				
			||||||
    if (fd < 0)
 | 
					    if (fd < 0)
 | 
				
			||||||
        return 0;
 | 
					        return 0;
 | 
				
			||||||
| 
						 | 
					@ -1057,6 +1065,7 @@ is_valid_fd(int fd)
 | 
				
			||||||
        close(fd2);
 | 
					        close(fd2);
 | 
				
			||||||
    _Py_END_SUPPRESS_IPH
 | 
					    _Py_END_SUPPRESS_IPH
 | 
				
			||||||
    return fd2 >= 0;
 | 
					    return fd2 >= 0;
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* returns Py_None if the fd is not valid */
 | 
					/* returns Py_None if the fd is not valid */
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue